multithreading - C# how to handle a flag with a thread depending on external conditions -
i'm beginner c# , have following problem. idea is: have possible triggers [water_level_threshold1
, water_level_threshold2
]. if water_level_threshold2
active water_level_threshold1
false [threshold1=true means water level between 2 values]. depending on trigger want activate 2 sounds corresponding 2 alarms. conditions monitored every few milliseconds , alarms last seconds. need sounds played asynchronously because don't want stop water level monitoring. water level can of course changes randomly. @ moment, example first threshold, wrote like:
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(); } // avoid sound stuck on first msecs [sampling time] water_level_threshold1_sound_already_started = true; }
as far know soundplayer can handle tune per time not overlap. , loop ensures me tune played until in state, want. if have variation in time like:
threshold1 exceeded [alarm1 performs correctly]-> threshold2 exceeded [alarm2 performs correctly]-> ** water level decreased **-> threshold1 exceeded [no audio]-> threshold2 exceeded [no audio]
from second occurrence on not work anymore because told have been started , since executed in different threads couldn't find way make thread say: "i finished execution can put water_level_threshold1_sound_already_started = false
" if re-enter in situation new thread open , alarm play again
any detailed suggestion and/or solution appreciated
thanks in advance try help
a backgroundworker may helps in situation. in dowork event introduce operation , checking isbusy property can check weather it's running or not. luck.
Comments
Post a Comment