2017-5-4 Threads

Source: Internet
Author: User

Thread:
Help the boss work, do not interfere with the boss's normal job

If the execution of a piece of code takes time, a new thread must be opened to execute,
If you do not thread, the window will appear suspended animation

Thread:

Thread th = new thread (delegate);
Th. IsBackground = true; Setting up a background thread
Th. Start ();

Threads are not allowed to access objects across threads by default
Use Control.checkforillegalcrossthreadcalls = False in the constructor; You can allow access to objects across threads.
To close a thread:

Th. Abort ();

A thread can execute a method of at most one parameter, which must be of type Object

Use a thread to do a three-level connection to a small program:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceThread Contact __ Applet ___2017_5_4{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Control.checkforillegalcrossthreadcalls=false; } Thread th=NULL; Thread Th1=NULL; Thread Th2=NULL; Private voidButton1_Click (Objectsender, EventArgs e) {th=NewThread (test1); Th. IsBackground=true; Th.            Start (); Th1=NewThread (test2); Th1. IsBackground=true; Th1.            Start (); Th2=NewThread (TEST3); Th2. IsBackground=true; Th2.         Start (); }              Private voidButton2_Click (Objectsender, EventArgs e) {th.            Abort (); Th1.            Abort (); Th2.        Abort (); }         Public voidtest1 () {Random R=NewRandom (); string[] name =New string[] {"Zheng","Xu Qiang","son June","Zi Xuan","Luo Wei" };  while(true)            {                intA = R.next (1, name.                Length); TextBox1.Text=Name[a]; Thread.Sleep ( -); }        }         Public voidtest2 () {Random R=NewRandom (); string[] City =New string[] {"Boxing","Zhangdian","Maqiao","Jinan","Qingdao" };  while(true)            {                intb = R.next (1, City.                Length); TextBox2.Text=City[b]; Thread.Sleep ( -); }        }           Public voidtest3 () {Random R=NewRandom (); string[] Events =New string[] {"Eat Hamburgers","Travel","Naughty","School","Write code","Play" };  while(true)            {                intc = R.next (1, events.                Length); TextBox3.Text=Events[c]; Thread.Sleep ( -); }                                               }      }}

2017-5-4 Threads

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.