Freigeben über


Thread.Name Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the name of the thread.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property Name As String
public string Name { get; set; }

Property Value

Type: System.String
A string that contains the name of the thread, or nulla null reference (Nothing in Visual Basic) if no name was set.

Exceptions

Exception Condition
InvalidOperationException

A set operation was requested, and the Name property has already been set.

Remarks

This property is write-once.

Examples

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.

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
using System;
using System.Threading;
using System.Text;

class Example
{
    private const int numIterations = 10;

    // Create an AutoResetEvent that is initially not signaled.
    private static AutoResetEvent myResetEvent = new AutoResetEvent(false);

    private static int number;
    private static bool running = true;
    private static StringBuilder output = new StringBuilder();
    private static System.Windows.Controls.TextBlock outputBlock;

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Example.outputBlock = outputBlock;

        // Create and start the reader thread.
        Thread myReaderThread = new Thread(MyReadThreadProc);
        myReaderThread.Name = "ReaderThread";
        myReaderThread.Start();

        // Create and start the writer thread.
        Thread myWriterThread = new Thread(MyWriteThreadProc);
        myWriterThread.Name = "WriterThread";
        myWriterThread.Start();
    }

    public static void MyReadThreadProc()
    {
        while (running)
        {
            // The value will not be read until the writer has written
            // at least once since the last read.
            myResetEvent.WaitOne();

            lock (output)
            {
                output.AppendLine(String.Format("{0} reading value: {1}", 
                    Thread.CurrentThread.Name, number));
            }
        }
    }

    public static void MyWriteThreadProc()
    {
        for(int i = 1; i <= numIterations; i++)
        {
            lock (output)
            {
                output.AppendLine(String.Format("{0} writing value: {1}", 
                    Thread.CurrentThread.Name, i));
            }

            number = i;

            // Signal that a value has been written.
            myResetEvent.Set();

            // Give the Reader thread an opportunity to act.
            Thread.Sleep(1);
        }

        // Terminate the reader thread.
        running = false;
        myResetEvent.Set();

        lock (output)
        {
            // Append the output from the example to the TextBlock control.
            // 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.
            outputBlock.Dispatcher.BeginInvoke(delegate () {
                outputBlock.Text += output.ToString(); });
        }
    }
}

/* 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
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.