C # inter-thread communication

Source: Internet
Author: User

Using System;
Using System.Text;
Using System.Windows.Forms;
Using System.Threading;

namespace inter-thread communication
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
1. Create the Invoke function, roughly as follows:
<summary>
Delegate function to being invoked by main thread
</summary>
private void Invokefun ()
{
if (Prgbar.value < 100)
{
Prgbar.value = prgbar.value + 1;
Button1. Text = PrgBar.Value.ToString ();
}
if (Prgbar.value = = 100)
{
MessageBox.Show ("Done", this.) Text);
Prgbar.value = 0;
}
}

2. Child Thread Entry function:
<summary>
Thread function Interface
</summary>
private void Threadfun ()
{
Create invoke method by specific function
methodinvoker mi = new MethodInvoker (this. Invokefun);
for (int i = 0; i <; i++)
{
This . BeginInvoke (MI);Allow the main thread to access the controls you have created.
Thread.Sleep (100);//Perform time-consuming operations on new threads.
}
}

3. Begin from here
private void Button1_Click (object sender, EventArgs e)
{
thread thdprocess = new Thread (new ThreadStart (Threadfun));
Thdprocess.start ();
}
}
}

Without processing, if a child thread accesses a control created by the main thread, the system will give an error, telling us that the thread cannot be called directly. Because different threads are running in parallel without interference in different memory spaces. So what do you do to get access to the controls you want to access in a child thread?

In fact, from the above example, it can be seen that the implementation of inter-thread communication is not complex. Thdprocess.start () Later, a new thread is started, starting with the entry function Threadfun (). Here's the point:

The code uses the MethodInvoker delegate, which is described in MSDN: the delegate can use any method that is declared void in managed code and does not accept any parameters, and is used when invoking the control's Invoke method or when a simple delegate is required and does not want to be defined by itself. Here it actually represents the Invokefun () method.
Another important method: BeginInvoke (Delegate), which represents the execution of the specified delegate asynchronously on the thread that creates the control's underlying handle. It can invoke the delegate asynchronously and this method returns immediately. You can call this method from any thread, even the thread that owns the control handle. If the control handle does not already exist, this method searches along the control's parent chain until it finds a control or form that has a window handle. This is the asynchronous call that completes the child thread's access to the corresponding control on the main thread.

Looking back, isn't it easy? Three steps: Creating and starting a thread----asynchronous call with the specified delegate method.

C # inter-thread communication

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.