1.11M Members

Adding Multithread to existing code

 
0
 

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!

 
0
 

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);
    } 
 
0
 

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?

 
0
 

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);
    } 
 
0
 

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... :(

 
0
 

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!

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article