gpt4 book ai didi

c# - 计时器说它已启用,但从不执行

转载 作者:行者123 更新时间:2023-11-30 18:39:23 25 4
gpt4 key购买 nike

我有一个 sub 启动两个计时器之一(取决于“区域”条件)。这个名为“CheckAndActivateRelays”的子程序本身由串行端口_DataReceived 事件调用。我正在插入断点以帮助我进行故障排除,并且看到 tmrSoundSirensAfterDelay.Start() 行正在成功执行,计时器的状态甚至更改为已启用。但是,关联的 Tick 事件从不执行其中包含的任何代码。

如果我通过从 button24 的点击事件中调用子程序来做同样的事情,它会完美地工作。一切都在同一个表单上,没有线程进程。

有人吗?谢谢

private void checkAndActivateRelays(int zoneNumber)
{

if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
{
if (zoneNumber == 1) //Entry zone
{
//kick off a timer after delay specified in Settings1 file,
if (Settings1.Default.alarmSirenDurationInMinutes != 0)
{
//activates the relays if global alarm flags are still set to true
//(i.e. user has not entered code in time)
globalAlarmEntryDurationTicks = 0;
tmrSoundSirensAfterDelay.Start();

}
}
else //If any other zone is activated during alarm set condition
{
if (Settings1.Default.alarmSirenDurationInMinutes != 0)
{
//Output to relays 1 & 2
spIOCard.Write("~out10=1~");
spIOCard.Write("~out11=1~");

//then close after duration from Settings1 file
globalAlarmSirenDurationTicks = 0;
tmrSoundSirens.Start();
}
}

}

}

private void tmrSoundSirensAfterDelay_Tick(object sender, EventArgs e)
{
globalAlarmEntryDurationTicks = globalAlarmEntryDurationTicks + 1;

if (globalAlarmEntryDurationTicks == Settings1.Default.alarmEntryDelayInSeconds) //Value from Settings1 file
{
spIOCard.Write("~out10=1~");
spIOCard.Write("~out11=1~");
globalAlarmEntryDurationTicks = 0;
tmrSoundSirensAfterDelay.Stop();
tmrSoundSirens.Start();
}
}

private void tmrSoundSirens_Tick(object sender, EventArgs e)
{
globalAlarmSirenDurationTicks = globalAlarmSirenDurationTicks + 1;

if (globalAlarmSirenDurationTicks == (Settings1.Default.alarmSirenDurationInMinutes * 5)) //*60 Value from Settings1 file
{
spIOCard.Write("~out10=0~");
spIOCard.Write("~out11=0~");
globalAlarmSirenDurationTicks = 0;
tmrSoundSirens.Stop();
}
}



private void button24_Click(object sender, EventArgs e)
{
globalFullAlarmSet = true;
checkAndActivateRelays(1);
}

串口数据接收码:

 private void spIO_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = spIOCard.ReadExisting();

if (RxString == "~in00=1~")
{
checkAndActivateRelays(1);
button10.BackColor = System.Drawing.Color.Red;
}

if (RxString == "~in00=0~")
{
button10.BackColor = System.Drawing.Color.LightGray;
}

if (RxString == "~in01=1~")
{
checkAndActivateRelays(2);
button11.BackColor = System.Drawing.Color.Red;
}

if (RxString == "~in01=0~")
{
button11.BackColor = System.Drawing.Color.LightGray;
}

if (RxString == "~in02=1~")
{
button12.BackColor = System.Drawing.Color.Red;
}

if (RxString == "~in02=0~")
{
button12.BackColor = System.Drawing.Color.LightGray;
}

最佳答案

自从您使用 DataReceivedEvent 后需要考虑的事项.根据 MSDN,它是在辅助线程上引发的。这可能是您遇到问题的原因。

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

关于c# - 计时器说它已启用,但从不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9951854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com