File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
src/libraries/System.Threading.Thread/tests Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments