Step by step -65-thread small example

Source: Internet
Author: User
Tags file copy

1 draw Machine

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_01_ lottery Machine { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Checkforillegalcrossthreadcalls=false; }        Private voidButton1_Click (Objectsender, EventArgs e) {             while(true) {Random R=NewRandom (); Label1. Text= R.next (0,Ten).                ToString (); Label2. Text= R.next (0,Ten).                ToString (); Label3. Text= R.next (0,Ten).                                ToString (); Thread.Sleep ( +); }        }        Private voidButton2_Click (Objectsender, EventArgs e) {Thread th=NewThread (() ={Random R=NewRandom ();  while(true) {Label1. Text= R.next (0,Ten).                    ToString (); Label2. Text= R.next (0,Ten).                    ToString (); Label3. Text= R.next (0,Ten).                    ToString (); Thread.Sleep ( +);            }            }); Th. IsBackground=true; Th.        Start (); }    }}
View Code

2 Copy files

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_02_ file Copy { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            //01 Creating ThreadsThread th =NewThread (() =            {                //perform file read and write operations//01-01 Read and read                using(FileStream Fsreader =NewFileStream ("2015-04-14 Foundation Enhancement 1.rar", FileMode.Open)) {                    //01-02 Write and write                    using(FileStream fswrite =NewFileStream ("A.rar", FileMode.Create)) {                        LongCount =fsreader.length; LongCurrentcount =0; //set the length of each read                        byte[] bs =New byte[1024x768*1024x768]; intLen;  while(len = Fsreader.read (BS,0Bs. Length)) >0) {Currentcount+=Len; Progressbar1.invoke (Newaction<int> ((XH) = {progressBar1.Value = XH;}), (int) (currentcount/count) * -                                ); Fswrite.write (BS,0, Len);            }                    }                }            }); //02 Set as Background threadTh. IsBackground =true; //03 Startth.        Start (); }    }}
View Code

3

Step by step -65-thread small example

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.