C# System.Threading.Thread继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/936034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Threading.Thread inheritance
提问by Dave
Is it possible to inherit from the Thread class and override the Start method?
是否可以从 Thread 类继承并覆盖 Start 方法?
采纳答案by CainKellye
I don't think any inheritance could result in a more readable code than this:
我认为任何继承都不会产生比这更易读的代码:
Thread myThread = new Thread(() =>
{
//Do what you want...
});
myThread.Start();
回答by Dave
No...
不...
The Thread class is sealed...
Thread 类是密封的...
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public sealed class Thread : CriticalFinalizerObject,
_Thread
Thanks
谢谢
回答by Jean Azzopardi
The Thread class is sealed, but here's a good site for learning about Threads. Also, take a look at the Stackoverflow thread here : Multithreading reference?
Thread 类是密封的,但这里有一个了解 Threads的好网站。另外,看看这里的 Stackoverflow 线程:多线程参考?
回答by Yann Schwartz
No, the Thread
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.
不,Thread
班级是sealed
。这个决定背后的基本原理是它是一个围绕内核对象的非常棘手和低级的包装器,你不应该弄乱它。
One question for you though, why would you want to override the Start method? What are you trying to achieve?
不过有一个问题,为什么要覆盖 Start 方法?你想达到什么目的?
回答by CSharpAtl
No it is a sealed class which means you cannot inherit from it.
不,它是一个密封类,这意味着您不能从它继承。
回答by Noldorin
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.
不知道为什么你可能想要这样做(或者如果可能的话甚至应该这样做),但是有一种方法可以绕过这个Thread
类的事实sealed
- 使用扩展方法。从技术上讲,它只是向Start
方法组添加重载(而不是覆盖),但这在您的上下文中可能仍然有帮助。
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);
回答by user25967
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.
关于为什么有人想要这样做:许多语言(例如 Java)和/或线程 API(例如 Qt)允许开发人员通过从“线程”基类继承来实现线程,然后重载实现线程的方法常规。
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.
在 Qt 中广泛使用这个模型后,我发现它非常方便——而不是让线程针对某个函数或方法,这通常会导致奇怪和/或复杂的代码,整个线程都包含在一个对象中。
Here's example code using the Qt API:
以下是使用 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.
QThread 是 Qt 的线程基类。要使用 MyThread,请创建线程对象的实例并调用 QThread::start()。然后,在 run() 重新实现中出现的代码将在单独的线程中执行。
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.
这样做的好处是我认为它可以让您真正将线程需要的所有内容包含在单个对象中,并且(根据我的经验),它构成了一个非常连贯的对象模型。我不认为它满足每个人的需求,但我有点惊讶 C# 没有这样一个基本的线程模型说实话。
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#.
我绝对不认为 C# 的 Thread 类因为内核复杂性而被密封;如果 Qt 可以提供允许继承 QThread 的跨平台线程库,我认为没有真正的理由 MSFT 不能在 C# 的线程类中提供相同的功能。
回答by user25967
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
我也习惯于在 Java 应用程序中使用相同的逻辑,将线程捆绑到一个对象中,然后进一步将新进程的开始与其父进程分开是很有意义的。同样,非常失望的 C# 不支持这种方法。戴夫
回答by Nathan
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();
}
}