Skip to content

Commit 9efae7c

Browse files
committed
Add test
1 parent eaef706 commit 9efae7c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libraries/System.Threading.Thread/tests/ThreadTests.cs

+28
Original file line numberDiff line numberDiff line change
@@ -1298,5 +1298,33 @@ public static void GetCurrentProcessorId()
12981298
{
12991299
Assert.True(Thread.GetCurrentProcessorId() >= 0);
13001300
}
1301+
1302+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
1303+
private static extern int GetThreadDescription(IntPtr hThread, out IntPtr threadDescription);
1304+
1305+
[DllImport("kernel32.dll", SetLastError = true)]
1306+
private static extern IntPtr GetCurrentThread();
1307+
1308+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
1309+
public static void NameNativeThreadBeforeStart()
1310+
{
1311+
string threadName = "Test thread name";
1312+
1313+
Thread t = new Thread(() =>
1314+
{
1315+
IntPtr threadHandle = GetCurrentThread();
1316+
GetThreadDescription(threadHandle, out IntPtr threadDescriptionPtr);
1317+
1318+
string nativeThreadName = Marshal.PtrToStringUni(threadDescriptionPtr);
1319+
Marshal.FreeHGlobal(threadDescriptionPtr); // Free the unmanaged memory
1320+
Assert.Equal(threadName, nativeThreadName);
1321+
}){
1322+
Name = threadName
1323+
};
1324+
1325+
t.IsBackground = true;
1326+
t.Start();
1327+
Assert.True(t.Join(UnexpectedTimeoutMilliseconds));
1328+
}
13011329
}
13021330
}

0 commit comments

Comments
 (0)