vb.net - Form is not updating, after custom class event is fired -
i'm having issue main form isn't updating though see event fire off. let me explain situation , share of code i'm sure horrible since i'm amateur.
i created class take in settings running process in background. add custom events in class use in form instead of timer.
i put break on 2 subs handle events , see them kicked off install starts.
i @ data , it's coming across , no exceptions thrown.
at first thought because datagridview had latency issues. set double buffered through tricks found didn't matter. there still 10 second delay before data showed in datagrid.
i thought , decided didn't need datagridview , replaced control multiline textbox, didn't make difference. it's still taking 10 seconds or longer show updates form/textbox.
i've included of code below.
public shared withevents np newprocess
private sub form1_load(byval sender object, byval e system.eventargs) handles me.load try np = new newprocess addhandler np.installfinished, addressof np_installfinished addhandler np.installstarted, addressof np_installstarted catch ex exception end try end sub
protected sub np_installfinished(byval description string, byval exitcode integer) installinprocess = false if not description = nothing if not exitcode = nothing addlog(string.format("completed install of {0} ({1}).", description, exitcode)) else addlog(string.format("completed install of {0}.", description)) end if end if refreshbuttons() updatelistofapps() np.dispose() end sub
protected sub np_installstarted(byval description string) installinprocess = true if not description = nothing addlog(string.format("started install of {0}.", description)) end sub
public class newprocess dim processname string dim processvisibile boolean dim arguments string dim waitforexit boolean dim description string dim shellexecute boolean dim ec integer = nothing 'exit code private isbusy boolean = nothing dim th threading.thread public event installfinished(byval description string, byval exitcode integer) public event installstarted(byval description string) public function busy() boolean if isbusy = nothing return false return isbusy end function public function exitcode() integer return ec end function public function processdescription() string return description end function ''' <summary> ''' starts new multithreaded process. ''' </summary> ''' <param name="path">path of file run</param> ''' <param name="visible">should application visible?</param> ''' <param name="arg">arguments</param> ''' <param name="waitforexit">wait application exit?</param> ''' <param name="description">description show in logs</param> ''' <remarks>starts new multithreaded process.</remarks> public sub startprocess(byval path string, byval visible boolean, optional byval arg string = nothing, optional byval waitforexit boolean = false, optional byval description string = nothing) try me.processname = path me.processvisibile = visible if arguments = nothing me.arguments = arg me.description = description me.waitforexit = waitforexit if isbusy , waitforexit messagebox.show("another install in process, please wait previous install finish.", "warning", messageboxbuttons.ok, messageboxicon.warning) exit sub end if if not fn_fileexists(processname) messagebox.show("could not find file " & processname & ".", "could not start process because file missing.", messageboxbuttons.ok, messageboxicon.error) exit sub end if th = new threading.thread(addressof newthread) th .isbackground = true if not description nothing .name = description .start() end catch ex exception end try end sub private sub newthread() dim p process try p = new process p .enableraisingevents = true .startinfo.arguments = arguments .startinfo.filename = processname .startinfo.createnowindow = processvisibile end if processvisibile p.startinfo.windowstyle = processwindowstyle.normal else p.startinfo.windowstyle = processwindowstyle.hidden end if p.start() isbusy = true raiseevent installstarted(description) if waitforexit while p.hasexited = false threading.thread.sleep(500) loop isbusy = false raiseevent installfinished(description, p.exitcode) end if ec = p.exitcode catch ex exception end try end sub public sub dispose() processname = nothing processvisibile = nothing arguments = nothing waitforexit = nothing description = nothing ec = nothing installinprocess = nothing th.join() memorymanagement.flushmemory() end sub end class
sub addlog(byval s string) try s = string.format("[{0}] {1}", timeofday.toshorttimestring, s) form1.tblogs.appendtext(s & vbcrlf) using st new streamwriter(logfilepath, true) st.writeline(s) st.flush() end using catch ex exception end try end sub
any idea's? i'm @ complete loss.
i've tried adding application.doevents, me.refresh , quite few other things :(
form1.tblogs.appendtext(s & vbcrlf)
standard vb.net trap. form1 class name, not reference form. unfortunately, vb.net implemented anachronism vb6 legal. falls apart when use threads. you'll another form object automatically created, 1 isn't visible because show() method never called. otherwise dead doornail since thread not pumping message loop.
you'll need pass reference actual form object user looking @ worker class. value of me in form1 code. have use control.invoke since isn't legal update controls thread. recommend fire event instead, 1 form1 can subscribe to, worker class isn't infected implementation details of ui.
Comments
Post a Comment