PC Review


Reply
Thread Tools Rate Thread

Best location to start a thread

 
 
Carl Heller
Guest
Posts: n/a
 
      2nd Jan 2007
If I'm creating a class to do some work that I want threaded out, where's
the best location to call ThreadStart? Or does it depend on the nature of
the work?

a. Call it outside the class, giving it the starting method of the class?
b. Have the class create the thread itself?

ie:
x = new WorkerClass();
ioThread = new Thread(new ThreadStart(x.StartWork));
ioThread.Start();

...

Class WorkerClass
{
...

public void StartWork()
{
SomeMethod();
}
}


or
x = new WorkerClass();
x.StartWork();

...

Class WorkerClass
{
private Thread WorkerThread;
...

public void StartWork()
{
WorkerThread = new Thread(new ThreadStart(this.SomeMethod));
WorkerThread.Start();
}
}


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
Guest
Posts: n/a
 
      2nd Jan 2007
I think its best to have it in the class. Its then encapsulated which is a
principle of OO. I also think its better to ThreadPool.QueueUserWorkItem
rather than make a new thread as there is overhead in thread creation.

--
Ciaran O'Donnell
http://wannabedeveloper.spaces.live.com


"Carl Heller" wrote:

> If I'm creating a class to do some work that I want threaded out, where's
> the best location to call ThreadStart? Or does it depend on the nature of
> the work?
>
> a. Call it outside the class, giving it the starting method of the class?
> b. Have the class create the thread itself?
>
> ie:
> x = new WorkerClass();
> ioThread = new Thread(new ThreadStart(x.StartWork));
> ioThread.Start();
>
> ...
>
> Class WorkerClass
> {
> ...
>
> public void StartWork()
> {
> SomeMethod();
> }
> }
>
>
> or
> x = new WorkerClass();
> x.StartWork();
>
> ...
>
> Class WorkerClass
> {
> private Thread WorkerThread;
> ...
>
> public void StartWork()
> {
> WorkerThread = new Thread(new ThreadStart(this.SomeMethod));
> WorkerThread.Start();
> }
> }
>
>
>

 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      2nd Jan 2007
I agree with Ciaran. It gives you the opportunity to do what I've done
in the following example, notably derive a class and override what the
worker does without affecting what the base class does.

#region Example 4
public class Threads4
{
static readonly object _countLock = new object();
private int _count = 0;
public Threads4()
{
}
public void ThreadBasics()
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
Thread myThread = new Thread(myThreadStart);
myThread.Start();
for(int i = 1 ; i<=5 ; i++)
{
Console.WriteLine("Main thread {0}",i);
Thread.Sleep(500);
}

}
protected virtual void ThreadMethod()
{
for(int i = 1 ; i<=5 ; i++)
{
Console.WriteLine("Child thread {0}",i);
Thread.Sleep(750);
}
}
}
public class Threads4b : Threads4
{
public Threads4b()
{
}
protected override void ThreadMethod()
{
for(int i = 1 ; i<=5 ; i++)
{
Console.WriteLine("Derived child thread {0}",i);
Thread.Sleep(750);
}

}
}
#endregion


Alternatively, If you want it to work on different external objects, I
personally would have each implement an interface, then the main object
would take that interface as a parameter and start worker method using
that parameter, ie:

public interface IThreadThis
{
void WriteSomething(int pCount);
}
public class Threads5
{
IThreadThis _threadThis;
public Threads5()
{
}
public void ThreadBasics(IThreadThis pThreadThis)
{
_threadThis = pThreadThis;
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
Thread myThread = new Thread(myThreadStart);
myThread.Start();
for(int i = 1 ; i<=5 ; i++)
{
Console.WriteLine("Main thread {0}",i);
Thread.Sleep(500);
}

}
protected virtual void ThreadMethod()
{
for(int i = 1 ; i<=5 ; i++)
{
_threadThis.WriteSomething(i);
Thread.Sleep(750);
}
}
}
public class Test1 : IThreadThis
{
public void WriteSomething(int pCount)
{
Console.WriteLine("Test1 {0}",pCount);
}
}
public class Tets2 : IThreadThis
{
public void WriteSomething(int pCount)
{
Console.WriteLine("Test2 {0}",pCount);
}
}

There are plenty of other ways of doing it, and you might find the
above unsuitable for your specific needs, so don't feel railroaded into
one man's idea of good practice


