i have one function which contains no. of tasks and no. of thread (function (var noOftasks,var noOfThread)); which will be called from outside or say from another app.
when this function calls i create noOfthread and assign task to each thread one by one, if all thread is busy then wait for any thread to be free and then again assign task( if any remaining) to it.
now i also want to set timeout that if any thread running out of time then it will be aborted
because main thread is working continously to check free thread and assign task, i can not rely on Thread.Join(timeout) and make thread aborted according to that.
how can i achieve same? below is code snippet
public class TaskA
{
public List<TaskProcess> ProcessTasks(List<TaskProcess> ListOfTaskProcess, int NoOfThreads)
{
try
{
Thread[] _threadPool = new Thread[NoOfThreads];
foreach (var _task in ListOfTaskProcess)
{
Thread _Thread = null;
while (true)
{
_Thread = GetFreeThread(_threadPool);
if (_Thread != null)
{
break;
}
else
{
Thread.Sleep(1000);
}
}
if (_Thread != null)
{
_Thread.Start(_task);
}
}
foreach (var _Thread in _threadPool)
{
if (_Thread.IsAlive)
{
_Thread.Join();
}
}
return ListOfTaskProcess;
}
catch (Exception)
{
return ListOfTaskProcess;
}
}
private Thread GetFreeThread(Thread[] p_ThreadPool)
{
try
{
foreach (var _thread in p_ThreadPool)
{
if (_thread == null || (_thread.IsAlive == false && _thread.ThreadState != ThreadState.Running))
{
return new Thread(doTask);
}
}
return null;
}
catch (Exception _Exception)
{
}
}
private void doTask(object _task)
{
try
{
_task.status = "In Progress";
/* do task process here*/
_task.status = "completed";
}
catch (Exception _Exception)
{
_task.status = "Failed";
}
}
public class TaskProcess
{
/* other properties */
public string status { get; set; }
}
}
If you want to run the multiple threads at the same time you may have two or more cpu. You may want to run the main thread all the time from what you have said. Or you may need to use the newThread.Join() method to block the main thread for some time.
If you want to run the multiple threads at the same time you may have two or more cpu
Not at all. Depends on whether you mean at the precise same instant, or more loosely. Multi-threading has been around in Windows and other OSes for a lot longer than multiple cores have been common.
vrparekh@gma...
Member
224 Points
353 Posts
multithreading with timeout
Jan 18, 2012 06:47 AM|LINK
Hello all,
my requirement is like below;
i have one function which contains no. of tasks and no. of thread (function (var noOftasks,var noOfThread)); which will be called from outside or say from another app.
when this function calls i create noOfthread and assign task to each thread one by one, if all thread is busy then wait for any thread to be free and then again assign task( if any remaining) to it.
now i also want to set timeout that if any thread running out of time then it will be aborted
because main thread is working continously to check free thread and assign task, i can not rely on Thread.Join(timeout) and make thread aborted according to that.
how can i achieve same? below is code snippet
public class TaskA { public List<TaskProcess> ProcessTasks(List<TaskProcess> ListOfTaskProcess, int NoOfThreads) { try { Thread[] _threadPool = new Thread[NoOfThreads]; foreach (var _task in ListOfTaskProcess) { Thread _Thread = null; while (true) { _Thread = GetFreeThread(_threadPool); if (_Thread != null) { break; } else { Thread.Sleep(1000); } } if (_Thread != null) { _Thread.Start(_task); } } foreach (var _Thread in _threadPool) { if (_Thread.IsAlive) { _Thread.Join(); } } return ListOfTaskProcess; } catch (Exception) { return ListOfTaskProcess; } } private Thread GetFreeThread(Thread[] p_ThreadPool) { try { foreach (var _thread in p_ThreadPool) { if (_thread == null || (_thread.IsAlive == false && _thread.ThreadState != ThreadState.Running)) { return new Thread(doTask); } } return null; } catch (Exception _Exception) { } } private void doTask(object _task) { try { _task.status = "In Progress"; /* do task process here*/ _task.status = "completed"; } catch (Exception _Exception) { _task.status = "Failed"; } } public class TaskProcess { /* other properties */ public string status { get; set; } } }Multithread timeout thread
Qi Wu - MSFT
Contributor
5794 Points
677 Posts
Re: multithreading with timeout
Jan 20, 2012 05:34 AM|LINK
Hi,
You can refer to the answer in the below link.
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3165ae6a-661e-4285-996c-7999ca339bf2
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Richey
Contributor
3816 Points
431 Posts
Re: multithreading with timeout
Jan 20, 2012 06:03 AM|LINK
If you want to run the multiple threads at the same time you may have two or more cpu. You may want to run the main thread all the time from what you have said. Or you may need to use the newThread.Join() method to block the main thread for some time.
DMW
All-Star
15943 Points
2353 Posts
Re: multithreading with timeout
Jan 31, 2012 03:56 PM|LINK
Not at all. Depends on whether you mean at the precise same instant, or more loosely. Multi-threading has been around in Windows and other OSes for a lot longer than multiple cores have been common.
Dave