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