Unable to inherit from a Thread Class in C# ?

后端 未结 2 1152
情话喂你
情话喂你 2021-01-04 18:43

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

2条回答
  •  抹茶落季
    2021-01-04 19:38

    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...
    }
    

提交回复
热议问题