vb.net - Test while running two functions at the same time -
i running these 2 simple functions using background workers, (write test file) , (read text file) shouldn't give errors getting these exceptions
a first chance exception of type 'system.io.ioexception' occurred in mscorlib.dll
any idea how prevent that?
option explicit on option strict on public class form1 dim istring string dim worker1 boolean = false dim worker2 boolean = false private sub button2_click(byval sender system.object, byval e system.eventargs) handles button2.click timer1.enabled = true end sub private sub timer1_tick(sender object, e eventargs) handles timer1.tick try if worker1 = false worker1 = true backgroundworker1.runworkerasync() end if if worker2 = false worker2 = true backgroundworker2.runworkerasync() end if catch ex exception debug.print("timer1_tick error:" & date.now & ex.message) end try end sub private sub backgroundworker1_dowork(byval sender system.object, byval e system.componentmodel.doworkeventargs) handles backgroundworker1.dowork call writedatatofile(date.now.tostring("h:mm:ss tt - fff"), "test.txt") end sub private sub backgroundworker2_dowork(byval sender system.object, byval e system.componentmodel.doworkeventargs) handles backgroundworker2.dowork istring = gettextdatacontents("test.txt") end sub private sub backgroundworker1_complete(byval sender system.object, byval e system.componentmodel.runworkercompletedeventargs) handles backgroundworker1.runworkercompleted worker1 = false end sub private sub backgroundworker2_complete(byval sender system.object, byval e system.componentmodel.runworkercompletedeventargs) handles backgroundworker2.runworkercompleted worker2 = false label1.text = istring end sub public sub writedatatofile(byval strdata string, byval strfile string, optional byval fullpath boolean = false) if fullpath = false strfile = system.appdomain.currentdomain.basedirectory & strfile dim notinuse = true while notinuse try using objwriter new system.io.streamwriter(strfile, false) objwriter.write(strdata) objwriter.close() notinuse = false end using catch ex exception debug.print("writedatatofile error:" & date.now & ex.message) system.threading.thread.sleep(1) end try end while end sub function gettextdatacontents(byval strfile string, optional byval fullpath boolean = false) string gettextdatacontents = nothing if fullpath = false strfile = system.appdomain.currentdomain.basedirectory & strfile dim notinuse = true while notinuse try dim strcontents string dim objreader system.io.streamreader objreader = new system.io.streamreader(strfile, true) strcontents = objreader.readtoend() objreader.close() notinuse = false return strcontents catch ex exception debug.print(date.now & " gettextdatacontents error " & ex.message) system.threading.thread.sleep(1) end try end while end function end class
the file may locked editing while writing it.
see article more details - http://msdn.microsoft.com/en-us/library/vstudio/kztecsys%28v=vs.100%29.aspx
Comments
Post a Comment