Using Control.Invoke to process multithreaded application interfaces

Source: Internet
Author: User
Tags foreach define integer connect thread tostring
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?



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.