C # multithreaded Programming instance-code analysis of thread-to-form interaction

Source: Internet
Author: User
C # Multithreaded Programming instance threads interact with forms

Code:

    Public partial class Form1:form {//Declare thread array thread[] workthreads = new THREAD[10];        Public Form1 () {InitializeComponent ();        }//This delegate allows an asynchronous call to add Item delegate void Additemcallback (string text) to the listbox;        This method demonstrates how thread-safe mode is lowered with a control on a Windows Form. private void AddItem (string text) {if (this.listBox1.InvokeRequired) {Addite                Mcallback d = new Additemcallback (AddItem); This.            Invoke (d, new object[] {text});            } else {this.listBox1.Items.Add (text); }}//Data acquisition method public void Dataget () {while (true) {Addit                EM ("OK");            Thread.Sleep (1000);        }//If this method exits, then the thread exits}///<summary>///boot thread//</summary> <param name= "Sender" ></param>//<param Name= "E" ></param> private void Button1_Click (object sender, EventArgs e) {//loop Create and start thread                Line for (int i = 0; i < workthreads.length; i++) {if (workthreads[i] = = null)                    {//If the thread does not exist, create workthreads[i] = new Thread (new ThreadStart (Dataget)); Workthreads[i].                    Name = i.ToString (); Workthreads[i].                Start (); } else {//already exists, if not running, start if (Workthreads[i]. ThreadState = = Threadstate.aborted | | Workthreads[i]. ThreadState = = threadstate.stopped) {Workthreads[i] = new Thread (New Threadstar                        T (Dataget)); Workthreads[i].                        Name = i.ToString (); Workthreads[i].                    Start (); }else{Workthreads[i].                    Start ();      }                }      }}///<summary>//Stop thread///</summary>//<param name= "Sender" ></param>//<param name= "E" ></param> private void button2_click (object sender, Eventa                RGS e) {//Loop stop thread execution for (int i = 0; i < workthreads.length; i++) { If the thread exists and the state is not stopped or terminated, then the thread is terminated if (workthreads[i]! = null && workthreads[i]. ThreadState! = threadstate.stopped && workthreads[i]. ThreadState! = threadstate.aborted) {workthreads[i].                Abort (); }            }        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.