Hi guys...I have ready program which often hangs when the textBox is refreshing with content.
I don't want to rewrite the code so I decided to add backgroundworker.
My Code structure is like this

private void startButton_Click(object sender, RoutedEventArgs e)
{
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // there are many IFs related to the options choosen in the GUI
    if (option == "1")
    {
        LogBox.Text = "Option 1 is Starting";
        option1();
    }
    if (option == "2")
    {
        LogBox.Text = "Option 2 is Starting";
        option2();
    }
    ...
} 

The problem is that I receive infamous error "BackgroundWorker, Cross-thread operation not valid"
I heard that I should use delegates...Can somebody give show me some examples based on the code above?
DO I need to use delegates within option1 and option2, whenever I want to put some text into LogBox?

Thanks for help!

Recommended Answers

All 5 Replies

i guess you should add a delegate with a threadsafe in it.. only a guess ;)

 delegate void SetTextBox_Delegate(TextBox tb);

 protected void SetTextBox_ThreadSafe(TextBox tb)
        {
          if (tb.InvokeRequired) 
         {
    SetTextBox_Delegate MyDelegate = new  SetTextBox_Delegate(SetTextBox_ThreadSafe);
          this.Invoke(MyDelegate, new object[] {tb});
         } 
         else 
          {

            //Inline Code  Here

          }
        }


    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        SetTextBox_ThreadSafe(LogBox);
    } 

ok - I have created function:

private void Start_Click(object sender, EventArgs e)
{

    Thread t = new Thread(start);
    t.Start();
}

Start looks like this:

private void start()
{
    // there are many IFs related to the options choosen in the GUI
    if (option == "1")
    {
        LogBox.Text = "Option 1 is Starting";
        option1();
    }
    if (option == "2")
    {
        LogBox.Text = "Option 2 is Starting";
        option2();
    }
    ...
} 

And it works almost ok...
I had to change all LogBox.Text to

LogBox.BeginInvoke(new EventHandler(delegate {LogBox.Text = "SOME TEXT";}));

Unfortunely it seems to be very tiresome.
I have 100+ LogBox.Text commands in the whole code (every option has it).And I dont want to modify everything...Is there any better way to do that?

hmm.. Did you tried this?... just cut paste it on the else section of the threadsafe..
as long all the textbox is "LogBox"

 delegate void SetTextBox_Delegate(TextBox tb);
 protected void SetTextBox_ThreadSafe(TextBox tb)
        {
          if (tb.InvokeRequired) 
         {
    SetTextBox_Delegate MyDelegate = new  SetTextBox_Delegate(SetTextBox_ThreadSafe);
          this.Invoke(MyDelegate, new object[] {tb});
         } 
         else 
          {
            // there are many IFs related to the options choosen in the GUI
         if (option == "1")
             {
                  LogBox.Text = "Option 1 is Starting";
                   option1();
             }
             if (option == "2")
                 {
                  LogBox.Text = "Option 2 is Starting";
                      option2();
             }
             ...
          }
        }
    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        SetTextBox_ThreadSafe(LogBox);
    } 

Hmm Sorry for noobish question:

private void Start_Click(object sender, EventArgs e) // THIS IS A START BUTTON CLICK EVENT
{
    Thread t = new Thread(start);
    t.Start();
}

how to connect it with your code... :(

Ok I have made the delegation
and changed every Logbox.text to my modifed new function AddText...
And its working..
Replace function built in VS made it fast...

THX for help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.