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();
> > }
> > }
> >
> >
> >