0

I have tried to access a GUI element (a ToolStripStatusLabel) from inside a STA thread. Im having all sorts of issues trying to get invoke on it, thats becuase now i know there is no invoke for ToolStripStatusLabel, my latest error is: Error 1 'System.Windows.Forms.ToolStripStatusLabel' does not contain a definition for 'Invoke

I cant seem to update it without invoke for i get the error: System.NullReferenceException' occurred

What are my options here? How would i go about doing this, changing the statuslabel.text on the GUI thread from inside a STA thread.

I have used all manors of standard invoke code and control extensions found here but still the same issues.

CODE AS REQUESTED:

private void buttonclick(object sender, EventArgs e)
{
    CheckForIllegalCrossThreadCalls = false;
    var thread = new Thread(() =>
    {
        var ie = watinBrowser();
        log("[!] Starting Application");
        log("blahblah");

    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();

}

private void log(string logIt)
{       

    statusUpdate(logIt);

}

private void statusUpdate(string text)
{
    if (this.statusStrip1.InvokeRequired)
        {
            this.statusStrip1.Invoke(new MethodInvoker(() => this.toolStripStatusLabel1.Text = text));
        }
    else 
        {
            this.toolStripStatusLabel1.Text = text;
        }    
}
scott
  • 1,531
  • 2
  • 16
  • 29
  • Try to use `Invoke` of the form instead – Tu Tran Jan 20 '15 at 04:38
  • I only have one form, how would i go about using invoke on the form when its the statuslabel i want to change? Any code samples would be much appreciated. – scott Jan 20 '15 at 04:39
  • show your code that starts the STA thread and that updates the label – kennyzx Jan 20 '15 at 04:41
  • @scott: Because statuslabel and its container form in on the same thread – Tu Tran Jan 20 '15 at 04:44
  • you can use an even in the STA class to trigger it to form and get the text value in the event as return data. Well, you should write your code for a better visualization. – Rohit Prakash Jan 20 '15 at 04:47
  • code posted, i have a log function that updates status bar also, function is called inside another function that runs in STA thread, statusbar runs on GUI thread. – scott Jan 20 '15 at 04:56
  • I don't find any error in your code. that must work unless, you are either have toolStripStatusLabel1 uninitialized or your are nullifying it in GUI thread parallely. – Rohit Prakash Jan 20 '15 at 07:21
  • I appreciate the try Peter, Yes ROHIT i agree, rather strange isnt it.... Toolstripstatuslabel one dosnt need to be initialized? this is VS i whacked it into the form and there it is... whats more to do ? I dont remember all the other multithreaded applications i made the same way behaving like this... is this a strange .net 4.5 thing ??? – scott Jan 21 '15 at 00:18
  • Im but im having a hard time understand how the hell a toolstripstatuslabel can be a null value expecially when im not trying to receive the value..... im trying to pass it a value.. – scott Jan 21 '15 at 00:28

0 Answers0