C # Multi-threaded and cross-threaded access to interface controls

Source: Internet
Author: User

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

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.