Note that there are some explanatory texts on larger screens.

plurals
  1. POTo change properites of control in another thread out of Main (UI) thread
    primarykey
    data
    text
    <p>I'm now implementing the network program using the namedpipe and C#. Within the server program's main method, I started one thread named "ReadingMessage" which is to read the messages from the client program. In this thread, it needs to change the properties of controls (Text of textbox, Position of labels, etc.) based on the messages returned from the client. Therefore, it had cross-threaded errors in this procedure ("ReadingMesssage between UI thread"). I found out that there are two ways to solve this problem </p> <ol> <li>Invoke the controls using Delegate</li> <li>Form.CheckForIllegalCrossThreadCalls = false;</li> </ol> <p>I tested both of them but there is one problem. When the form started as first time, the properties of control are changed without any errors. When the form is closed and started again as second time, the properties of control are not changed and any error messages are not shown. I catch the breakpoint to the code where the properties of controls are instructed to change. The code run smoothly but the problem still occurs.</p> <p>I don't know what's that problem. Therefore, if anyone knows this problem, please suggest me...</p> <p><strong>Update: (Posting Code and Correcting some errors in my first question)</strong></p> <p>The thread <strong>is not in the main method</strong> of server program. I was wrong because I wanted to describe my question in short. Now, I will describe my question in more detail. There is one button click event in the main form of server program. </p> <pre><code>//Main Form public partial class MainForm : Form { private void btnButton_Click(object sender, EventArgs e) { AnotherForm aform = new AnotherForm(); //This is the form where threading run aform.show(); } } //AnotherForm public partial class AnotherForm : Form { private void CargoLoading_Load(object sender, EventArgs e) { (new System.Threading.Thread(readingMessage)).Start(myPipe); } } private void readingMessage(Object myPipeObject) //Read the messages send from the client machine { while (true) { //Code of serverstream SetTextofTextBox1("AnyText"); //Code where the properties of control are instructed to change } } private void SetTextofTextBox1(string text) { if (this.txtTextBox1.InvokeRequired) //the debugger return false at second time of this form is opened { SetTextCallback d = new SetTextCallback(SetTextofTextBox1); this.Invoke(d, new object[] { text }); } else { this.txtTextBox1.Text = text; this.txtTextBox1.Update(); } } </code></pre> <p>That's all the code where problem occur. When "AnotherForm" is open as first time, it's OK. But it is opened again as second time, the properties of control (text of TextBox1) is not effected and not changed. At there, I noticed that the debugger return false to <code>Control.InvokeRequired</code> at this second time. Really sorry for all for long question because I'm a novice as a programmer.</p> <p>Still expecting your suggestions...</p> <p><strong>Update 2:</strong> </p> <p>Finally I tried all the ways but it doesn't come out the best solution. Therefore, I would like to suggest all to just avoid this condition and re-write some of your code in another concept when u meet this situation like me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload