Solved

Run Multiple Threads in Parallel

Posted on 2013-01-03
7
376 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 75

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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