Ciaran O''''Donnell wrote:
> I think its best to have it in the class. Its then encapsulated which is a
> principle of OO. I also think its better to ThreadPool.QueueUserWorkItem
> rather than make a new thread as there is overhead in thread creation.
>
> --
> Ciaran O'Donnell
> http://wannabedeveloper.spaces.live.com
>
>
> "Carl Heller" wrote:
>
> > If I'm creating a class to do some work that I want threaded out, where's
> > the best location to call ThreadStart? Or does it depend on the nature of
> > the work?
> >
> > a. Call it outside the class, giving it the starting method of the class?
> > b. Have the class create the thread itself?
> >
> > ie:
> > x = new WorkerClass();
> > ioThread = new Thread(new ThreadStart(x.StartWork));
> > ioThread.Start();
> >
> > ...
> >
> > Class WorkerClass
> > {
> > ...
> >
> > public void StartWork()
> > {
> > SomeMethod();
> > }
> > }
> >
> >
> > or
> > x = new WorkerClass();
> > x.StartWork();
> >
> > ...
> >
> > Class WorkerClass
> > {
> > private Thread WorkerThread;
> > ...
> >
> > public void StartWork()
> > {
> > WorkerThread = new Thread(new ThreadStart(this.SomeMethod));
> > WorkerThread.Start();
> > }
> > }
> >
> >
> >


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      2nd Jan 2007
Hi

It all depends of what you are doing, the example you are providing is a
great way to pass parameter to the thread (you pass them in the
constructor).
Also helps you to prevent possible errors in concurrency.

Of course if the thread needs access to the creating instance ( if it's a
form and the thread needs to update the UI) then is better to define the
threaded method as an instance method of the calling class.


--
Ignacio Machin
machin AT laceupsolutions com



"Carl Heller" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If I'm creating a class to do some work that I want threaded out, where's
> the best location to call ThreadStart? Or does it depend on the nature of
> the work?
>
> a. Call it outside the class, giving it the starting method of the class?
> b. Have the class create the thread itself?
>
> ie:
> x = new WorkerClass();
> ioThread = new Thread(new ThreadStart(x.StartWork));
> ioThread.Start();
>
> ...
>
> Class WorkerClass
> {
> ...
>
> public void StartWork()
> {
> SomeMethod();
> }
> }
>
>
> or
> x = new WorkerClass();
> x.StartWork();
>
> ...
>
> Class WorkerClass
> {
> private Thread WorkerThread;
> ...
>
> public void StartWork()
> {
> WorkerThread = new Thread(new ThreadStart(this.SomeMethod));
> WorkerThread.Start();
> }
> }
>



 
Reply With Quote
 
Carl Heller
Guest
Posts: n/a
 
      2nd Jan 2007
Thank you for your suggestions. Reading this newsgroup always teaches me
something.

That interfaces idea wasn't something I previously would have even thought
of.

Carl.


 
Reply With Quote
 
Chris Mullins [MVP]
Guest
Posts: n/a
 
      3rd Jan 2007
"Carl Heller" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If I'm creating a class to do some work that I want threaded out, where's
> the best location to call ThreadStart? Or does it depend on the nature of
> the work?
>
> a. Call it outside the class, giving it the starting method of the class?
> b. Have the class create the thread itself?


My general rule of thumb is:
If I'm in a client application, I try to use threadpool threads. This
eliminates much of the thread managment, and helps keep my application that
much less complex.

If I'm in a server application, I manage my own threads. I tend to go with
option "B". Generally, I try to keep knowledge of the thread encapsulated
inside a class, so that external classes don't need to worry about it.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      4th Jan 2007
Interesting; I'd have thought that *for short lived threads* (to avoid
starvation) the server scenario would benefit *more* from
thread-pooling, since it will typically be serving more disparate
requests, and hence firing up more (briefly) threaded operations. Of
course, for longer running operations I prefer my own threads for more
control (naming, simple joining, avoiding pool starvation [although
you'd have to be massively threaded for this to be an issue]).

It is also harder to accidentally wabbit the thread-pool ;-p (ahh, the
fun "learning experiences" that stay in our minds...)

Marc


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      4th Jan 2007
Marc Gravell wrote:
> Interesting; I'd have thought that *for short lived threads* (to avoid
> starvation) the server scenario would benefit *more* from
> thread-pooling, since it will typically be serving more disparate
> requests, and hence firing up more (briefly) threaded operations.


Yup, but I'd still avoid using the system threadpool where possible.
It's quite easy to deadlock the system threadpool if one thread
(already in the pool) takes some action which requires another
threadpool thread in order to complete.

I'd recommend using a custom threadpool for custom jobs. Of course, if
..NET provided a way of creating *instances* of threadpools instead of
just having a single one, this wouldn't be an issue...

Jon

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      4th Jan 2007
Fair points

Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a new thread from an existing thread, which was started from atimer Curious Microsoft Dot NET 1 13th Jun 2008 07:36 PM
Start a new thread and capture the event happening in the thread Compact Framework samuel84@gmail.com Microsoft C# .NET 0 27th Feb 2007 08:36 AM
what does reuse thread meaning -- call thread.Start() again to execute same threadStart again? Ryan Liu Microsoft C# .NET 2 23rd Dec 2006 08:52 PM
Catch Exception thrown by spawned Thread.Start in Parent Thread =?Utf-8?B?RGF2ZSBCb29rZXI=?= Microsoft Dot NET Framework 6 22nd Nov 2005 01:33 AM
Secondary Thread start Method in Main Thread Dale Lundgren Microsoft C# .NET 3 4th Oct 2005 08:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:33 AM.