开发者

New to threading in C#, can you make thread methods generic and what are the dangers?

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 = n开发者_JS百科ew 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!


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");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