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

前端 未结 7 1118
礼貌的吻别
礼貌的吻别 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:19

    C# uses the ThreadStart delegate instead of Java's Runnable style.

    public class Foo 
    {
    
       public void DoStuff()
       {
          while (true)
          {
             // do some stuff
          }
       }
    };
    
    public class Bar
    {
        public static int Main()
        {   
            Foo foo = new Foo();
            // create a ThreadStart delegate and pass in the method that will run 
            // (similar to run on Java's Runnable)
            Thread thread = new Thread(new ThreadStart(foo.DoStuff));
            thread.Start();
        }
    }
    

提交回复
热议问题