rakeshwalia
rakeshwalia

Reputation: 3

facing issue using Multithreading in exe

I am facing issue in multi threading . Case : I am creating exe to download photos from some another website , As there are 1000's of photos coming from other server i have implemented multi-threading, but that is not working properly
In Main() , i have called a method named as ThreadMain(); and In ThreadMain(); function, we have divided task into ten threads like

 ThreadStart jobOne = new ThreadStart(ThreadOne);
            Thread threadOne = new Thread(jobOne);
            // Start the thread
            threadOne.Start();
ThreadStart jobTwo = new ThreadStart(ThreadTwo);
            Thread threadTwo = new Thread(jobTwo);
            threadTwo.Start();
 ThreadStart jobThree = new ThreadStart(ThreadThree);
            Thread threadThree = new Thread(jobThree);
            threadThree.Start();

etc upto 10 threads
Then further we have defined static method like

static void ThreadOne() { database tasks }

static void ThreadTwo() { database tasks }

static void ThreadThree() { database tasks }

Upto 10 jobs But After completing threads,console window does not close itself Or i am not able to know whether threads are completed or NOT ? Please advice

Upvotes: 0

Views: 97

Answers (1)

swordfish
swordfish

Reputation: 4995

use background workers.

They are a special kind of thread which runs in your program. You can use the "Progress" property of the background worker to report the progress to another method and in the method compute the necessary criteria and check if the threads are closed and finally close the program.

If you do not want to alter the structure of the program another method would be to have another thread called "watcherThread" (call it wat ever u want) and make this thread run continuosly in intervals of three or five seconds based upon your general execution time and have it check the isRunning property of all the other threads or threadState property of all the other threads and once you know all threads have completely run you can safely close your windows using "environment.exit(0);"

Some references

http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx http://www.dotnetperls.com/backgroundworker

http://midnightprogrammer.net/post/Using-Background-Worker-in-C.aspx http://csharptuning.blogspot.com/2007/05/background-worker.html

Upvotes: 1

Related Questions