vb.net - Update Text Box Properly when Cross-threading in Visual Basic (VS 2012 V11) -


  • here question in short: how use backgroundworker (or invokerequired method) make thread-safe calls append text text box?

  • here question in detail , background: i've been working on program copies file 1 location backup purposes. set option save file when file modified using filesystewatcher. here code:

    private sub form1_load(byval sender system.object, byval e system.eventargs) handles me.load      dim directorypath string = path.getdirectoryname(textbox1.text)     dim varfilesystemwatcher new filesystemwatcher()     varfilesystemwatcher.path = directorypath      varfilesystemwatcher.notifyfilter = (notifyfilters.lastwrite)     varfilesystemwatcher.filter = path.getfilename(textbox1.text)     addhandler varfilesystemwatcher.changed, addressof onchanged     varfilesystemwatcher.enableraisingevents = true  end sub  private sub onchanged(source object, byval e filesystemeventargs)      my.computer.filesystem.copyfile(e.fullpath, textbox2.text & "\" & e.name, true)     textbox3.apendtext("[new text]") ' line causes error end sub 

the code works fine except updating text in textbox3. need textbox update when file specified modified. important user can know program working , have complete log of programs operations.

the error caused is:

cross-thread operation not valid: control 'textbox3' accessed thread other thread created on.

i'm assuming "addhandler varfilesystemwatcher.changed, addressof onchanged" creates new thread. also, updating "textbox3" within "onchange sub" (in way did) not thread-safe manner. did research , found article:

how to: make thread-safe calls windows forms controls

the article explains 1 can use invokerequired or backgroundworker make thread-safe calls. use backgroundworker make thread-safe call (if invokerequired more efficient i'll use that), portion, of example provied, leaves me confused:

' need these imports use backgroundworker or invokerequired? imports system imports system.componentmodel imports system.threading imports system.windows.forms  public class form1    inherits form ' need trying do?     ' delegate enables asynchronous calls setting    ' text property on textbox control.    delegate sub settextcallback([text] string)     ' thread used demonstrate both thread-safe ,    ' unsafe ways call windows forms control.    private demothread thread = nothing     ' backgroundworker used demonstrate     ' preferred way of performing asynchronous operations.    private withevents backgroundworker1 backgroundworker     private textbox1 textbox    private withevents settextunsafebtn button    private withevents settextsafebtn button    private withevents settextbackgroundworkerbtn button     ' part of code , need it?    private components system.componentmodel.icontainer = nothing     ' again, part of code , need it?    public sub new()       initializecomponent()     end sub     ' , again, part of code , need it?    protected overrides sub dispose(disposing boolean)       if disposing andalso (components isnot nothing)          components.dispose()       end if       mybase.dispose(disposing)     end sub 

many parts of above code leave me confused. do? parts of code need use backgroundworker? parts invokerequired method? again, how use backgroundworker (or invokerequired method) make thread-safe calls append text text box? (it'd great have code above explained, need 1 example of how update text of text box in thread-safe manner.)

change:

private sub onchanged(source object, byval e filesystemeventargs)     my.computer.filesystem.copyfile(e.fullpath, textbox2.text & "\" & e.name, true)     textbox3.apendtext("[new text]") ' line causes error end sub 

to:

private sub onchanged(source object, byval e filesystemeventargs)     my.computer.filesystem.copyfile(e.fullpath, textbox2.text & "\" & e.name, true)     appendtextbox(textbox3, "[new text]") end sub  private delegate sub appendtextboxdelegate(byval tb textbox, byval txt string)  private sub appendtextbox(byval tb textbox, byval txt string)     if tb.invokerequired         tb.invoke(new appendtextboxdelegate(addressof appendtextbox), new object() {tb, txt})     else         tb.appendtext(txt)     end if end sub 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -