Monday, 15 June 2015

.net - Multi-Threading Glitch -



.net - Multi-Threading Glitch -

hi have programme uses label called valve , textbox called variable

the gist if variable = 0 label colour gray, if variable = 1 label uses thread flashes between grayness , red.

this works apart if changing between 2 values (entering 0 deleting entering 1, , on) thread speed increases (as if multi-threading).

the unusual thing if swapping between values 0 & 1 (every 2 seconds +), doesn't increment blinking speed (this programme needs do)

this expanded code next question: vb.net multi threading

note: vb.net conversion project on visiwin.net. in illustration textbox variable actual variable read plc , label valve triangles representing process solenoid process flowsheet mimic. every solenoid controlled different variables.

imports microsoft.visualbasic imports scheme imports system.collections imports system.collections.generic imports system.drawing imports system.windows.forms imports system.threading imports system.diagnostics public class form1 private _flash boolean = false private sub variable1_textchanged(sender system.object, e system.eventargs) handles variable.textchanged if variable.text = "1" , not _flash _flash = true dim flashthread new thread(new threadstart(addressof flashlabel)) flashthread.start() end if if variable.text = "0" _flash = false valve.forecolor = color.gray end if end sub private sub flashlabel() dim _color color = color.gray while _flash if valve.forecolor = _color valve.forecolor = color.red else valve.forecolor = color.gray end if system.threading.thread.sleep(2000) end while end sub end class

the problem following:

you come in 1, _flash false: thread started, changes color , sleeps 2 seconds you come in 0 after entering 1, _flash true: _flash set false. you come in 1 after entering 0, _flash false again: new thread started.

now, if steps 2 , 3 happen while first thread sleeps, have 2 running threads. after first thread finished sleeping, see _flash true , go on running.

.net multithreading blinking

No comments:

Post a Comment