-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ProcessThreadTests.Windows.cs
58 lines (51 loc) · 2.09 KB
/
ProcessThreadTests.Windows.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
using System.Threading;
using System.Linq;
using Xunit;
namespace System.Diagnostics.Tests
{
public partial class ProcessThreadTests : ProcessTestBase
{
[PlatformSpecific(TestPlatforms.Windows)] // P/Invokes
[Fact]
public void PriorityLevel_Roundtrips()
{
using (Barrier b = new Barrier(2))
using (Process currentProcess = Process.GetCurrentProcess())
{
int targetThreadId = 0;
// Launch a thread whose priority we'll manipulate.
var t = new Thread(() =>
{
targetThreadId = GetCurrentThreadId();
b.SignalAndWait();
b.SignalAndWait(); // wait until the main test is done targeting this thread
});
t.IsBackground = true;
t.Start();
b.SignalAndWait(); // wait until targetThreadId is valid
try
{
// Find the relevant ProcessThread in this process
ProcessThread targetThread = currentProcess.Threads.Cast<ProcessThread>().Single(pt => pt.Id == targetThreadId);
// Try setting and getting its priority
foreach (ThreadPriorityLevel level in new[] { ThreadPriorityLevel.AboveNormal, ThreadPriorityLevel.BelowNormal, ThreadPriorityLevel.Normal })
{
targetThread.PriorityLevel = ThreadPriorityLevel.AboveNormal;
Assert.Equal(ThreadPriorityLevel.AboveNormal, targetThread.PriorityLevel);
}
}
finally
{
// Allow the thread to exit
b.SignalAndWait();
}
t.Join();
}
}
[DllImport("kernel32")]
private static extern int GetCurrentThreadId();
}
}