此主题尚未评级 - 评价此主题

Thread.Name 属性

2013/12/13

获取或设置线程的名称。

Namespace:  System.Threading
程序集:  mscorlib(位于 mscorlib.dll 中)
public string Name { get; set; }

属性值

类型: System.String
一个包含线程名称的字符串,如果未设置名称,则为 null
异常条件
InvalidOperationException

请求设置操作,但已设置了 Name 属性。

此属性为只写一次。

下面的示例在创建读取器线程和写入器线程时为其命名,然后在线程过程的输出中检索并显示这些名称。

有关该示例执行的操作的完整说明,请参见 AutoResetEvent

说明注意:

要运行此示例,请参见生成具有静态 Windows Phone TextBlock 控件的示例


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


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示:
© 2014 Microsoft. 版权所有。