gpt4 book ai didi

c# - C#如何根据外部条件使用线程处理标志

转载 作者:行者123 更新时间:2023-12-02 23:45:34 24 4
gpt4 key购买 nike

我是C#的初学者,遇到以下问题。我的想法是:我有一些可能的触发器[water_level_threshold1water_level_threshold2]。如果water_level_threshold2处于 Activity 状态,则water_level_threshold1为false [threshold1 = true表示水位在两个值之间]。根据触发器,我想激活与两个警报相对应的两个声音。每隔几毫秒就会监视一次状况,我的警报会持续几秒钟。我需要异步播放声音,因为我不想停止水位监控。水位当然可以随机变化。此刻,例如对于第一个阈值,我写了类似以下内容:

if ((!water_level_threshold1_sound_already_started) && (water_level_threshold1))
{
using (SoundPlayer player = new SoundPlayer(@"C:\Users\Antonino\Desktop\water_level_threshold1_alarm.wav"))
{
player.PlayLooping();
}

// to avoid the sound to be stuck on its first msecs [sampling time]
water_level_threshold1_sound_already_started = true;
}

soundPlayer每次只能处理一首曲子,所以它们不会重叠。而且,循环确保我可以按照自己的意愿在该状态下播放乐曲。但是,如果我有时间变化,例如:
threshold1 exceeded [alarm1 performs correctly]->
threshold2 exceeded [alarm2 performs correctly]->
** water level decreased then **->
threshold1 exceeded [no audio]->
threshold2 exceeded [no audio]

从第二次出现起,它们将不再工作,因为它们被告知已经启动,并且由于它们是在不同的线程中执行的,所以我找不到找到线程的方法:“我完成了执行,然后可以放入 water_level_threshold1_sound_already_started = false “,这样,如果我在这种情况下重新进入,将打开一个新线程并再次播放警报

任何详细的建议和/或解决方案将不胜感激

在此先感谢那些会尽力帮助的人

最佳答案

在这种情况下,BackgroundWorker可能会有所帮助。
在DoWork事件中,您将介绍操作,并通过检查IsBusy属性来检查天气是否正在运行。
祝好运。

关于c# - C#如何根据外部条件使用线程处理标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32041385/

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