[2014-9-15] Asynchronous Delegate thread advanced

Source: Internet
Author: User

Yesterday tragedy, help others tune code, Leng didn't tune out. There is no time to write the blog. Worry Mulberry ...

Yesterday's Today, do not learn today, on the day of the class,

1 First, the data between the Windows is passed. That is, the parent window and the child window data are passed.

Implement with a delegate

①, define the delegate under the same named control.

②, creates a delegate instance in a child window.

③, creates a child window object in the parent window, passing the delegate method.

public delegate void SetString (String str);
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
This. Text = Thread.CurrentThread.ManagedThreadId.ToString ();
Allow other threads to access
Control.checkforillegalcrossthreadcalls = false;
}

private void Btnmain_click (object sender, EventArgs e)
{
Fchild fchild = new Fchild ();
Fchild.setstr = new SetString (set);
Fchild.show ();
}
private void Set (String str)

{

THIS.TXTMAIN.TEXT=STR;
}
}

child window:

Public Fchild ()
{
InitializeComponent ();
This. Text = Thread.CurrentThread.ManagedThreadId.ToString ();
}
Public SetString Setstr;

private void Btnchild_click (object sender, EventArgs e)
{
Setstr (This.txtChild.Text);
}

Implemented in Multithreading:

Routing between threads cannot be passed between different windows, that is, allowing other threads to access controls created by the current thread

Control.checkforillegalcrossthreadcalls = false;//This method is not recommended

Using the Invoke () method

public delegate void Settextdel (string txt);

public partial class Mainfrm:form
{
Private Settextdel Mysetcotroltxt4otherthreaddel;

Public MainFrm ()
{
InitializeComponent ();

This. Text = Thread.CurrentThread.ManagedThreadId + "";

Allow other threads to access controls created by the current thread: control
Deceiving
Control.checkforillegalcrossthreadcalls = false;

Mysetcotroltxt4otherthreaddel = new Settextdel (this. Settext4otherthread);
}

private void Button1_Click (object sender, EventArgs e)
{
Childfrm frm = new childfrm ();
Frm._settextdel = new Settextdel (SetText);
frm. Show ();

Thread thread = new Thread (() =
{
Childfrm frm = new childfrm ();
Frm._settextdel = new Settextdel (SetText);
frm. ShowDialog ();
});
Thread. Start ();
}

public void SetText (string txt)
{
InvokeRequired when the thread executes to this, verify which thread of the Txtmessage control was created. True if the Invokerequired:fasle is created by itself
if (this.txtMessage.InvokeRequired)
{
This. Invoke (Mysetcotroltxt4otherthreaddel, TXT);
}
Else
{
This.txtMessage.Text = txt;
}
}

public void Settext4otherthread (string strtxt)
{
This.txtMessage.Text = Strtxt;
}
}

[2014-9-15] Asynchronous Delegate thread advanced

Related Article

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.