Solved

Run Multiple Threads in Parallel

Posted on 2013-01-03
7
375 Views
Last Modified: 2013-01-08
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
Comment
Question by:madmare
  • 3
  • 3
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38739768
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
 

Author Comment

by:madmare
ID: 38739775
I'll fix my self

 in any way create new Thread :)

Thanks
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38739802
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:madmare
ID: 38739815
can you please give me the example for ThreadPool.QueueUserWorkItem ?

Thanks
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38739825
Check the example to run the method ProcessFile at:
http://www.dotnetperls.com/threadpool
0
 

Author Comment

by:madmare
ID: 38739995
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
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 38750199
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now