Link to home
Start Free TrialLog in
Avatar of madmare
madmareFlag for Israel

asked on

Run Multiple Threads in Parallel

Hello Experts,

 I'm using the following code to run a Thread in parallel to the program:
            Thread myThread = new System.Threading.Thread(delegate()
            {
                //Your code here
                System.Threading.Thread.Sleep(14000);
                close_window_by_handle(ie);
            });
            myThread.Start();

Open in new window



In this code I need to wait 14 seconds ( according to the customer request )

the problem is when I run this code few times during 14 seconds it stuck

 How do I enhance my code check if a thread is running, if YES it create a new thread in addition to the running  one
else it use the Stopped thread

Thanks
Avatar of kaufmed
kaufmed
Flag of United States of America image

What do you mean by "else it use the Stopped thread?" A terminated thread cannot be restarted, and you must create a new Thread instance in that case.
Avatar of madmare

ASKER

I'll fix my self

 in any way create new Thread :)

Thanks
Is this a desktop app (eg. Winform) or Asp.net?
How many threads are you running in parallel in your App?

If this number is small (<20) then you can use ThreadPool.QueueUserWorkItem
else consider using TPL (.Net 4.0 up)

Note: Check TPL vs queueuserworkitem
Avatar of madmare

ASKER

can you please give me the example for ThreadPool.QueueUserWorkItem ?

Thanks
Check the example to run the method ProcessFile at:
http://www.dotnetperls.com/threadpool
Avatar of madmare

ASKER

Thanks mas

 I looked again to the request, I think that TPL can be helpful for  me

 can you please help to convert my code to TPL

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial