user3416924
user3416924

Reputation: 11

MultiThreading Async

Why does this code not reach the Console.WriteLine("Other thread is done!"); ? This code is from Pro C# 5.0 and the .NET 4.5 Framework book, pg 717-718.

        private static AutoResetEvent waitHandle = new AutoResetEvent(false);

    static void Main(string[] args)
    {
        Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);
        AddParms data = new AddParms(3, 4);
        Thread t = new Thread(new ParameterizedThreadStart(Add));
        t.Start(data);

        waitHandle.WaitOne();
        Console.WriteLine("Other thread is done!");
        Console.ReadLine();
    }

    private static void Add(object data)
    {
        Console.WriteLine("ID of thread in Add(): {0}", Thread.CurrentThread.ManagedThreadId);

        AddParms ap = (AddParms)data;
        Console.WriteLine("{0} + {1} = {2}", ap.A, ap.B, ap.A + ap.B);
    }

Upvotes: 1

Views: 47

Answers (1)

Tim
Tim

Reputation: 2922

waitHandle.WaitOne();

This line causes the execution to stop until the wait handle is set. The provided code never sets that wait handle, and thus the code blocks indefinitely.

Upvotes: 1

Related Questions