views:

1472

answers:

8

Is it possible to inherit from the Thread class and override the Start method?

+2  A: 

No...

The Thread class is sealed...

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class Thread : CriticalFinalizerObject, 
    _Thread

Thanks

Chalkey
+4  A: 

The Thread class is sealed, but here's a good site for learning about Threads. Also, take a look at the Stackoverflow thread here : http://stackoverflow.com/questions/601558/multithreading-reference

Jean Azzopardi
+8  A: 

No the Thead class is sealed. The rationale behind this decision is that it's such a tricky and low level wrapper around the kernel object that you should not mess with it.

One question for you though, why would you want to override the Start method? What are you trying to achieve?

Yann Schwartz
Ditto about why one would want to override Start.
jrista
I'd lay pretty good odds he just wants to pass one or more strongly-typed parameters to his method.
Joel Coehoorn
+1  A: 

No it is a sealed class which means you cannot inherit from it.

CSharpAtl
+4  A: 

Not sure why you might want to do this (or that even should do this if it were possible), but there is a way to get around the fact that the Thread class is sealed - using extension methods. More technically, it's just adding an overload to the Start method group (rather than overriding), but that may still be helpful in your context.

Something like this might be what you want:

public static void Start(this Thread thread, int foo)
{
    // Your code here.

    thread.Start();
}

You can then simply call it as such:

var thread = new Thread();
thread.Start(123);
Noldorin
+3  A: 

Regarding why someone would want to do this: a lot of languages (e.g. Java) and/or thread APIs (e.g. Qt) allow developers to implement threads by inheriting from a "thread" base class, and then overloading a method that implements the thread routine.

Having used this model extensively in Qt, I actually find it very handy--instead of having threads target some function or method, which often leads to weird and/or convoluted code, the whole thread is contained inside of an object.

Here's example code using the Qt API:

class MyThread : public QThread
{
    Q_OBJECT

protected:
    void run();
};

void MyThread::run()
{
    ...something I need threaded...
}

QThread is Qt's threading base class. To use MyThread, create an instance of the thread object and call QThread::start(). The code that appears in the run() reimplementation will then be executed in a separate thread.

What's nice about this is I think it lets you really contain everything a thread needs inside a single object, and (in my experience), it makes for a very coherent object model. I don't think it meets everyone's needs, but I was a bit surprised that C# doesn't have such a basic threading model to be honest.

I definitely don't buy that C#'s Thread class is sealed because of kernel complexity; if Qt can supply a cross-platform threading library that allows inheritance of QThread, I see no real reason MSFT couldn't provide the same capability in a threading class in C#.

I agree with your response and have also used for many years that kind of abstract class in my C++ code.
Dave
+1  A: 

I'm also used to using the same logic in Java applications, and it makes serious sense to bundle a thread into an object which then further separates the start of a new process from its parent. Likewise, very disappointed C# does not support this approach. Dave P

+2  A: 
using System.Threading;

namespace NGrubb.Threading {

    public abstract class BaseThread {
        private Thread m_thread;

        public BaseThread() {
            m_thread = new Thread( Run );
        }

        public void Start() {
            m_thread.Start();
        }

        protected abstract void Run();
    }
}
Nathan