Program | multithreading
Using Control.Invoke to process multithreaded application interfaces
Using a single thread causes the interface to lose its response when a large amount of computation or time-consuming operation is performed. Control.Invoke provides a simple way to update the meeting in a worker thread. The function submits the callback function as a parameter to the queue of the application's interface process (typically the main thread), waiting to be processed. In this way, the operation of the interface does not need to worry about synchronization, interlock and other issues.
The following example is an excerpt from a local Area network resource search Program
As you can see, the worker thread in the thread pool updates the TreeView by calling the TreeView's Invoke method and providing a callback function.
In frameworks 1.1, some simple multithreaded update interfaces do not require this type of action, such as modifying text in static text boxes. But in Frameworks 2.0, all operations must be done in this way.
private void Searchnet ()
{
Clears the byte points under all root nodes
Treeview1.nodes[0]. Nodes.clear ();
for (UINT i = ipstart; I <= ipend; i++)
{
Converts an integer to an IP address, adding a task to the thread pool
ThreadPool.QueueUserWorkItem (New WaitCallback (Search),
("\\\\" + (I >> 24). ToString () + '. ' +
((int) I & 0x00ff0000) >> 16). ToString () + '. ' +
((int) I & 0x0000ff00u) >> 8). ToString () + '. ' + (i%256). ToString ()));
}
}
delegate void Updater (TreeNode Parent, TreeNode child);
public void Updatetreeview (TreeNode Parent, TreeNode)
{
PARENT.NODES.ADD (child);
Treeview1.nodes[0]. Expand ();
foreach (TreeNode n in Treeview1.nodes[0]. Nodes)
{
N.expand ();
}
}
private void Search (object Host)
{
Wksta_info_100 Stainfo;
Trying to connect
if (Connect (Host,out stainfo))
{
TreeNode nodecomputer = new TreeNode (stainfo.wki100_computername);
Search for shares
Searchserver (Nodecomputer,stainfo, (string) Host);
}
}
private void Searchserver (TreeNode nodecomputer,wksta_info_100 stainfo, String Host)
{
* * Other code * *
Treeview1.invoke (New Updater (Updatetreeview), new object[] {nodecomputer, node});
* * Other code * *
}
private void Searchnet ()
{
Clears the byte points under all root nodes
Treeview1.nodes[0]. Nodes.clear ();
for (UINT i = ipstart; I <= ipend; i++)
{
Converts an integer to an IP address, adding a task to the thread pool
ThreadPool.QueueUserWorkItem (New WaitCallback (Search),
("\\\\" + (I >> 24). ToString () + '. ' +
((int) I & 0x00ff0000) >> 16). ToString () + '. ' +
((int) I & 0x0000ff00u) >> 8). ToString () + '. ' + (i%256). ToString ()));
}
}
delegate void Updater (TreeNode Parent, TreeNode child);
public void Updatetreeview (TreeNode Parent, TreeNode)
{
PARENT.NODES.ADD (child);
Treeview1.nodes[0]. Expand ();
foreach (TreeNode n in Treeview1.nodes[0]. Nodes)
{
N.expand ();
}
}
private void Search (object Host)
{
Wksta_info_100 Stainfo;
Trying to connect
if (Connect (Host,out stainfo))
{
TreeNode nodecomputer = new TreeNode (stainfo.wki100_computername);
Search for shares
Searchserver (Nodecomputer,stainfo, (string) Host);
}
}
private void Searchserver (TreeNode nodecomputer,wksta_info_100 stainfo, String Host)
{
* * Other code * *
Treeview1.invoke (New Updater (Updatetreeview), new object[] {nodecomputer, node});
* * Other code * *
}
private void Searchnet ()
{
Clears the byte points under all root nodes
Treeview1.nodes[0]. Nodes.clear ();
for (UINT i = ipstart; I <= ipend; i++)
{
Converts an integer to an IP address, adding a task to the thread pool
ThreadPool.QueueUserWorkItem (New WaitCallback (Search),
("\\\\" + (I >> 24). ToString () + '. ' +
((int) I & 0x00ff0000) >> 16). ToString () + '. ' +
((int) I & 0x0000ff00u) >> 8). ToString () + '. ' + (i%256). ToString ()));
}
}
delegate void Updater (TreeNode Parent, TreeNode child);
public void Updatetreeview (TreeNode Parent, TreeNode)
{
PARENT.NODES.ADD (child);
Treeview1.nodes[0]. Expand ();
foreach (TreeNode n in Treeview1.nodes[0]. Nodes)
{
N.expand ();
}
}
private void Search (object Host)
{
Wksta_info_100 Stainfo;
Trying to connect
if (Connect (Host,out stainfo))
{
TreeNode nodecomputer = new TreeNode (stainfo.wki100_computername);
Search for shares
Searchserver (Nodecomputer,stainfo, (string) Host);
}
}
private void Searchserver (TreeNode nodecomputer,wksta_info_100 stainfo, String Host)
{
* * Other code * *
Treeview1.invoke (New Updater (Updatetreeview), new object[] {nodecomputer, node});
* * Other code * *
}
Summarized as follows:
1. Define the delegate
2. Define callback function
3, call Control.Invoke ()
Personal sense of grammar is more troublesome, especially for each interface modification must define a delegate (because the parameters are different), there is a way to improve it?