C # try to write a multi-threaded question (delegate, invoke (), begininvoke ())

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. diagnostics;

Namespace _
{
Public partial class form1: Form
{
Public Delegate void del (string Str );
Public Delegate void del1 (string str1 );
Thread thread1, thread2;
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
This. thread1 = new thread (New threadstart (this. SA ));
This. thread1.start ();
// MessageBox. Show (this ,);
This. button1.enabled = false;
// Obtain the current thread.
Int thread1id = thread. currentthread. gethashcode ();
String strthreadid = convert. tostring (thread1id );
MessageBox. Show (this, strthreadid );
}

Private void progressset (string text)
{
Progressbar1.value = 0;
Progressbar1.maximum = 999999;
For (INT I = 0; I <999999; I ++)
{
Progressbar1.value ++;
}
Progressbar1.value = 0;
}

private void SA ()
{< br> Del pro = new del (progressset);
This. invoke (Pro, new object [] {"hello"});
}

private void button2_click (Object sender, eventargs e)
{< br> This. thread2 = new thread (this. SA1);
thread2.start ();
This. button2.enabled = false;
// get the current thread
int thread2id = thread. currentthread. gethashcode ();
string strthreadid = convert. tostring (thread2id);
MessageBox. show (this, strthreadid);
}

private void SA1 ()
{< br> // Other Operations
// For example, set the textbox content on the Interface
del1 Mi1 = new del1 (updatetextbox );
This. invoke (Mi1, new object [] {"I Am a text box"});
}

// Method for updating the interface
private void updatetextbox (string Str)
{< br> // update
This. textbox1.text = STR;
}< BR >}

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.