• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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
0
madmare
Asked:
madmare
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
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.
0
 
madmareAuthor Commented:
I'll fix my self

 in any way create new Thread :)

Thanks
0
 
Miguel OzSoftware EngineerCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Thanks
0
 
Miguel OzSoftware EngineerCommented:
Check the example to run the method ProcessFile at:
http://www.dotnetperls.com/threadpool
0
 
madmareAuthor Commented:
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
0
 
Miguel OzSoftware EngineerCommented:
using System.Threading.Tasks;

Open in new window


Your code should look like:
            Parallel.Invoke(
                () => {
                    //Your code here
                System.Threading.Thread.Sleep(14000);
                close_window_by_handle(ie);
                }
            );

Open in new window

Note: I use Parallel.Invoke because you are not returning  any value.
If you are returning  a value use:
Task<int> task1 = Task<int>.Factory.StartNew(() => DoWork());
// Get the results and write them out.
Console.WriteLine("Result ={0}", task1.Result);

Open in new window


where
static int DoWork()
{
     //Your code here
     System.Threading.Thread.Sleep(14000);
     close_window_by_handle(ie);
     return 0;
}

Open in new window

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now