views:

76

answers:

1

Hey all,

I'm just now starting to get into the idea of threading, and wanted to know if I could make this more abstract. Both foo and bar derive methods from a base class, so I'd like to pass in one or the other and be able to do work using a method that was derived. I'd also like to know how you properly name threads and the methods inside threads.

    if (ChkFoo.Checked)
            {
                Thread fooThread = new Thread(new ThreadStart(this.ThreadedFooMethod));
                fooThread.Start();
            }
    if (ChkBar.Checked)
            {
                Thread barThread = new Thread(new ThreadStart(this.ThreadedBarMethod));
                barThread.Start();
            }
    .
    .
    .
    public void ThreadedFooMethod()
    {
    Foo newFoo = new Foo();
    //Do work on newFoo
    }

    public void ThreadedBarMethod()
    {
    Bar newBar = new Bar();
    //Do similar work
    }

Thanks all!

+1  A: 

I would look at using an interface that they both implement. And if you really care if its a foo or bar you can use the "is" keyword and "as". You can pass in stuff to a thread using the thread pool but it must be of type object.

ThreadPool.QueueUserWorkItem(new WaitCallback("FuncName"), new "Foo or Bar");

David Daniel
+1 for interfaces! yay!
johnny g
I did not think of that. I think that's what I'll go with.
ibarczewski