Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to inherit from a Thread Class in C# ?

Tags:

c#

The class Thread is a sealed class meaning it cannot be inherited from and I need an instance of a reusable Thread that should inherit from the Thread class. Does anyone have an idea how i can reuse tread ?

like image 465
Xris Avatar asked Nov 14 '11 14:11

Xris


1 Answers

A preferable alternative to using Inheritance is to use Composition. Create your class and have a member of type Thread. Then map the methods of your class to call methods from the Thread member and add any other methods you may wish. Example:

public class MyThread 
{
    private Thread thread;
    // constructors

    public void Join()
    {
        thread.Join();
    }

    // whatever else...
}
like image 193
Tudor Avatar answered Oct 04 '22 17:10

Tudor