471,602 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

Thread.Suspend and Thread.Resume in Framework 2.0

Hello,

I want to understand whats the best way to write code to replace
Thread.Suspend, Thread.Resume and Thread.Abort.

I have lots of code calling these existing methods and want to minimize the
risk of changing the code everywhere so here is what I think I could do,
which is to create my own ThreadWrapper class which inherits from Thread and
which has these three methods already defined but does it a different way
and then my original code can work as normal by just referencing my
ThreadWrapper class.

Example

public class ThreadWrapper : Thread
{
public void Abort()
{
// What do i do here
}
public void Suspend()
{
// What do i do here
}
public void Resume()
{
// What do i do here
}
}

My App
public class Test
{
static void Main(string[] args)
{
ThreadWrapper tw = new ThreadWrapper();
...
tw.Suspend();
tw.Resume();
tw.Abort();
}
}
Thanks,
Dec 14 '06 #1
6 10102
Why do you need to suspend and resume threads in your app? Could that be a
sign of poor engineering? Please explain.
While "suspending" a thread in the traditional manner has been deprecated by
MSFT, "pausing" a thread between atomic operations (under the developer's
control) is certainly a legitimate requirement :)
Dec 14 '06 #2
"Larry Smith" <no_spam@_nospam.comwrote
>Why do you need to suspend and resume threads in your app? Could that be
a
sign of poor engineering? Please explain.

While "suspending" a thread in the traditional manner has been deprecated
by MSFT, "pausing" a thread between atomic operations (under the
developer's control) is certainly a legitimate requirement :)
I don't think that's really legit - if I were to see code that was manually
freezing and thawing threads, I would kick it back to the developer and tell
him to re-write it. He would have to come back with a VERY strong reason why
he's doing it.

The standard thread synchronization mechanisms are pretty rich - monitor /
mutes / semaphore / rwlock / event / Interlocked. Other than debugging
scenarios, I don't see the need to externally start/stop threads very often.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Dec 14 '06 #3
Hi

I do not see any advantage of moving start, stop, suspend, resume a
thread to a separate class. They are in a single class already and are
static.... what else you need..

Thanks
-Srinivas.

Buddy Home wrote:
Hello,

I want to understand whats the best way to write code to replace
Thread.Suspend, Thread.Resume and Thread.Abort.

I have lots of code calling these existing methods and want to minimize the
risk of changing the code everywhere so here is what I think I could do,
which is to create my own ThreadWrapper class which inherits from Thread and
which has these three methods already defined but does it a different way
and then my original code can work as normal by just referencing my
ThreadWrapper class.

Example

public class ThreadWrapper : Thread
{
public void Abort()
{
// What do i do here
}
public void Suspend()
{
// What do i do here
}
public void Resume()
{
// What do i do here
}
}

My App
public class Test
{
static void Main(string[] args)
{
ThreadWrapper tw = new ThreadWrapper();
...
tw.Suspend();
tw.Resume();
tw.Abort();
}
}
Thanks,
Dec 14 '06 #4
I don't think that's really legit - if I were to see code that was
manually freezing and thawing threads, I would kick it back to the
developer and tell him to re-write it. He would have to come back with a
VERY strong reason why he's doing it.
I'm glad I don't work for you :)
The standard thread synchronization mechanisms are pretty rich - monitor /
mutes / semaphore / rwlock / event / Interlocked. Other than debugging
scenarios, I don't see the need to externally start/stop threads very
often.
Of course you apply proper synchronization techniques when required. And you
certainly don't pause a thread as a means of synchronization. You use one of
the techniques you cited. If a user clicks the "Cancel" button to abort a
background operation however (for example), and you then display a "confirm
cancellation" dialog for the user, normally you'll want to signal a "pause"
event so the background thread can temporarily suspend itself until the user
exits the dialog (the thread will respect the "pause" event by periodically
testing for it).
Dec 14 '06 #5
Let me get my point out.

I'm aware that it's bad design to suspend and pause the thread but I've got
a task to convert the code from Framework 1.0 to 2.0 and I was trying to get
rid of all the obsolete warning messages in the code. There are approx 200
warnings which call Suspend, Resume and Abort methods of the thread class. I
don't really have the time in my project plan to rewrite all this code so I
was looking for a generic approx which is why I showed the snippet code in
my original post.

Thanks,

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:EC**********************************@microsof t.com...
Why do you need to suspend and resume threads in your app? Could that be a
sign of poor engineering? Please explain.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Buddy Home" wrote:
>Hello,

I want to understand whats the best way to write code to replace
Thread.Suspend, Thread.Resume and Thread.Abort.

I have lots of code calling these existing methods and want to minimize
the
risk of changing the code everywhere so here is what I think I could do,
which is to create my own ThreadWrapper class which inherits from Thread
and
which has these three methods already defined but does it a different way
and then my original code can work as normal by just referencing my
ThreadWrapper class.

Example

public class ThreadWrapper : Thread
{
public void Abort()
{
// What do i do here
}
public void Suspend()
{
// What do i do here
}
public void Resume()
{
// What do i do here
}
}

My App
public class Test
{
static void Main(string[] args)
{
ThreadWrapper tw = new ThreadWrapper();
...
tw.Suspend();
tw.Resume();
tw.Abort();
}
}
Thanks,

Dec 14 '06 #6
Let me get my point out.

I'm aware that it's bad design to suspend and pause the thread but I've got
a task to convert the code from Framework 1.0 to 2.0 and I was trying to get
rid of all the obsolete warning messages in the code. There are approx 200
warnings which call Suspend, Resume and Abort methods of the thread class. I
don't really have the time in my project plan to rewrite all this code so I
was looking for a generic approx which is why I showed the snippet code in
my original post.

Thanks,

"Larry Smith" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>I don't think that's really legit - if I were to see code that was
manually freezing and thawing threads, I would kick it back to the
developer and tell him to re-write it. He would have to come back with a
VERY strong reason why he's doing it.

I'm glad I don't work for you :)
>The standard thread synchronization mechanisms are pretty rich - monitor
/ mutes / semaphore / rwlock / event / Interlocked. Other than debugging
scenarios, I don't see the need to externally start/stop threads very
often.

Of course you apply proper synchronization techniques when required. And
you certainly don't pause a thread as a means of synchronization. You use
one of the techniques you cited. If a user clicks the "Cancel" button to
abort a background operation however (for example), and you then display a
"confirm cancellation" dialog for the user, normally you'll want to signal
a "pause" event so the background thread can temporarily suspend itself
until the user exits the dialog (the thread will respect the "pause" event
by periodically testing for it).

Dec 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jorge | last post: by
2 posts views Thread by White Spirit | last post: by
4 posts views Thread by wanwan | last post: by
4 posts views Thread by fAnSKyer/C# newbie | last post: by
3 posts views Thread by =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post: by
1 post views Thread by Mike Fellows | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.