Suspending a thread

U

Uzi

Hi all,

I have a thread which is running a long method in a third party
component (witch is a com object and accessed via interop).
I need to be able to suspend this thread.
the thread.Suspend() method is deprecated in .NET 2.0
the AutoResetEvent and similar are not good for me since my thread is
running an external method which I cannot modify to add such synch
events.
Is there any safe way to suspend such a thread?

Thanks,

Uzi
 
P

Peter Duniho

Hi all,

I have a thread which is running a long method in a third party
component (witch is a com object and accessed via interop).
I need to be able to suspend this thread.

Why?

"Suspend this thread" is a description of a particular implementation
you've chosen to solve some unnamed problem. What is that unnamed
problem? Why do you believe that suspending the thread is the appropriate
solution?
the thread.Suspend() method is deprecated in .NET 2.0
the AutoResetEvent and similar are not good for me since my thread is
running an external method which I cannot modify to add such synch
events.
Is there any safe way to suspend such a thread?

No. It's unfortunate that you don't have some existing mechanism provided
by the component you're using that would allow you to signal it to wait,
but absent that there's no way for you to safely suspend the thread.

You can, of course, still use the Thread.Suspend() method. But it's not
safe. It never was. It would be better to step back a bit and try to
find an alternative solution to whatever larger problem is at issue here.

Pete
 
W

Willy Denoyette [MVP]

Uzi said:
Hi all,

I have a thread which is running a long method in a third party
component (witch is a com object and accessed via interop).
I need to be able to suspend this thread.
the thread.Suspend() method is deprecated in .NET 2.0
the AutoResetEvent and similar are not good for me since my thread is
running an external method which I cannot modify to add such synch
events.
Is there any safe way to suspend such a thread?

Thanks,

Uzi


Adding to what Peter said, who say's that the third party component runs on
a managed thread, are you sure it doesn't create his own thread(s) or that
it doesn't runs on another thread like in case of a COM object that runs in
an incompatible apartment?

Willy.
 
U

Uzi

Adding to what Peter said, who say's that the third party component runs on
a managed thread, are you sure it doesn't create his own thread(s) or that
it doesn't runs on another thread like in case of a COM object that runs in
an incompatible apartment?

Willy.

OK, here's the problem:
I'm using microsoft's COM object for running VB scripts:
MSScriptControl.ScriptControlClass. I have a method in my managed
code, which is running in a dedicated worker thread which calls the
"Run()" method of this COM object. now, I want to give the user the
option of pausing the execution of the script and then, of course,
resuming it. this MSScriptControl.ScriptControlClass object doesn't
have a built in "pause" method. I thought I can externally pause the
script by pausing the worker thread running the wrapper method. Any
idea on who to acheive the result without using Thread.Suspend()?
Thanks!
 
W

Willy Denoyette [MVP]

Adding to what Peter said, who say's that the third party component runs
on
a managed thread, are you sure it doesn't create his own thread(s) or that
it doesn't runs on another thread like in case of a COM object that runs
in
an incompatible apartment?

Willy.

OK, here's the problem:
I'm using microsoft's COM object for running VB scripts:
MSScriptControl.ScriptControlClass. I have a method in my managed
code, which is running in a dedicated worker thread which calls the
"Run()" method of this COM object. now, I want to give the user the
option of pausing the execution of the script and then, of course,
resuming it. this MSScriptControl.ScriptControlClass object doesn't
have a built in "pause" method. I thought I can externally pause the
script by pausing the worker thread running the wrapper method. Any
idea on who to acheive the result without using Thread.Suspend()?
Thanks!


The script control is an ActiveX control, that means it should run in a AX
control host (a Windows Form for instance). Are you running this from a
Forms application, if YES, are you aware that the script runs on your UI
thread and NOT on the worker thread?
Suspending a thread that runs unmanaged code has no effect, only threads
running in the CLR (execute managed code) can be suspended.
That said, once the script engine starts running a procedure, there is no
way to suspend it's execution.

Willy.
 
P

Peter Duniho

[...]
I'm using microsoft's COM object for running VB scripts:
MSScriptControl.ScriptControlClass. I have a method in my managed
code, which is running in a dedicated worker thread which calls the
"Run()" method of this COM object. now, I want to give the user the
option of pausing the execution of the script and then, of course,
resuming it. this MSScriptControl.ScriptControlClass object doesn't
have a built in "pause" method. I thought I can externally pause the
script by pausing the worker thread running the wrapper method. Any
idea on who to acheive the result without using Thread.Suspend()?

IMHO, to do this correctly you will need to essentially write a VBScript
debugger. That is, some component that knows enough about VBScript to
control its execution statement by statement, and which has an opportunity
between each statement to interrupt execution of the script.

There are lots of things that can go wrong if you just suspend a thread
arbitrarily. I would _hope_ those risks may be reduced if all that thread
is doing is executing a VBScript (on the assumption that a VBScript is
limited in what it could actually be doing), but even there I think there
are potential problems.

And of course, Willy's caveat about an OS thread versus a managed thread
is appropriate as well: if you're some unmanaged component, the thread may
or may not be executing in a context where a managed Thread.Suspend() call
will work. At the very least, you may have to use p/invoke to get at the
real thread and suspend/resume it.

Of course, this begs the question: if you're not already writing a
VBScript debugger (and it sounds like you aren't), why does the user need
the ability to suspend and resume the operation of VBScript code?
Modification of the flow of any executable code is not something to be
taken lightly, IMHO. It's not the sort of thing that you just stick in a
"suspend" button and a "resume" button, hook it to a few lines of code,
and let the user have a party with. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top