Threading: does c# have an equivalent of the Java Runnable interface?

前端 未结 7 1185
礼貌的吻别
礼貌的吻别 2021-01-31 15:44

Does c# have an equivalent of the Java Runnable interface?

If not how could this be implemented or is it simply not needed?

thanks.

7条回答
  •  灰色年华
    2021-01-31 16:05

    Does c# have an equivalent of the Java Runnable interface?

    Yes, it's ThreadStart

    class Runner
    {
        void SomeMethod() 
        {
            Thread newThread = new Thread(new ThreadStart(Run));
            newThread.Start(); 
        }
    
         public void Run() 
         {
              Console.WriteLine("Running in a different thread.")
         }
    }
    

    Would be equivalent to the following Java code

     class Runner implements Runnable {
    
         void someMethod() {
            Thread newThread = new Thread( this );
            newThread.start(); 
          }
    
          public void run() {
              out.println("Running in a different thread.");
          }
      }
    

提交回复
热议问题