Problem description:
When our interface needs to constantly update data while the program is running,
When the data of a textbox needs to change,
For this question, refer to my other article.
C #
Multithreading| Anonymous delegated parameter transfer | test the stress on your website | source code-upgraded version
To prevent the interface from being executedStuckThe best way isMultithreadingTo solve
A main thread to create the interface, use a sub-thread to execute the program andUpdateMain Interface
In this way, there will be no stuck images.
This is certainly no problem,
But why is there a lot of freezing problems in the process of using it, and some users tell me that it is my httphelper class, but it is not actually, again, I declare that my httphelper class has nothing to do with multithreading. Don't trust me anymore.
This problem has actually been difficult for me for a long time, but I have finally solved it today, and I have found many people have such problems, so I will share an example for your reference.
Let's take a look at my interface.
When I click
After execution
Data is constantly updated.
At this time, the interface will not be stuck,
Data is constantly updated.
See my code below
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. threading; namespace windowsformsapplication3 {public partial class form1: FORM {public form1 () {initializecomponent () ;}// create a delegate to access the Textbox Control. Public Delegate void updatetxt (string MSG); // defines a delegate variable public updatetxt; // modifies the textbox value. Public void updatetxtmethod (string MSG) {richtextbox1.appendtext (MSG + "\ r \ n"); richtextbox1.scrolltocaret ();} // This is the call method in a non-creation thread, in fact, the textbox invoke method is used. Public void threadmethodtxt (int n) {This. begininvoke (updatetxt, "thread-started execution, executed" + N + "Times, executed once every second"); For (INT I = 0; I <n; I ++) {This. begininvoke (updatetxt, I. tostring (); // executes the thread once every second. sleep (1000);} This. begininvoke (updatetxt, "end of thread");} // enable the thread private void button#click (Object sender, eventargs E) {thread objthread = new thread (New threadstart (delegate {threadmethodtxt (convert. toint32 (textbox1.text. trim ();}); objthread. start ();} private void form1_load_1 (Object sender, eventargs e) {// instantiate the delegate updatetxt = new updatetxt (updatetxtmethod );}}}
The above is all the code for your reference.
The first step is to define a delegate updatetxt.
// Create a delegate to access the Textbox Control. Public Delegate void updatetxt (string MSG); // defines a delegate variable public updatetxt;
It mainly uses a delegate to update the richtextbox1 interface.
The instance method is as follows:
Private void form1_load_1 (Object sender, eventargs e) {// instantiate the delegate updatetxt = new updatetxt (updatetxtmethod );}
The updatetxtmethod method is as follows:
// Modify the textbox value. Public void updatetxtmethod (string MSG) {richtextbox1.appendtext (MSG + "\ r \ n"); richtextbox1.scrolltocaret ();}
Next we will define a loop to output a value, and call this delegate to update richtextbox1
// This is the call method in a non-creation thread. It actually uses the textbox invoke method. Public void threadmethodtxt (int n) {This. begininvoke (updatetxt, "thread-started execution, executed" + N + "Times, executed once every second"); For (INT I = 0; I <n; I ++) {This. begininvoke (updatetxt, I. tostring (); // executes the thread once every second. sleep (1000);} This. begininvoke (updatetxt, "end of thread ");}
Then a subthread is used to call it.
// Enable the thread private void button#click (Object sender, eventargs e) {thread objthread = new thread (New threadstart (delegate {threadmethodtxt (convert. toint32 (textbox1.text. trim ();}); objthread. start ();}
Okay, that's basically enough.
That's where the problem is.
This. begininvoke (updatetxt, "end of thread ");
You may have discovered that I wrote it like this, not
Updatetxt ("End of thread ");
In this way, it is used directly in the Child thread,
I believe that many of my comrades write this statement. In fact, the error is true.
If you directly use
Updatetxt ("End of thread ");
You should understand it after thinking about it,
UpdatetxtIt is created in the main thread, and we use it directly in the sub-thread. If there is more data to run, it will become stuck. This is the reason why the interface information is blocked,
So evenDelegateIt cannot be used directly in the Child thread. Instead, you must use the begininvoke method to call this delegate.
In this way, there will be no stuck images.
The problem is solved.
Support
Below is my source code for everyone to download it http://files.cnblogs.com/sufei/WindowsFormsApplication3%281%29.zip