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 Run in the main thread but I "fire and forget" this 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 join prevents the application and all those BeginInvoke

such BeginInvoke () actually works Is:

  1. This message sends some WM_USER (or likes) to the loop.
  2. The main thread pops this message into Application.DoEvents () (or as always is called application.Run () ) < / Li>
  3. Represents the main thread sent to BeginInvoke ()
  4. Endive () Waiting for such a signal (or if IAsyncResult << code> IAsyncResult in WaitHandle

then again: To fully execute the event or type something like this: / code> to 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 ();  

ADDON: Use of Eihter invocation () (synchronize) and with the above loop application.DoEvents () <

ADDON2:

    
  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

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -