none
can't AppendText when thread started RRS feed

  • Question

  • hello guys 

    when i start thread command AppendText for textbox he did't work 

              int IntHome;
              string[] List;
              private static string[] ListSplit = new string[1];
              private static bool StopChecker = false;
    
          private void button2_Click(object sender, EventArgs e)
            {
                                 Thread thread = new Thread(delegate (object a0)
                        {
                            this.StartWork(Conversions.ToInteger(RuntimeHelpers.GetObjectValue(a0)));
                        });
                        thread.Start(TextBox4.Text);
        
    
            }
    
           public void StartWork(int ga6o)
            {
                Application.DoEvents();
                checked
                {
                    if (ga6o > this.List.Length - 1)
                    {
                        ga6o = this.List.Length - 1;
                    }
                    Form1.ListSplit = new string[ga6o + 1 - 1 + 1];
                    int num = Form1.ListSplit.Length - 1;
                    for (int i = 0; i <= num; i++)
                    {
                        Form1.ListSplit[i] = "";
                        Thread thread = new Thread(delegate (object a0)
                        {
                            this.Working(Conversions.ToInteger(RuntimeHelpers.GetObjectValue(a0)));
                        }, 100);
                        thread.Start(i);
                    }
                    for (; ; )
                    {
                        try
                        {
                            if (Form1.StopChecker)
                            {
                                break;
                            }
                            int num2 = Form1.ListSplit.Length - 1;
                            for (int j = 0; j <= num2; j++)
                            {
                                if (Form1.ListSplit[j].Length <= 0)
                                {
                                    if (this.IntHome > this.List.Length - 1)
                                    {
                                        this.IntHome = 0;
    
                                        return;
                                    }
                                    Form1.ListSplit[j] = this.List[this.IntHome];
                                    this.IntHome++;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        Thread.Sleep(1);
                    }
                }
    
            }
           public void Working(int klb)
            {
                try
                {
                    while (!StopChecker)
                    {
                        if (ListSplit[klb].Length > 0)
                        {
                            stre = ListSplit[klb];
                          ListSplit[klb] = "";
                            bool flag2 = Conversions.ToBoolean(RuntimeHelpers.GetObjectValue(CheckUser(stre)));
                            if (flag2)
                            {
                                Textbox1.AppendText(stre)
                            }
                        }
                        else
                        {
                            TextBox3.AppendText(ListSplit[klb]);
                        }
                        int slep = Convert.ToInt32(TextBox4.Text);
                        Thread.Sleep(slep);
                    }
                }
                catch (Exception ex)
                {
                }
    
            }

    Monday, December 16, 2019 1:41 PM

Answers

  • Hello,

    The first thing to do is a) get rid of the try-catch b) keep the try-catch and actually do something, if nothing else get the error message.

    An educated guess is you need to use InvokeRequied off the TextBox and if InkokeRequired is try use Invoke as per the following Microsoft documentation (see WriteTextSafe).

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 16, 2019 2:23 PM
    Moderator
  • Hi castlehere,

    Thank you for posting here.

    In WinForm, when we trying to modify control's value using another thread, an exception usually appears: System.InvalidOperationException: 'Cross-thread operation not valid: Control' textBox1 'accessed from a thread other than the thread it was created on.'

    Because your catch block contains nothing, you can only see that the modification was unsuccessful.

    Like Karen said, you can use Invoke to make changes take effect.

       private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(ThreadProc));
                thread.Start();
            }
            private void ThreadProc()
            {
                textBox1.Invoke((Action)delegate
                {
                    textBox1.Text = "Test";
                });
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 17, 2019 7:08 AM

All replies

  • Hello,

    The first thing to do is a) get rid of the try-catch b) keep the try-catch and actually do something, if nothing else get the error message.

    An educated guess is you need to use InvokeRequied off the TextBox and if InkokeRequired is try use Invoke as per the following Microsoft documentation (see WriteTextSafe).

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 16, 2019 2:23 PM
    Moderator
  • Hi castlehere,

    Thank you for posting here.

    In WinForm, when we trying to modify control's value using another thread, an exception usually appears: System.InvalidOperationException: 'Cross-thread operation not valid: Control' textBox1 'accessed from a thread other than the thread it was created on.'

    Because your catch block contains nothing, you can only see that the modification was unsuccessful.

    Like Karen said, you can use Invoke to make changes take effect.

       private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(ThreadProc));
                thread.Start();
            }
            private void ThreadProc()
            {
                textBox1.Invoke((Action)delegate
                {
                    textBox1.Text = "Test";
                });
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, December 17, 2019 7:08 AM
  • Hi castIlhere,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 7, 2020 3:21 AM