C # Multithreading Concurrency Precision Control Example

Source: Internet
Author: User

Online looking for a long time can not find a better example, so I wrote a!

Main demo multi-threading, delegate, callback
Core code:

Public partial class Frmstart:form {#pragma warning disable 0618 public delegate void Addculumcallbac        K (int index,int i);        Public Addculumcallback Addculumcallback;         ManualResetEvent resumeevent = new ManualResetEvent (false);            Public Frmstart () {InitializeComponent ();        Addculumcallback = new Addculumcallback (addculum); } public void Addculum (int index,int i) {This.lvshow.items[index]. SUBITEMS[1].        Text = "Multithreaded concurrent Run Test:" +i.tostring (); private void Btninitil_click (object sender, EventArgs e) {for (int i = 0; i < 3 0;                i++) {ListViewItem LVI = new ListViewItem (""); Lvi.                SubItems.Add (i.ToString ());            THIS.LVSHOW.ITEMS.ADD (LVI);            }} private string GetWebRequest (string url) {uri uri = new uri (URL);   WebRequest myreq = WebRequest.Create (URI);         WebResponse result = Myreq.getresponse (); Stream Recevicestream = result.            GetResponseStream ();            StreamReader Readerofstream = new StreamReader (Recevicestream, System.Text.Encoding.GetEncoding ("Utf-8"));            String strhtml = Readerofstream.readtoend ();            Readerofstream.close ();            Recevicestream.close (); Result.            Close ();        return strhtml;        } private void Btnstart_load (object sender, EventArgs e) {} list<thread> Listthread;            private void Button1_Click (object sender, EventArgs e) {listthread = new list<thread> ();                for (int i = 0; i < i++) {Object index = i;                Thread myThread = new Thread (Method);                Mythread.start (i);            Listthread.add (MyThread); }} public void Method (object index) {String a = GetWebRequest ("HTTp://www.baidu.com "); for (int i = 0; i < 10000; i++) {this.                BeginInvoke (Addculumcallback, index,i);            Thread.Sleep (100); }} private void Btnpausethread_click (object sender, EventArgs e) {foreach (T Hread item in Listthread) {item.            Suspend (); }} private void Btngoahead_click (object sender, EventArgs e) {foreach (Thread item in Li Stthread) {Item.                            Resume (); }} private void btnClose_Click (object sender, EventArgs e) {foreach (Thread item in list Thread) {Item.                            Abort ();                    } listthread.clear ();  }    }

  

C # Multithreading Concurrency Precision Control 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.