5

I'm starting new thread:

Thread t = new Thread(UpdateListOutput);    
t.IsBackground = true;    
t.Start();

UpdateListOutput:

void UpdateListOutput()
{
    while (true)
    {
        if (!string.IsNullOrEmpty(engineOutput))
        {
            OutputBox.Items.Add(engineOutput);
        }
    }
}

And I recive error:

Cross-thread operation not valid: Control 'OutputBox' accessed from a thread other than the thread it was created on.

How can I run this?

Lion
  • 18,729
  • 22
  • 80
  • 110
Carlj28
  • 125
  • 2
  • 2
  • 7
  • 1
    possible duplicate of [Accessing a form's control from a separate thread](http://stackoverflow.com/questions/7609839/accessing-a-forms-control-from-a-separate-thread) – dparsons Aug 30 '13 at 13:48

3 Answers3

17

Try this

this.Invoke((MethodInvoker)(() => OutputBox.Items.Add(engineOutput)));
Nick
  • 1,903
  • 2
  • 21
  • 40
  • This is working in mono too. I'm getting xcb_xlib_too_much_data_requested error when updating listbox from multiple threads and this is solved. Thx. – kodmanyagha Jul 15 '18 at 07:52
0

try this

void UpdateListOutput()
    {

            Dispatcher.BeginInvoke(new Action(() => {
                while (true)
                {
                    if (!string.IsNullOrEmpty(engineOutput))
                    {
                        OutputBox.Items.Add(engineOutput);
                    }
                }                

            }));


    }
Fly Rose
  • 11
  • 4
-1

I have a method seleniumCode() that uses the 'chromedriver' of Selenium to do some things. this method is invoked by clicking a button on de GUI, which uses the main thread of the execution. When this method is running, the GUI is 'blocked' 'cause the main thread is collapsed by the seleniumCode(). To make this work right, I called seleniumCode() like this:

Thread th = new Thread(new ThreadStart(seleniumCode));
th.Start();

Here's the problem: I have some calls to 'writeLbx("each string")' inside seleniumCode(). And I can't write on the ListBox 'cause it's from another thread. So, after 2 weeks of brain-crashing, I arrived here.

I picked parts of some of your codes to make this:

private void writeLbx(string s)
{
    switch (s)
    {
        case "Empezando_Tracking": lbxProcess.Items.Add("xxx");
                    break;
        case "Mi Cuenta": lbxProcess.Items.Add("xxx");
                    break;
        case "Email_Pass": lbxProcess.Items.Add("xxx");
                    break;
        case "Iniciar sesión": lbxProcess.Items.Add("xxx");
                    break;
        case "Procesando_Intento": lbxProcess.Items.Add("xxx");
                    break;
        case "Precio_OK": lbxProcess.Items.Add("xxx");
                    break;
        case "Poner_Cantidad": lbxProcess.Items.Add("xxx");
                    break;
    }

    lbxProcess.Update();
    lbxProcess.TopIndex = lbxProcess.Items.Count - 1;
}

... into this:

private void writeLbx(string s)
{

    this.Invoke(new Action(() =>
    {

        switch (s)
        {
            case "Empezando_Tracking": lbxProcess.Items.Add("xxx");
                    break;
            case "Mi Cuenta": lbxProcess.Items.Add("xxx");
                    break;
            case "Email_Pass": lbxProcess.Items.Add("xxx");
                    break;
            case "Iniciar sesión": lbxProcess.Items.Add("xxx");
                    break;
            case "Procesando_Intento": lbxProcess.Items.Add("xxx");
                    break;
            case "Precio_OK": lbxProcess.Items.Add("xxx");
                    break;
            case "Poner_Cantidad": lbxProcess.Items.Add("xxx");
                    break;
        }

        lbxProcess.Update();
        lbxProcess.TopIndex = lbxProcess.Items.Count - 1;

    }));

}

And that simply change makes my code work just like I want.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • Is this supposed to be an answer with a solution to the problem described in the questin at the top of the page? It reads like "I had a similar but still different problem and that I solved by .... ." – Yunnosch Jan 05 '21 at 14:03
  • @Yunnosch This is a similar example, like you say. The same problem (how to add a item into a ListBox from other thread), but a solution that works made on my way. The user who has the problem will have to think how to make my solution work on his code. Is only help, you know? My way works, maybe it works for him too ¿? – Sergio Quinteiro Jan 06 '21 at 23:12
  • Please see [tour] and [answer]. You seem to have misunderstood the concept of StackOverflow, which is to make a Q/A collection and not a collection of questions and discussion of similar problems. You say "The user who has the problem will have to think how to make my solution work on his code.", which means you are not discussing his problem and describing how your experience helps with that. Instead you describe your problem and your solution and expext others to transfer to the question at the top of this page. Please either delete your unrelated answer or make it anser THIS QUESTION. – Yunnosch Jan 07 '21 at 07:22
  • You could phrase a conditional answer, like "If you problem is caused by ... then the solution is to .... because ... ." But please not as "I had a similar but different problem, maybe you can learn something from it, maybe not." – Yunnosch Jan 07 '21 at 07:24