This article describes the method of implementing cross-thread operation controls in C# and is shared with you for your reference. The specific implementation method is as follows:
Since Winform and wpf prohibit directly accessing controls across threads under the .net platform, it adopts an asynchronous method when accessing controls across threads.
1. Cross-thread access controls in winform project:
Write a small Winform instance: In a winform project, sometimes in order to display the system running status to Form in real time, a RichTextbox control is added to display the system running log in real time. The operation implemented in this example is to write the log into the RichTextbox control as a string. Because it is displayed in real time, it involves cross-thread input.
public void setText(string txt)
{
if (this.t_revmsg.InvokeRequired)//Waiting for asynchronous
{
setRichTexBox fc = new setRichTexBox(Set);
(fc, new object []{ txt});
}
else
{
this.t_revmsg.AppendText(txt);
}
}
private void Set(string txt)
{
t_revmsg.AppendText(txt);
}
When the value of InvokeRequired of the control is true, it means that there is a thread that does not belong to the creation of it to access it. At this time, it needs to operate asynchronously.
2. Cross-thread access controls in the wpf project:
In the page page of wpf, add a button control and textbox control, and click button to add the content in the textbox control.
{
this.(new WriteDelegate(WriteMethod), "asdf");
}
private delegate void WriteDelegate(string str);
private void WriteMethod(string str)
{
this.(str);
}
3. Get the contents in the textbox control across threads in the wpf project
The function implemented in this example is: click the button control and enable the socket listening service. The listening port needs to be obtained from the textbox in the UI interface, because it is not the thread that created it accesses it, so there is an asynchronous operation. A little addition: In the process of starting monitoring, there is a while (true) dead loop. Putting it directly into the main thread will cause the thread to not proceed normally. The solution is to open a new thread to start listening.
{
Thread thread = new Thread(new ThreadStart(StartServer));
();
}
public void StartServer()
{
int port = Convert.ToInt32(GetText());
ServerSocket serverSocket = new ServerSocket(port);
();
}
private delegate string GetTextHandle(Control control);
private string GetText(Control control)
{
if ( != )
{
return (string)(new GetTextHandle(), control);
}
else
{
if (() == typeof(TextBox))
{
return ((TextBox)control).Text;
}
else
{
return ;
}
}
}
Summarize:
From the above examples, we can see that cross-thread calls require the use of Invoke and BeginInvoke (not used for the time being). In some systems with not very strict requirements, we believe that the two are common. Interested friends can test it and run it. More you will gain through practice!
I hope this article will be helpful to everyone's C# programming.