A serious Threading Problem

  • Thread starter Thread starter Dinesh Jain
  • Start date Start date
D

Dinesh Jain

Hi All,
First of all let me make the problem generic!
I have three function A(),B() & C().
From main function function I call them in sequence like this -
public function MyMain()
A()
B()
C()
End Function

B() shuold be executed after A() & C() should be executed after B().
But my problem is is to put only B() in a subthread.
I want B() to be put in a subthread because I create UI in A() and
want it to be resposive for user events when B() is being executed.
How can we tell C() not te execute unless B() finishes?

Please help,
Thanks in advance,

-Regards,
Dinesh
 
If you put func C in a thread, check for a condition or a shared variable
The value of the shared variable will be true if B has finished

Inside C, you can use a while loop

while true
if sharedvar = true
do my task...

loop

Guys, correct me if I am wrong
But this looks like a easy way out

Kalpesh
 
Hi Kalpesh,
Thnax for reply.
Putting a while loop inside C() will cuase the application
hang till B() finishes which will cause the while loop to be stopped.
Moreover, putting while loop will consume most of the CPU useage.
I think I will have to deal with thead-communiactions,
Any more ideas?
Thnaks in advance,

-Regards,
Dinesh
 
why not create a seperate function d() which is runs in a subthread which
calls c() and then d()??

Regards
Simon
 
Hi Simon,
Thanks for reply.
Actually I can't put function C() into the subthread
created by B() because I create(new()) some controls inside
C(), those will be used by the Main thread.
I want to put the function C() with Main thread.

Thanks in advance,

-Regards,
Dinesh
 
Dinesh Jain said:
Hi All,
First of all let me make the problem generic!
I have three function A(),B() & C().
From main function function I call them in sequence like this -
public function MyMain()
A()
B()
C()
End Function

B() shuold be executed after A() & C() should be executed after
B(). But my problem is is to put only B() in a subthread.
I want B() to be put in a subthread because I create UI in A()
and
want it to be resposive for user events when B() is being executed.

How can we tell C() not te execute unless B() finishes?

When A finishes, create the new thread. In the thread, run B and C in
succession.
 
Hi Armin,
please read this I am reposting it-

Thanks for reply.
Actually I can't put function C() into the subthread
created by B() because I create(new()) some controls inside
C(), those will be used by the Main thread.
I want to put the function C() with Main thread.

Thanks in advance,

-Regards,
Dinesh
 
Dinesh Jain said:
Hi Armin,
please read this I am reposting it-

Thanks for reply.
Actually I can't put function C() into the subthread
created by B() because I create(new()) some controls inside
C(), those will be used by the Main thread.
I want to put the function C() with Main thread.

I don't know your object model, but I'd raise an event when B finishes,
catch the Event, and in the event handler, call the Invoke/BeginInvoke
method of a control in the main thread. The destination method can call
function C.
 
Dinesh, why is creating a new control inside a separate thread a problem?

If you do want to wait for function B() to finish then use the Join method
from inside your main thread just before you call function C(). The Join
method will holdup your main thread until the thread running function B() is
finished.

Regards
Fred
 
Thanks Armin & fred,
I will try BeginInvoke solution suggested by Armin
& get back soon.

-Regards,
Dinesh
 
Use it asyncronously. Don't call C from within myMain. Instead create a
callback function from B that will execute C when the thread is finished.

-CJ
 
Back
Top