Winform cross-thread GUI and winform thread gui
I have been engaged in Web development for a long time. Although I am confused about everything, I am not very familiar with Winform and WPF, And I am rarely engaged in multi-thread development.
Sometimes there is nothing to do in the company, but IT cannot freely browse the news or anything on the internet, IT will be monitored, IT is always bad to see more. QQ is also disabled. The majority of users use OC. OC chat records are also uploaded to the server. So when I was idle, I decided to create a Winform chat room. The basic functions include logon, group chat, private chat, and online list viewing.
I encountered a problem today and checked it online. Although the answer is correct, most of the questions that I was confused about were copied and pasted, and even Chinese programming appeared... So record it yourself.
Background: The Chat Room includes the server and client. When the server clicks the "Start Service" button, a thread is enabled to listen on the client Socket connecting to the server. The online list needs to be updated after the client connects. Because the User Access Server --> saves the User object to the online collection Dictionary <User, Socket> --> Update the online list in one go, they are all written in the thread that listens to the Socket, however, when I directly update the online list at the beginning, a prompt is displayed that cross-thread operations are not allowed.
The solution is as follows:
Declare a delegate and a delegate variable. Assign the method for updating the online list to the delegate variable before entering the thread.
After entering the thread, query the control's InvokeRequired attribute. If it is True, call the Invoke method to activate the delegate method.
Public partial class MainForm: Form
{
......
Private Dictionary <string, Socket> _ clientList;
......
Public delegate void UpdateOnlineList ();
UpdateOnlineList updateOnlineList;
Private void btn_StartService_Click (object sender, EventArgs e)
{
UpdateOnlineList = new UpdateOnlineList (onlinelistchange );
......
Var listenThread = new Thread (StartListen );
ListenThread. IsBackground = true;
ListenThread. Start ();
......
}
Public void OnOnlineListChange ()
{
ListView_OnlineList.Items.Clear ();
Foreach (var item in _ clientList)
{
ListView_OnlineList.Items.Add (item. Key );
}
}
Public void StartListen ()
{
Try
{
While (_ isListening)
{
...
If (len> 0)
{
...
If (listView_OnlineList.InvokeRequired)
ListView_OnlineList.Invoke (updateOnlineList );
...
}
}
}
}
}
C # winform cross-thread control
Public delegate void MyInvoke (bool checked );
Private void button_Click (object sender, EventArgs e)
{
Thread t = new Thread (new ThreadStart (fun ));
T. Start ();
}
Private void fun ()
{
SetChecked (false );
}
Private void SetChecked (bool checked)
{
If (checkbox. InvokeRequired)
{
MyInvoke _ myInvoke = new MyInvoke (SetChecked );
This. Invoke (_ myInvoke, new object [] {checked });
}
Else
{
This. checkbox. Checked = checked;
}
}
How does WinForm quickly access interface controls across threads?
Proxy and event mechanisms