summaryrefslogtreecommitdiff
path: root/tests/src/baseservices/threading/waithandle/waitone/waitoneex2.cs
blob: 889e1541ca12f6e858a6193b86f80e77e1bb74e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;

class WaitOneEx
{
    private Mutex myMutex;
    private ManualResetEvent myMRE;

    public WaitOneEx()
    {
        myMutex = new Mutex(false, Common.GetUniqueName());
        myMRE = new ManualResetEvent(false);
    }

    public static int Main()
    {
        WaitOneEx wao = new WaitOneEx();
        return wao.Run();
    }

    private int Run()
    {
        int iRet = -1;
        Console.WriteLine("Test AbandonedMutexException is " +
            "thrown when abandoned with a WaitAll");
        Thread t = new Thread(new ThreadStart(this.AbandonMutexWaitAll));
        t.Start();
        myMRE.WaitOne();
        try
        {
            Console.WriteLine("Wait on an abandoned mutex");
            bool bRet = myMutex.WaitOne(10000);
            Console.WriteLine("WaitOne did not throw an exception, bRet = " + bRet);
        }
        catch(AbandonedMutexException)
        {
            // Expected
            iRet = 100;
        }
        catch(Exception e)
        {
            Console.WriteLine("Unexpected exception thrown: " + e.ToString());
        }
        Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed");
        return iRet;
    }

    private void AbandonMutexWaitAll()
    {
        Console.WriteLine("Abandoning Mutex");
        WaitHandle.WaitAll(new WaitHandle[]{myMutex, new Mutex()});
        myMRE.Set();
        Thread.Sleep(1000);
    }
}