c# - How to ensure that async method finished work? -
I am very new to threads, so my thoughts and questions can be a bit silly :)
I fill out the WinForm
control with data from another thread, so I have to call the applicant ()
when I'm trying to use the control.
If I correctly understand, treeView.BeginInvoke (/ * some action () * /)
this takes action
BeginInvoke ()
, so I do not know when the work has actually been done. And even when the worker thread stops and returns the execution on the main thread, then I can not be sure that all BeginInvoke ()
methods have completed the execution.
This is the reason why after returning to main, I can not manage the thread with the control
on which I BeginInvoke ()
.
The real problem is TreeView.ExpandAll ()
does not work.
Take a look at the code snippet below.
Private zeros btnGetTree_Click (Object Sender, EventArgs e) {var treeViewWriter = New thread ((=); UpdateTreeview (New AddXmlNodeArgs (di, null), tree directory contents); TreeViewWriter.Start (); TreeViewWriter.Join (); TreeDirectoryContents.ExpandAll (); } / / Method A worker thread runs a public static update UpdateTreeView (AddXmlNodeArgs args, TreeView treeView) {// I will remember the details ... here is the code that I run for every new TreeNode: treeView.UpdateTree (TreeView tree) Is = & gt; {tree.nodes [0] .Nodes.Add (newTreeNode); // treeView.Nodes [0] ...}); } // TreeView public static extension method for zero UpdateTree (this TreeView tree, Action & amp;; TreeView & gt; code) {if (tree.InvokeRequired) tree.BeginInvoke (code, tree); Other code. Invoke (tree); }
I do not call anywhere tree.BeginInvoke ()
but I do not call endInvoke ()
. So I think that when btnGetTree_Click
execution reaches treeDirectoryContents.ExpandAll ()
- not all inserts ()
Have been . Expand all () does not work
.
Correct me if I am wrong, and please give me an advice to solve this problem.
This is absolutely wrong:
treeViewWriter.start (); TreeViewWriter.Join ();
Never call a thread. Join the main thread! Because it such then again: To fully execute the event or type something like this: / code> to ADDON: Use of Eihter ADDON2: join
prevents the application and all those BeginInvoke
BeginInvoke ()
actually works Is:
Application.DoEvents ()
(or as always is called application.Run ()
) < / Li> BeginInvoke ()
Endive ()
Waiting for such a signal (or if IAsyncResult << code> IAsyncResult
in WaitHandle
from BeginInvoke
Minister is not stored, it is garbage collected)
to private bool Ne = false; Zero clicks (object, eventargs) {thread. Start (); While (! Done) application Duants (); Tree.ExpandAll ();
invocation ()
(synchronize) and with the above loop application.DoEvents () <
personalized; Zero click (object, event.rg) {done = false; // init state new thread (function). Start (); // While starting the work (! Done) application while working. DoEvents (); // Wait for completion (); } // The main thread ends in the zero job () {Thread.Sleep (100); // did your work = true; } // indicated zero over () {whatever (); } // The main thread is called zero click 2 (Object, EventArgues) {New Thread (Task 2). Start (); } // just start zero work2 () {thread. Sleep (100); // Your Work BeginInvoke (New Action (Finish)); } // Perform on main thread ()
Comments
Post a Comment