I wonder if you have encountered thread-UI interaction in Winform code? Then, do you write the code in this way:
| The code is as follows: |
Copy code |
Public delegate void AddListViewCrossThreadDelegate (******); Public void AddListViewCrossThreads (ListViewItem lvi, int action) { If (lsvName. InvokeRequired) { AddListViewCrossThreadDelegate d = new AddListViewCrossThreadDelegate (AddListViewCrossThreads ); LsvName. Invoke (addlistviewdelegate, lvi, action ); } Else { // Add the code for the actual operation control } } |
However, if a page contains a large number of controls that involve UI interaction and these controls require many different parameters, we have to declare the delegate types with different parameters for these controls, then, use InvokeRequired to judge and finally write the code for century control controls. If so, the workload is really huge. In addition, this Copy/Paste job may drive you crazy, and the reusability is too bad. Is there any better way? Of course:
Through observation, it is found that every control needs to determine whether thread interaction (that is, whether InvokeRequired is required) during thread and UI interaction ), can this operation be integrated into a class? Check the code:
| The code is as follows: |
Copy code |
Using System. Windows. Forms; Namespace CommonUntil { Public static class UIThread { Public static void UIInvoke (this Control control, MethodInvoker invoker) { If (control. InvokeRequired) // if the interaction between the thread and the interface is generated { Control. Invoke (invoker); // use MethodInvoker to replace any Delegate method Return; } Else { Invoker. Invoke (); // triggers an interactive event } } } } |
So how should we use it?
Suppose we need to add a new ListViewItem object to the ListView control named lsvName, and we stipulate that the addFlag is used to add a list item, and the deleteFlag is used to clear all options. We will perform this operation:
| The code is as follows: |
Copy code |
Public void AddListView (ListViewItem lvi, int action) { If (addFlag = action) { This. lsvName. Items. Add (lvi ); } Else if (deleteFlag = action) { This. lsvName. Items. Clear (); } } |
What should we do when thread interaction occurs? We only need to get the ListViewItem object and action, and we can achieve it through our extension method:
| The code is as follows: |
Copy code |
For (int I = 0; I <myFiles. Count; I ++) { FileInfo file = myFiles [I]; ListViewItem lvi = new ListViewItem (); Lvi. Text = GetFileName. GetFileName (file. FullName ); Lvi. Tag = file. FullName; // store the fullname UIThread. UIInvoke (lsvName, Delegate { AddListView (lvi, addFlag ); } ); } |
Or simply write it like this:
| The code is as follows: |
Copy code |
UIThread. UIInvoke (lsvName, Delegate { // AddListView (lvi, action ); If (addFlag = action) { This. lsvName. Items. Add (lvi ); } Else if (deleteFlag = action) { This. lsvName. Items. Clear (); } } ); |
In this way, we don't have to assign values to different parameters each time, and then the judgment is being manipulated. The code is much more concise and hopefully useful to you.