vb.net - Activating timer from another thread -
so purpose of check connection of ftp server , if ftp enable timer1. i've read threads don't work synchonized , causing problem. without thread works fine, program hangs , stops responding constantly.
how can activate timer thread? maybe invoking , delegating work? don't know how that.
public function canportopen(byval hostname string, byval port integer) boolean dim tcp new system.net.sockets.tcpclient try tcp.connect(hostname, port) catch end try if tcp.connected = true canportopen = true tcp.close() timer1.enabled = true else canportopen = false tcp.close() timer1.enabled = false ftpup.abort() end if end function public sub checkconnection() canportopen("hostname", port) end sub private sub timer2_tick(sender object, e eventargs) handles timer2.tick testftp = new system.threading.thread(addressof checkconnection) testftp.isbackground = true testftp.priority = threading.threadpriority.abovenormal testftp.start() end sub private sub timer1_tick(sender object, e eventargs) handles timer1.tick ftpup = new system.threading.thread(addressof uploadtoftp) ftpup.isbackground = true ftpup.priority = threading.threadpriority.abovenormal ftpup.start() end sub
i think before start getting in deep threads should start looking @ backgroundworker
component. can find on toolbar in designer , can drop on form. gives several events
dowork
- hook event whatever want done in background thread
runworkercompleted
- hook event run code in main (ui) thread, triggered when thread completes. code here can interact ui objects normal.
there other events allow report progress main thread, etc. purpose of backgroundworker component make simple multithreading tasks easier.
documentation -> here
see -> here examples of how pass data worker thread main thread using eventargs.
alternatively, if want run timer thread can :
'declare appropriate delegate delegate sub dlgtimerenable(byval enable boolean) 'the delegate should match method signature private sub timerenable(byval enable boolean) timer1.enabled = enable end sub
and in thread procedure
public function canportopen(byval hostname string, byval port integer) boolean dim tcp new system.net.sockets.tcpclient try tcp.connect(hostname, port) catch end try if tcp.connected = true canportopen = true tcp.close() me.invoke(new dlgtimerenable(addressof timerenable), new object() {true}) else canportopen = false tcp.close() me.invoke(new dlgtimerenable(addressof timerenable), new object() {false}) ftpup.abort() end if end function
here invoke
causes method executed on thread owns timer1
(assuming method in form me
refer form). arguments passed object.
you can general way work timer, example :
delegate sub dlgtimerenable(byref tmr timer, byval enable boolean) private sub timerenable(byref tmr timer, byval enable boolean) tmr.enabled = enable end sub
and :
me.invoke(new dlgtimerenable(addressof timerenable), new object() {timer1, true})
this makes delegate general - can pass timer , enable/disable it.
Comments
Post a Comment