C # multi-threaded control instance

Source: Internet
Author: User

The function of this instance is "multi-thread control UI control", and the thread function is automatically added with 1. The interface is as follows:

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;

Namespace treadTest
{
// Define the delegate
Public delegate void ListBoxDelegate ();
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
// Delegate processing method (associated with listboxdelegate)
Private void ListBox ()
{
If (! Listbox1.invokerequired) // if you operate ListBox In the UI main thread,
{
Listbox1.items. Add (++ commondata. Num); // you can directly perform the control operation and associate it with the main UI thread"
Listbox1.selecteditem = listbox1.items [listbox1.items. Count-1];
}
Else // If The ListBox is operated in another thread, enable the delegate
ListBox1.Invoke (new ListBoxDelegate (listShow ));
}

// Define the operations on the main program-controlled parts of the UI, "associated with AddAuto ".
Private void listShow ()
{
ListBox1.Items. Add (CommonData. num );
ListBox1.SelectedItem = listBox1.Items [listBox1.Items. Count-1];
}
// Define the thread function
Private void AddAuto ()
{
While (commondata. Flag = 0)
{
Commondata. Num ++;
Thread. Sleep (1000 );
ListBox (); // controls on the UI cannot be directly controlled, so use this method to select the delegate
}
}
// Start multithreading In the Click Event
Private void btnstart_click (Object sender, eventargs E)
{
// Set the thread flag to 0, indicating that the thread is enabled.
CommonData. Flag = 0;
// Define the parameter of the ThreadStart delegate type and direct the delegate to the thread function
ListBoxDelegate mycn = new ListBoxDelegate (AddAuto );
// Instantiate the thread
Thread insertTxt = new Thread (new ThreadStart (mycn ));
// Start the thread
InsertTxt. Start ();
}

Private void btnabort_click (Object sender, eventargs E)
{
Commondata. Flag = 1;
}
Private void btnctrlmain_click (Object sender, eventargs E)
{
ListBox ();
}
Private void btnreset_click (Object sender, eventargs E)
{
CommonData. num = 0;
}
Private void btnClear_Click (object sender, EventArgs e)
{
ListBox1.Items. Clear ();
}
Private void btnQuit_Click (object sender, EventArgs e)
{
Application. Exit ();
}

}

// Global variable solution
Public class CommonData
{
Private static int _ Flag = 0;
Private static int _ num = 0;
Public static int Flag
{
Get {return _ Flag ;}
Set {_ Flag = value ;}
}
Public static int num
{
Get {return _ num ;}
Set {_ num = value ;}
}
}
}

 

Summary:

To use a multi-threaded UI control, you must use a delegate. Call the control's Invoke method (the Invoke method parameter is a delegate type parameter ).

Steps:

1. Declare delegation.

2. Declare the delegate processing function (determine whether the main thread controls the UI control or the Invoke (multi-thread) controls the UI control ).

3. Declare a thread instance and pass the thread function delegate to ThreadStart ().

4. Enable this thread.

5. Define the thread function. To control the UI control, call the delegate processing function in step 1 and use Invoke for his own judgment.

6. Define the functions to be called by Invoke (such as the listShow function in this example)

//************************************** **************************************** **************************************** ***********

In the above example, only the function of controlling the program-controlled component of the main thread with multiple threads is completed. If global variables can be accessed manually and automatically at the same time, thread synchronization may occur. The following describes how to use the lock thread lock to modify the solution so that the thread can be synchronized.

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;

Namespace treadtest
{
// Define the delegate
Public Delegate void listboxdelegate ();
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
// Delegate processing method (associated with listboxdelegate)
Private void ListBox ()
{
If (! Listbox1.invokerequired) // if you operate ListBox In the UI main thread,
{
Listbox1.items. Add (commondata. Plus (); // directly performs control operations, "associated with the main UI thread"
ListBox1.SelectedItem = listBox1.Items [listBox1.Items. Count-1];
}
Else // If The ListBox is operated in another thread, enable the delegate
ListBox1.Invoke (new ListBoxDelegate (listShow ));
}

// Define the operations on the main program-controlled parts of the UI, "associated with AddAuto ".
Private void listShow ()
{
Listbox1.items. Add (commondata. Plus ());
Listbox1.selecteditem = listbox1.items [listbox1.items. Count-1];
}
// Define the thread function
Private void addauto ()
{
While (commondata. Flag = 0)
{
Thread. Sleep (1000 );
ListBox (); // controls on the UI cannot be directly controlled, so use this method to select the delegate
}
}
// Start multithreading In the Click Event
Private void btnstart_click (Object sender, eventargs E)
{
// Set the thread flag to 0, indicating that the thread is enabled.
Commondata. Flag = 0;
// Define the parameter of the threadstart delegate type and direct the delegate to the thread function
ListBoxDelegate mycn = new ListBoxDelegate (AddAuto );
// Instantiate the thread
Thread insertTxt = new Thread (new ThreadStart (mycn ));
// Start the thread
InsertTxt. Start ();
}

Private void btnAbort_Click (object sender, EventArgs e)
{
CommonData. Flag = 1;
}
Private void btnCtrlMain_Click (object sender, EventArgs e)
{
ListBox ();
}
Private void btnReset_Click (object sender, EventArgs e)
{
CommonData. num = 0;
}
Private void btnClear_Click (object sender, EventArgs e)
{
ListBox1.Items. Clear ();
}
Private void btnquit_click (Object sender, eventargs E)
{
Application. Exit ();
}
}

// Global variable solution
Public class commondata
{
Private Static int _ flag = 0;
Private Static int _ num = 0;
Public static int plus ()
{
Lock (new object ())
{
Return _ num ++;
}
}
Public static int Flag
{
Get {return _ Flag ;}
Set {_ Flag = value ;}
}
Public static int num
{
Get {return _ num ;}
Set {_ num = value ;}
}
}
}

 

 

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.