Thread.Name Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets or sets the name of the thread.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.StringA string that contains the name of the thread, or Nothing if no name was set.
| Exception | Condition |
|---|---|
| InvalidOperationException | A set operation was requested, and the Name property has already been set. |
The following example assigns names to the reader thread and writer thread when they are created, and then retrieves and displays those names in the output from the thread procedures.
For a full description of what this example does, see AutoResetEvent.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
Imports System.Threading Imports System.Text Class Example Private Const numIterations As Integer = 10 ' Create an AutoResetEvent that is initially not signaled. Private Shared myResetEvent As New AutoResetEvent(False) Private Shared number As Integer Private Shared running As Boolean = True Private Shared output As New StringBuilder() Private Shared outputBlock As System.Windows.Controls.TextBlock Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Example.outputBlock = outputBlock 'Create and start the reader thread. Dim myReaderThread As New Thread(AddressOf MyReadThreadProc) myReaderThread.Name = "ReaderThread" myReaderThread.Start() 'Create and start the writer thread. Dim myWriterThread As New Thread(AddressOf MyWriteThreadProc) myWriterThread.Name = "WriterThread" myWriterThread.Start() End Sub Shared Sub MyReadThreadProc() While running 'The value will not be read until the writer has written ' at least once since the last read. myResetEvent.WaitOne() SyncLock output output.AppendLine(String.Format("{0} reading value: {1}", _ Thread.CurrentThread.Name, number)) End SyncLock End While End Sub Shared Sub MyWriteThreadProc() For i As Integer = 1 To numIterations SyncLock output output.AppendLine(String.Format("{0} writing value: {1}", _ Thread.CurrentThread.Name, i)) End SyncLock number = i 'Signal that a value has been written. myResetEvent.Set() 'Give the Reader thread an opportunity to act. Thread.Sleep(1) Next i 'Terminate the reader thread. running = False myResetEvent.Set() SyncLock output ' Append the output from the example to the TextBlock control, ' on the UI thread. outputBlock.Dispatcher.BeginInvoke(displayHelper, output.ToString()) End SyncLock End Sub ' In order to update the TextBlock object, which is on the UI thread, you must ' make a cross-thread call by using the Dispatcher object that is associated ' with the TextBlock. The DisplayOutput helper method and its delegate, ' displayHelper, are used by the BeginInvoke method of the Dispatcher object ' to append text to the TextBlock. ' Private Shared displayHelper As New Action(Of String)(AddressOf DisplayOutput) Private Shared Sub DisplayOutput(ByVal msg As String) outputBlock.Text &= msg End Sub End Class ' This code example produces output similar to the following: ' 'WriterThread writing value: 1 'ReaderThread reading value: 1 'WriterThread writing value: 2 'ReaderThread reading value: 2 'WriterThread writing value: 3 'ReaderThread reading value: 3 'WriterThread writing value: 4 'ReaderThread reading value: 4 'WriterThread writing value: 5 'ReaderThread reading value: 5 'WriterThread writing value: 6 'ReaderThread reading value: 6 'WriterThread writing value: 7 'ReaderThread reading value: 7 'WriterThread writing value: 8 'ReaderThread reading value: 8 'WriterThread writing value: 9 'ReaderThread reading value: 9 'WriterThread writing value: 10 'ReaderThread reading value: 10
Note: