c# - How to wait for thread to finish with .NET? -


I have never threaded in C #, where I need two threads, as well as main UI threads. Basically, I have the following.

  Public Zero StartAction () {// Starting Thread .... Thread T1 = New Thread (New Thread Start (Action 1)); T1.Start (); // Now, I want to wait for `t1` to finish the main thread (which is calling the 'start' action 'method). I have made an event for `action1` for this // I want to start with` t2` ... thread t2 = new thread (new threadstart (action 2)); T2.Start (); }  

So, essentially, I have to wait for a thread to end someone else. What is the best way to do this?

I can see 5 options available:

1. Thread Add

with Mitch's Answer But it will block your UI thread, however you have a timeout built for you.


2 Rhusherpal

Use manual rsset a hacked as jrista suggests Have given.

One thing to note is that if you want to wait for multiple threads, then WaitHandle.WaitAll () will not work by default, because it is called MTA Thread is required You can mark your main () method with MTAThread - though it blocks your message pumps and is not recommended for what I've read Is performed.


3 An Event

Look at the events and multi-threading, it is possible that any event if and EventName (It can be unassigned between EventArgs.Empty) - This has happened to me before.

(hopefully this collection, I have not tried)

  Public category Form 1: form {int _count; Zero buttonclick (object sender, eventAgps E) {threadwalker worker = new threadworker (); Worker Threaddon + = Handlreaddone; Thread thread 1 = new thread (worker). Thread1.Start (); _count = 1; } Zero handlitterdone (object sender, eventArgse E) {// You should get the idea that this is just an example if (_count == 1) {threadwalker worker = new threadworker (); Worker Threaddon + = Handlreaddone; Thread thread 2 = new thread (worker). Thread2.Start (); _count ++; }} Class Threadworker {public event event hander threaded don; Run public Zero () {// Do one thing if (Threadden! = Zero) Thread Don (this, event.rg empty); }}}  

4 Use a representative

  public class form 1: form {int _count; Zero buttonclick (object sender, eventAgps E) {threadwalker worker = new threadworker (); Thread thread 1 = new thread (worker). Thread1.Start (HandleThreadDone); _count = 1; } Void HandleThreadDone () {// Like - just a simple example if (_count == 1) {threadwalker employee = new threadworker (); Thread thread 2 = new thread (worker). Thread2.Start (HandleThreadDone); _count ++; }} On the square trayworker {// on your favorite action & lt; T & gt; Switch or throw & lt; T & gt; Public Zero Run (Object State) {// A Action Action Full Action = (Action) State; CompleteAction.Invoke (); }}}  

If you use the _count method, it can be an idea (to be safe), so that increasing it

Interlocked.Increment (Riff _count)

I would be interested in knowing the differences in the use of events for delegates and thread notification, I know that the difference is only events are called synchronous

< Hr>

5


Delay / Events on the wrong thread

With this method, a very clear description of your options Is the debt. The program / delegate way of doing things is that your event handler is on method thread1 / thread2 not the main UI thread , so you have to switch back to the top HandleThreadDone methods:

  // Representative example (InvokeRequired) {Call (New Action (Handlreaddone)); Return; }  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -