C # multi-threaded control instance (simple routine, detailed comment)

Source: Internet
Author: User

 

View code

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
Using System. Threading;

NamespaceWindowsformsapplication2
{
//Define Delegation
Public Delegate VoidListboxdelegate ();

Public Partial ClassForm2: Form
{
PublicForm2 ()
{
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 ); // Directly perform the control operation, "associated with the main UI thread"
Listbox1.items. Add (commondata. Plus ()); // Directly perform the control operation, "associated with the main UI thread"
Listbox1.selecteditem = listbox1.items [listbox1.items. Count- 1 ];
}
Else // If you operate ListBox in another thread, enable delegation
Listbox1.invoke ( New Listboxdelegate (listshow ));
}

// Defines the operations on the main program-controlled parts of the UI, "associated with addauto ".
Private Void Listshow ()
{
// Listbox1.items. Add (commondata. Num );
Listbox1.items. Add (commondata. Plus ());
Listbox1.selecteditem = listbox1.items [listbox1.items. Count- 1 ];
}
// Define thread Functions
Private Void Addauto ()
{
While (Commondata. Flag = 0 )
{
// Commondata. Num ++;
Thread. Sleep ( 1000 );
ListBox (); // You cannot directly control the controls on the UI. Therefore, use this method to select the delegate.
}
}

///   <Summary>
/// Start multithreading In the Click Event
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private Void Btnstart_click_1 ( Object Sender, eventargs E)
{
Commondata. Flag = 0 ; // The thread flag is set to 0, indicating that the thread is enabled.
Listboxdelegate mycn = New Listboxdelegate (addauto ); // Defines the parameter of the threadstart delegate type and directs the delegate to the thread function.
Thread inserttxt = New Thread ( New Threadstart (mycn )); // Instantiation thread
Inserttxt. Start ();// Start thread
}

Private VoidBtnabort_click_1 (ObjectSender, eventargs E)
{
Commondata. Flag =1;
}

Private VoidBtnctrlmain_click_1 (ObjectSender, eventargs E)
{
ListBox ();
}

Private VoidBtnreset_click_1 (ObjectSender, eventargs E)
{
Commondata. num =0;
}

Private VoidBtnclear_click_1 (ObjectSender, eventargs E)
{
Listbox1.items. Clear ();
}

Private VoidBtnquit_click_1 (ObjectSender, eventargs E)
{
Application. Exit ();
}

}
}

 

 

View code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

NamespaceWindowsformsapplication2
{
ClassCommondata
{
Private Static Int_ Flag =0;
Private Static Int_ Num =0;

// Commondata. Num is only used to implement multi-thread control of the main program,
// If you can manually and automatically access global variables at the same time, the thread may not be synchronized.
// The following mainly uses the lock thread lock to modify the solution to synchronize threads:
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 ;}
}

}
}

 

 

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 to synchronize threads. Note:CodeDrive.

 

Transfer http://www.cnblogs.com/kingkoo/archive/2009/05/02/1447728.html

 

 

C # use proxy (delegate) in multithreading)

Http://blog.163.com/zhb123@126/blog/static/62515850201062714121186/

 

View code

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
Using System. Threading;

Namespace Windowsformsapplication2
{
Public Delegate Void Mydelegate1 ( String S ); // 1. Define Delegation

Public Partial Class Testthread: Form
{
Public Testthread ()
{
Initializecomponent ();
}
// 2. Create a method for modifying the progress bar:
Private Void Setprogressbar1 ( String S)
{
Progressbar1.value = 0 ;
Progressbar1.maximum = 2 ;
This . Progressbar1.value = This . Progressbar1.value + 1 ;
MessageBox. Show ( " Setprogressbar1 " + S );
}
// 3. Create a New thread and point to the "trun1" Method
Private Void Trun1 ()
{
// Determines whether the control belongs to this thread.

// Determine whether the control is in this thread
If (!This . Progressbar1.invokerequired)
{
Thread. Sleep ( 2000 );
}
Else
{
Mydelegate1 md1 =New Mydelegate1 (setprogressbar1 ); // Does not belong to this thread. Create a proxy and direct the proxy to the "setprogressbar1" method.
Thread. Sleep ( 2000 ); // Pause for 2 seconds
// Invoke (md1 ); // To execute the "setprogressbar1" method. (The "setprogressbar1" method is executed in the address space of the main thread)
Invoke (md1, " Trun1 " );
}
}
// 4. enable the new thread call:
Private Void Button#click ( Object Sender, eventargs E)
{
Thread thread = New Thread ( New Threadstart (trun1 ));
Thread. Start ();
MessageBox. Show ( " Multithreading " );
}
}
}

 

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.