VB.Net Rookie label question



  • Greetings Gang,

     Hopefully someone can help me out here. I've been using VBScript for a long long time, and now I'm trying to write an application in VB.Net (2005) that will download and update our NAV definitions sooner then the control panel allows (Don't ask). Anyway, we own a component from NSoftware called IPWorks - the FTP component in particular.

    So, I've written a sub to handle the ConnectionStatus events, but when I try to update the label on the form I'm using I get an error about how I am making an Unsafe Thread Call. I've read the MSDN article about it, and tried a variety of things, but I don't quite grasp it well enough to actually get something working, apparently. The sub i'm using is:


    Private Sub ftp_ConStatus(ByVal sender As System.Object, ByVal e As nsoftware.IPWorks.FtpConnectionStatusEventArgs) Handles ftp.OnConnectionStatus
            MsgBox(e.ConnectionEvent & " Descrip: " & e.Description & " Status Code: " & e.StatusCode)
            mylbl.Text = e.ConnectionEvent & " Descrip: " & e.Description & " Status Code: " & e.StatusCode
        End Sub


    The MsgBox works, but setting mylbl.Text does not - so my question is, how would I get around that thread-safe call error in this particular instance, since this article serves only to confuse me. I'm beginning to think that instead of the Vb.Net Programmers Reference, I should have gotten the beginners guide - hehe, thanks!



  • Quite simple really, just make sure you call functions and properties across threads in the proper manner. Below, myReferecneToMyLabel is a label object who's reference points to a different thread. To change the label's text from this thread, I call ChangeLabelText.

    private void ChangeLabelText(string newText)
    {
        object[] theArgs = {newText};
        myReferenceToMyLabel.Invoke(new SafeContextLabelChanger(this.RealLabelChangerInTheSafeContext),theArgs);
    }

    private delegate void SafeContextLabelChanger(string newText);

    private void RealLabelChangerInTheSafeContext(string newText)
    {
        myReferenceToMyLabel.Text = newText;
    }



  • @GoatCheez said:

    Quite simple really, just make sure you call functions and properties across threads in the proper manner. Below, myReferecneToMyLabel is a label object who's reference points to a different thread. To change the label's text from this thread, I call ChangeLabelText.

    private void ChangeLabelText(string newText)
    {
        object[] theArgs = {newText};
        myReferenceToMyLabel.Invoke(new SafeContextLabelChanger(this.RealLabelChangerInTheSafeContext),theArgs);
    }

    private delegate void SafeContextLabelChanger(string newText);

    private void RealLabelChangerInTheSafeContext(string newText)
    {
        myReferenceToMyLabel.Text = newText;
    }



    Ahhh, thank you very much! I appreciate the help. Now, to go book shopping hehe


Log in to reply