Avatar of madmare
madmare
Flag 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
C#

Avatar of undefined
Last Comment
Miguel Oz

8/22/2022 - Mon
kaufmed

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.
madmare

ASKER
I'll fix my self

 in any way create new Thread :)

Thanks
Miguel Oz

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
madmare

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

Thanks
Miguel Oz

Check the example to run the method ProcessFile at:
http://www.dotnetperls.com/threadpool
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Miguel Oz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.