質問

How can I run the code below in LinqPad as C# Program Thank you...

class ThreadTest
{
    bool done;

    static void Main()
    {
        ThreadTest tt = new ThreadTest();   // Create a common instance
        new Thread (tt.Go).Start();
        tt.Go();
    }

    // Note that Go is now an instance method
    void Go() 
    {
        if (!done) { done = true; Console.WriteLine ("Done"); }
    }
}

UPDATE

This sample - along with all the others in the concurrency chapters of C# 5 in a Nutshell are downloadable as a LINQPad sample library. Go to LINQPad's samples TreeView and click 'Download/Import more samples' and choose the first listing. – Joe Albahari

役に立ちましたか?

解決

Just move Main out of ThreadTest and it should work fine. You also need to make the class and method public (or internal at that point):

static void Main()
{
   ThreadTest tt = new ThreadTest();   // Create a common instance
   new Thread (tt.Go).Start();
   tt.Go();
}

public class ThreadTest
{
    bool done;
    // Note that Go is now an instance method
    public void Go() 
    {
        if (!done) { done = true; Console.WriteLine ("Done"); }
    }
}

The "C# Program" is implicitly contained inside a class - moving main inside a nested class is probably confusing the executor which is looking for Main in the outermost class.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top