Determining when all the threads are finished is simple. For (int I = 0; I "); Can you elaborate on your other requirements?
I tryed this but blocking the progress. – Rapunzo Aug 27 '10 at 12:32 @Rapunzo - indeed, that's the point. Thread.
Join is a blocking call - hence Console. WriteLine won't execute until all threads are finished. You can run this piece of code on another thread, with appropriate synchronisation on whichever notification mechanism you choose.
I used Console. WriteLine here for simplicity. – Winston Smith Aug 27 '10 at 14:10.
You can check the ThreadState property of the Thread. Might be better to use async methods. This gives you a WaitHandle object, and you can use WaitHandle.
WaitAll to wait for all of your async methods to finish. Here's an intro to asynchronous programming: msdn.microsoft.com/en-us/library/aa71959....
– Rapunzo Aug 27 '10 at 12:33 You call your method using BeginInvoke() instead of calling it directly – Jerome Aug 27 '10 at 13:00.
You definitely want to use the Task class for this or a higher-level concept like Parallel.ForEach. Using the Thread class directly is quite painful. I recently wrote a blog post comparing various asynchronous approaches, listed in order from best (Task) to worst (Thread).
Here's an example using Task, demonstrating what you wanted to do: // Start all tasks var threads = new TaskthreadCount; for (int I = 0; I x. Status == TaskStatus. Running); // Register a callback when they have all completed (this does not block) Task.Factory.
ContinueWhenAll(threads, MyCallback).
I try to add using System.Threading. Tasks; to my project but get an error: The type or namespace name 'Tasks' does not exist in the namespace 'System. Threading' (are you missing an assembly reference?) – Rapunzo Aug 27 '10 at 14:05 To use the Task class, you'll need either .
NET 4.0 or the Rx library for . NET 3.5.– Stephen Cleary Aug 27 '10 at 15:10.
Add a delegate to Searcher and pass it a callback method from your main thread that each thread will call when it finishes. As you launch each thread, add it to a Dictionary keyed by the thread's ManagedThreadId. When each thread finishes, the callback removes the thread from the Dictionary and checks to see if the count is zero.
Dictionary activeThreads = new Dictionary(); for (int I = 0; I Name = string. Format(i.ToString()); } foreach (Thread t in threads) { lock (activeThreads) { activeThreads. Add(t.
ManagedThreadId, t); } t.Start(); } } public void ThreadDone(int threadIdArg) { lock (activeThreads) { activeThreads. Remove(threadIdArg); if (activeThreads. Count == 0) { // all done } } } public delegate void SearcherDoneDelegate(int threadIdArg); public static object locker = new object(); public class Searcher { public SearcherDoneDelegate Done { get; set; } public void getIpRange() { Done(Thread.CurrentThread.
ManagedThreadId); } } If you have more threads than you want to run at one time, put them into a Queue and peel them off as older threads finish (use the callback).
Thanks for solution but (activeThreads. Count == 0) doesn't work healty, many times it fails! Here is my code if ((activeThreads.
Count) == 0) { j++; progressBar1. Value = 0; btnSearch. Enabled = true; } in this case progressbar1.
Value getting 0 and many times growing up after gettin 0 again. And I cant be sure from threads are done – Rapunzo Sep 3 '10 at 15:02 @Rapunzo - what's happening is that you're starting each thread as soon as it's created, and the initial threads are finishing before the latter threads have started. The quick way to get around this is to move the t.Start() to a second loop so you create all of them before starting any of them.
A more elegant approach is to put the created threads in a Queue and read them using a producer-consumer pattern. This also lets you control the number of concurrent threads. – ebpower Sep 3 '10 at 17:35.
First, I have to point out that creating 100, 150, 255, etc. Threads is probably not a good idea. You might be better off using the ThreadPool or Task class (if using . NET 4.0).
Aside from that there are two well established methods for waiting until all threads complete. Join the thread.Thread. Join blocks until the target thread finishes.
For (int I = 0; I { try { src.getIpRange(); } finally { finished.Signal(); } } threadsi. Name = string. Format(i.ToString()); } foreach (Thread t in threads) { t.Start(); } finished.Signal(); finished.WaitOne().
Is countdownevent defined in . Net 3.5? – Rapunzo Aug 27 '10 at 14:22 No, I updated my answer to include a link with a simple implementation.
– Brian Gideon Aug 27 '10 at 14:35.
Thread function can modify this variable (having entered critical section, of course).
And how can I use – Rapunzo Aug 27 '10 at 14:44 Here are some examples and definition: en.wikipedia. Org/wiki/Critical_section – FractalizeR Aug 28 '10 at 13:32.
Determining when all the threads are finished is simple.
You can check the ThreadState property of the Thread.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.