This article describes the C # multi-threaded and cross-threading approach to interface controls. Share to everyone for your reference. The specific analysis is as follows:
When writing WinForm access to WebService, it is often encountered because of network latency caused by the interface card dead. Enabling new threads to access webservice is a viable approach.
Typically, there is an example of starting a new thread below:
The code is as follows:
private void Loadremoteappversion ()
{
if (FileName.Text.Trim () = = "") return;
Statuslabel.text = "Loading";
S_controllers_bins.s_controllers_binssoapclient service = new S_controllers_bins.s_controllers_binssoapclient ();
S_controllers_bins.controllers_bins m = service. Queryfilename (FileName.Text.Trim ());
if (M! = null)
{
Todo:
Statuslabel.text = "Load succeeded";
}else
Statuslabel.text = "Failed to load";
}
private void Btnloadbininformation (object sender, EventArgs e)
{
Thread nonparameterthread = new Thread (new ThreadStart (loadremoteappversion));
Nonparameterthread.start ();
}
When running the program, if you want to manipulate the interface control in the thread, you may be prompted not to access the interface control across threads, there are two ways to handle it:
1. Start the program to change:
The code is as follows:
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.enablevisualstyles ();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
Application.setcompatibletextrenderingdefault (FALSE);
Application.Run (New Form1 ());
}
2. Using delegates
The code is as follows:
public delegate void Loadremoteappversiondelegate (); Defining delegate variables
private void Btnloadbininformation (object sender, EventArgs e)
{
loadremoteappversiondelegate func = new Loadremoteappversiondelegate (loadremoteappversion);//<span style= " Font-family:arial, Helvetica, Sans-serif; >loadremoteappversion no changes </span>
Func. BeginInvoke (null, NULL);
}
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Multi-threaded and cross-threaded access to interface controls
This address: http://www.paobuke.com/develop/c-develop/pbk23124.html
Related Content C # implement method to get MP3 tag information WinForm call Baidu Map Interface Usage Example C # program Get entity Class Property name and Value Method Example C # learning note-Basic syntax
WinForm Show pictures in DataGridView How to insert a picture in C # using C # to implement a web crawler C # Method of implementing multithreading between threads based on delegates
C # Multi-threaded and cross-threaded access to interface controls