Example of multi-thread callback value passing | C # example of multi-thread callback value passing

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Threading;
Using system;
Using system. Threading;
Namespace dataimportfromaccess
{

// Declare a callback function. Note that the passed parameters must be of the same type as the function parameters in the example class.
Public Delegate void examplecallback (INT linecount, label lb );
Public class form1 {
Public form1 ()
{
Initializecomponent ();
}

Public void currentnumber (INT tempcurrent, label lb)
{
LB. Text = tempcurrent. tostring ();
}
Private void button#click (Object sender, eventargs E)
{
Threadwithdata TWD = new threadwithdata (1,100, this. label1, new examplecallback (currentnumber ));
Thread TD = new thread (New threadstart (TWD. runmethod ));
TD. Start ();
}
Private void button2_click (Object sender, eventargs E)
{
Threadwithdata TWD = new threadwithdata (2,200, this. label2, new examplecallback (currentnumber ));
Thread TD = new thread (New threadstart (TWD. runmethod ));
TD. Start ();
}
}
Public class threadwithdata
{
Private int start = 0;
Private int end = 0;
Private examplecallback callback;
Private Label Lb;

Public threadwithdata (INT start, int end, label LB, examplecallback callback)
{
This. Start = start;
This. End = end;
This. Callback = callback;
This. lB = LB;
}
Public void runmethod ()
{
For (INT I = start; I <end; I ++)
{
Thread. Sleep (1000 );
If (callback! = NULL)
Callback (I, LB );
}

}
}
}

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.