vb.net undertanding invoke/delegate

Discussion in 'Application Software' started by Stannieman, Jul 5, 2010.

  1. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,818
    90
    #1 Stannieman, Jul 5, 2010
    Last edited by a moderator: Apr 20, 2017
    Hi, I'm trying to show text in a label and disable/enable buttons etc from within a backgroundworker.
    I'm aware of the fact that just puting
    Code:
    lblLabel.text = ""
    in the backgroundworker sub will result in an error because the label isn't updated from the form tread.

    I read a bit on the web about invoking but I can't get it to work. It will probable work by just copy past the code from the web but I can't do anything with that in the future if I don't understand what that code is doing.

    So I made test project with a form with 1 label and 1 button and this code:
    Code:
    Public Class Form1
        Private Delegate Sub delegateInvoke()
        Dim delegateInvoketest As delegateInvoke
    
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            bkgInvokeTest.RunWorkerAsync()
        End Sub
    
        Private Sub bkgInvokeTest_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkgInvokeTest.DoWork
            delegateInvoketest = New delegateInvoke(AddressOf updatelabel)
            delegateInvoketest.Invoke()
        End Sub
    
        Private Sub updatelabel()
            lblReport.Text = "hello world"
        End Sub
    End Class
    This isn't working cause I still get the error about crosstread stuff, but it's almost directly copy past.
    Is there anyone who can tell me how I should do this?

    EDIT: Topic title should be vb.net underStanding...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,818
    90
    OK managed to do it using progresschanged.
    I think it's not the best/cleanest way to do it, but at least it works and I undertand how that works.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...