<p>Conceptually, I would like to accomplish the following but have had trouble understand how to code it properly in C#:</p>

<pre><code>
SomeMethod { // Member of AClass{}
    DoSomething;
    Start WorkerMethod() from BClass in another thread;
    DoSomethingElse;
}
</code></pre>

<p>Then, when WorkerMethod() is complete, run this:</p>

<p><pre><code>
void SomeOtherMethod()  // Also member of AClass{}
{ ... }
</pre></code></p>

<p>Can anyone please give an example of that? </p>

[{'answer_id': 74917, 'author': 'MagicKat', 'author_id': 8505, 'author_profile': 'https://Stackoverflow.com/users/8505', 'pm_score': 1, 'selected': False, 'text': '<p>Check out BackgroundWorker.</p>\n'}, {'answer_id': 74948, 'author': 'Isak Savo', 'author_id': 8521, 'author_profile': 'https://Stackoverflow.com/users/8521', 'pm_score': 5, 'selected': True, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="noreferrer">BackgroundWorker</a> class was added to .NET 2.0 for this exact purpose.</p>\n\n<p>In a nutshell you do:</p>\n\n<pre><code>BackgroundWorker worker = new BackgroundWorker();\nworker.DoWork += delegate { myBClass.DoHardWork(); }\nworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SomeOtherMethod);\nworker.RunWorkerAsync();\n</code></pre>\n\n<p>You can also add fancy stuff like cancellation and progress reporting if you want :)</p>\n'}, {'answer_id': 75050, 'author': 'Randolpho', 'author_id': 12716, 'author_profile': 'https://Stackoverflow.com/users/12716', 'pm_score': 0, 'selected': False, 'text': "<p>Ok, I'm unsure of how you want to go about this. From your example, it looks like WorkerMethod does not create its own thread to execute under, but you want to call that method on another thread. </p>\n\n<p>In that case, create a short worker method that calls WorkerMethod then calls SomeOtherMethod, and queue that method up on another thread. Then when WorkerMethod completes, SomeOtherMethod is called. For example:</p>\n\n<pre><code>public class AClass\n{\n    public void SomeMethod()\n    {\n        DoSomething();\n\n        ThreadPool.QueueUserWorkItem(delegate(object state)\n        {\n            BClass.WorkerMethod();\n            SomeOtherMethod();\n        });\n\n        DoSomethingElse();\n    }\n\n    private void SomeOtherMethod()\n    {\n        // handle the fact that WorkerMethod has completed. \n        // Note that this is called on the Worker Thread, not\n        // the main thread.\n    }\n}\n</code></pre>\n"}, {'answer_id': 75164, 'author': 'Vivek', 'author_id': 7418, 'author_profile': 'https://Stackoverflow.com/users/7418', 'pm_score': 1, 'selected': False, 'text': '<p>Use Async Delegates:</p>\n\n<pre><code>// Method that does the real work\npublic int SomeMethod(int someInput)\n{\nThread.Sleep(20);\nConsole.WriteLine(”Processed input : {0}”,someInput);\nreturn someInput+1;\n} \n\n\n// Method that will be called after work is complete\npublic void EndSomeOtherMethod(IAsyncResult result)\n{\nSomeMethodDelegate myDelegate = result.AsyncState as SomeMethodDelegate;\n// obtain the result\nint resultVal = myDelegate.EndInvoke(result);\nConsole.WriteLine(”Returned output : {0}”,resultVal);\n}\n\n// Define a delegate\ndelegate int SomeMethodDelegate(int someInput);\nSomeMethodDelegate someMethodDelegate = SomeMethod;\n\n// Call the method that does the real work\n// Give the method name that must be called once the work is completed.\nsomeMethodDelegate.BeginInvoke(10, // Input parameter to SomeMethod()\nEndSomeOtherMethod, // Callback Method\nsomeMethodDelegate); // AsyncState\n</code></pre>\n'}, {'answer_id': 75513, 'author': 'ashwnacharya', 'author_id': 1909, 'author_profile': 'https://Stackoverflow.com/users/1909', 'pm_score': 2, 'selected': False, 'text': '<p>You have to use AsyncCallBacks. You can use AsyncCallBacks to specify a delegate to a method, and then specify CallBack Methods that get called once the execution of the target method completes.</p>\n\n<p>Here is a small Example, run and see it for yourself.</p>\n\n<p>class Program\n    {</p>\n\n<pre><code>    public delegate void AsyncMethodCaller();\n\n\n    public static void WorkerMethod()\n    {\n        Console.WriteLine("I am the first method that is called.");\n        Thread.Sleep(5000);\n        Console.WriteLine("Exiting from WorkerMethod.");\n    }\n\n    public static void SomeOtherMethod(IAsyncResult result)\n    {\n        Console.WriteLine("I am called after the Worker Method completes.");\n    }\n\n\n\n    static void Main(string[] args)\n    {\n        AsyncMethodCaller asyncCaller = new AsyncMethodCaller(WorkerMethod);\n        AsyncCallback callBack = new AsyncCallback(SomeOtherMethod);\n        IAsyncResult result = asyncCaller.BeginInvoke(callBack, null);\n        Console.WriteLine("Worker method has been called.");\n        Console.WriteLine("Waiting for all invocations to complete.");\n        Console.Read();\n\n    }\n}\n</code></pre>\n'}, {'answer_id': 75743, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': "<p>In .Net 2 the BackgroundWorker was introduced, this makes running async operations really easy:</p>\n\n<pre><code>BackgroundWorker bw = new BackgroundWorker { WorkerReportsProgress = true };\n\nbw.DoWork += (sender, e) =&gt; \n   {\n       //what happens here must not touch the form\n       //as it's in a different thread\n   };\n\nbw.ProgressChanged += ( sender, e ) =&gt;\n   {\n       //update progress bars here\n   };\n\nbw.RunWorkerCompleted += (sender, e) =&gt; \n   {\n       //now you're back in the UI thread you can update the form\n       //remember to dispose of bw now\n   };\n\nworker.RunWorkerAsync();\n</code></pre>\n\n<p>In .Net 1 you have to use threads.</p>\n"}, {'answer_id': 77113, 'author': 'Romain Verdier', 'author_id': 4687, 'author_profile': 'https://Stackoverflow.com/users/4687', 'pm_score': 2, 'selected': False, 'text': '<p>Although there are several possibilities here, I would use a delegate, asynchronously called using <code>BeginInvoke</code> method.</p>\n\n<p><strong>Warning</strong> : don\'t forget to always call <code>EndInvoke</code> on the <code>IAsyncResult</code> to avoid eventual memory leaks, as described in <a href="http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html?page=2" rel="nofollow noreferrer">this article</a>.</p>\n'}]
['https://Stackoverflow.com/questions/74880', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10505/']