I'm creating a C# program that has a sizable textbox that prints out data every 5 seconds. Whatever data that is displayed is (supposed to be) cleared and replaced with new data every 5 seconds. I've created a thread so that the window opens (main thread) and the data is processed, displayed, and paused as necessary (my thread), otherwise the window waits 5 seconds to open. I've had problems where I couldn't output text to the textbox because an error would say that the textbox was created on a different thread. However, I found a solution to that problem. But a new problem has arisen. I cannot clear the textbox using the same method I found to write text out to it across the threads. Does anybody know how to do that? Simply saying, textbox.text = ""; does not work. It merely prints a blank line before printing out the expected data. And saying texbox.Clear(); does not work and gives me the error I mentioned.

Below is an example of the code I'm using.

Thread t = new Thread(processData);
t.Start();

while (true)
                {
                    pauseForMilliseconds(5000);                              
                    clearTextBox("");     
                    processMoreData();
                }


private void clearTextBox(string text)
        {
            if (this.resultsTextBox.InvokeRequired)
            {
                SetTextCallback c = new SetTextCallback(printToTextBox);
                this.Invoke(c, new object[] { text });
            }
            else
            {
                resultsTextBox.Text = string.Empty;     
            }
        }

Nevermind. I found the problem almost instantly. The code,

SetTextCallback c = new SetTextCallback(printToTextBox);

was incorrect. It should have been

SetTextCallback c = new SetTextCallback(clearTextBox);
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.