Question

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

class ThreadTest
{
    static void Main()
    {
        Thread t = new Thread (WriteY);          // Kick off a new thread
        t.Start();                               // running WriteY()

        // Simultaneously, do something on the main thread.
        for (int i = 0; i < 1000; i++) Console.Write ("x");
    }

    static void WriteY()
    {
        for (int i = 0; i < 1000; i++) Console.Write ("y");
    }
}

Result Expected

xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

So far I came up with

static void Main()
{
    Thread t = new Thread (ThreadTest.WriteY);          // Kick off a new thread
    t.Start();                                          // running WriteY()

    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) Console.Write ("x");
}

class ThreadTest
{
    public static void WriteY()
    {
        for (int i = 0; i < 1000; i++) Console.Write ("y");
    }
}

Actual Result

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...

As seen on Result Expected it should be mixed X and Y.
Unfortunately Actual Result is 1000 times X and 1000 times Y

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

Was it helpful?

Solution

Thread switching is by nature non-deterministic. I can run your program multiple times and get varying results.

If you want the switching to be more evident, add some pauses:

static void Main()
{
    Thread t = new Thread (ThreadTest.WriteY);          // Kick off a new thread
    t.Start();                                          // running WriteY()

    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) 
    {
         Console.Write ("x");
         Thread.Sleep(1);
    }
}


class ThreadTest
{
    public static void WriteY()
    {
         for (int i = 0; i < 1000; i++) 
         {
             Console.Write ("y");
             Thread.Sleep(1);
         }
    }
}

OTHER TIPS

I cannot explain why this works, but changing to using Dump() seems to make it behave like the OP wants with the x's and y's "mixed" with every run (although with newlines between every output):

void Main()
{
    Thread t = new Thread (ThreadTest.WriteY);          // Kick off a new thread
    t.Start();                                          // running WriteY()

    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) "x".Dump();
}

class ThreadTest
{
    public static void WriteY()
    {
        for (int i = 0; i < 1000; i++) "y".Dump();
    }
}

From the LinqPAD documentation:

LINQPad's Dump command feeds the output into an XHTML stream which it displays using an embedded web browser (you can see this by right-clicking a query result and choosing 'View Source'. The transformation into XHTML is done entirely using LINQ to XML, as one big LINQ query! The deferred expansion of results works via JavaScript, which means the XHTML is fully prepopulated after a query finishes executing. The lambda window populates using a custom expression tree visitor (simply calling ToString on an expression tree is no good because it puts the entire output on one line).

I also know that LinqPAD overrides the default Console.WriteLine behavior, so perhaps that has something to do with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top