C # Multithreading concurrency

Source: Internet
Author: User




PublicPartialClassMultithread:Form {Publicint threadh;Thread CodePublicString strURL;The URL of the receiving filePublicFileStream FS;PublicHttpWebRequest request;PublicByte[] nbytes;Receive buffersPublicint nreadsize;Number of bytes Receivedint conts;int conted;Public multithread () {The error thread is not called System.Windows.Forms.Control.checkforillegalcrossthreadcalls =False        InitializeComponent (); }ThreadStart startPrivatevoid Butquantity_click (Object sender,EventArgs e) {if (txtquantity. Text! ="") {conts =Convert.ToInt32 (txtquantity. Text);thread[] Threads =NewThread[conts];for (int i = 0; i < conts; i++) {Threads[i] =NewThread (NewThreadStart (readhttp)); Threads[i]. Name = i.tostring () +"Thread number"; }foreach (Thread TIn threads) {T.start ();TextBox2.Text = Thread.CurrentThread.Name;            } initializetimer (); }else {MessageBox.Show ("The number of requests cannot be empty!" ","ERROR"); }        }Privatevoid Timerclock_elapsed (Object Source,Elapsedeventargs e) {if (txtquantity. Text! ="") {conts =Convert.ToInt32 (txtquantity. Text);thread[] Threads =NewThread[conts];for (int i = 0; i < conts; i++) {Threads[i] =NewThread (NewThreadStart (readhttp)); Threads[i]. Name = i.tostring () +"Thread number"; }foreach (Thread TIn threads) {T.start (); }            }else {MessageBox.Show ("The number of requests cannot be empty!" ","ERROR"); }        }Send RequestPrivatevoid Readhttp () {try {conted =Convert.ToInt32 (txtquantity.                Text); System.Net.WebClient client =New System.Net.WebClient (); strURL = Txtaddress.text;This.textBox2.Text = "Requesting" + strURL;Stream st = client. OpenRead (strURL);StreamReader sr =NewStreamReader (ST);string res = Sr. ReadToEnd ();if (res! =NULL) {LISTBOX1.ITEMS.ADD ("-" +Thread.CurrentThread.Name +"Success"); }else {LISTBOX1.ITEMS.ADD ("-" +Thread.CurrentThread.Name +"Failure"); } Sr.                Close (); St. Close ();Thread.Sleep (2000); }catch (Exception e) {MessageBox.Show (E.message,"Hint:");Throw }        }Publicvoid Initializetimer () {system.timers.Timer timerclock = new system.timers.             Timer ();            timerclock.elapsed + = new elapsedeventhandler (timerclock_elapsed);            //timerclock.interval = 1000 * 60 * 3;            Timerclock.interval = 3000;         timerclock.enabled = true; }     }}

C # Multithreading concurrency

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.