Switching between two threads
-
hi all, How to switch between two threads ? I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process. Please help me..
-
hi all, How to switch between two threads ? I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process. Please help me..
praveen pandey wrote:
I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.
You have the threads signal each other when control should pass from one to the other. While one thread is doing work, the other waits. When the work is finished, the working thread signals the waiting thread that it's done. It then begins waiting while the other thread does its work. Typically, a worker thread looks something like this:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);// Do some work... } }
}
From another thread, you have to pulse the lock to get the worker thread running:
lock(lockObject)
{
Monitor.Pulse(lockObject);
}However, since one of these threads is the main thread, you probably don't want it blocking while the other thread is doing work. This could tie up your UI. So we need more information about the kind of application you're writing. Is it a console app? A Window Forms app?
-
praveen pandey wrote:
I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.
You have the threads signal each other when control should pass from one to the other. While one thread is doing work, the other waits. When the work is finished, the working thread signals the waiting thread that it's done. It then begins waiting while the other thread does its work. Typically, a worker thread looks something like this:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);// Do some work... } }
}
From another thread, you have to pulse the lock to get the worker thread running:
lock(lockObject)
{
Monitor.Pulse(lockObject);
}However, since one of these threads is the main thread, you probably don't want it blocking while the other thread is doing work. This could tie up your UI. So we need more information about the kind of application you're writing. Is it a console app? A Window Forms app?
It's a windows app. Here is scenario. secondThreadMethod() { while(conditonIsTrue) { some code; //After one iteration i want to come out(since conditon is still true) and goto main thread finish some work and come back to this thread and continue in this way. } } thanks
-
It's a windows app. Here is scenario. secondThreadMethod() { while(conditonIsTrue) { some code; //After one iteration i want to come out(since conditon is still true) and goto main thread finish some work and come back to this thread and continue in this way. } } thanks
praveen pandey wrote:
It's a windows app.
Ok, well in your thread loop, you could marshal a delegate invocation to the main thread:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);// Do some work... BeginInvoke(new WorkDelegate(DoWorkOnMainThread)); } }
}
private delegate void WorkDelegate();
private void DoWorkOnMainThread()
{
lock(lockObject)
{
// Do some work on main thread.Monitor.Pulse(lockObject); }
}
This assumes you're writing a Forms application. The idea is that you have the main thread running and not blocking while the worker thread does its work. Then when you need work done on the main thread, the worker thread marshals a delegate invocation that does the work to the main thread through
BeginInvoke
. The method on the main thread signals the worker thread when it's done so that the worker thread can continue for another iteration. -
praveen pandey wrote:
It's a windows app.
Ok, well in your thread loop, you could marshal a delegate invocation to the main thread:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);// Do some work... BeginInvoke(new WorkDelegate(DoWorkOnMainThread)); } }
}
private delegate void WorkDelegate();
private void DoWorkOnMainThread()
{
lock(lockObject)
{
// Do some work on main thread.Monitor.Pulse(lockObject); }
}
This assumes you're writing a Forms application. The idea is that you have the main thread running and not blocking while the worker thread does its work. Then when you need work done on the main thread, the worker thread marshals a delegate invocation that does the work to the main thread through
BeginInvoke
. The method on the main thread signals the worker thread when it's done so that the worker thread can continue for another iteration.Thanks alot. It really worked in the way what i wanted. Thanks Again...