An example Analysis of the Control Operation problems of C # subthreads
This article mainly introduces "the example analysis of the control operation problem of C# subthread". In the daily operation, I believe that many people have doubts about the control operation problem of C# subthread. The editor consulted all kinds of data and sorted out the simple and useful operation method. I hope it will be helpful for you to answer the doubt of "example analysis of control operation problem of C# subthread". Next, please follow the editor to study!
Control operations related to C # child threads
Generally speaking, the operation of the control on the form directly in the child thread will cause an exception, because the child thread and the thread running the form are in different space, so if you want to operate the control on the form in the child thread, it is impossible to operate through the control object name, but it does not mean that the operation cannot be carried out. Microsoft provides the method of Invoke. Its function is to let the child thread tell the form thread to complete the corresponding control operation.
Now use a thread-controlled process bar to illustrate, the general steps are as follows:
1. Create the Invoke function, roughly as follows:
/ / /
< summary>/ Delegate function to be invoked by main thread /
< /summary>Private void InvokeFun () {if (prgBar.Value
< 100 ) prgBar.Value = prgBar.Value + 1; } 2. C#子线程入口函数: /// < summary>/ Thread function interface /
< /summary>Private void ThreadFun () {/ / Create invoke method by specific function MethodInvoker mi = new MethodInvoker (this.InvokeFun); for (int I = 0; I < 100; iTunes +) {this.BeginInvoke (mi); Thread.Sleep (100);}}
3. Create a C# child thread:
Thread thdProcess = new Thread (new ThreadStart (ThreadFun)); thdProcess.Start ()
Note:
Using System.Threading; private System.Windows.Forms.ProgressBar prgBar; at this point, on the "C # child thread control operation example analysis" study is over, I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!