当前位置: 首页>>代码示例>>C#>>正文


C# ThreadPriority枚举代码示例

本文整理汇总了C#中System.Threading.ThreadPriority枚举的典型用法代码示例。如果您正苦于以下问题:C# ThreadPriority枚举的具体用法?C# ThreadPriority怎么用?C# ThreadPriority使用的例子?那么, 这里精选的枚举代码示例或许可以为您提供帮助。


在下文中一共展示了ThreadPriority枚举的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Threading;
using Timers = System.Timers;

class Test
{
    static void Main()
    {
        PriorityTest priorityTest = new PriorityTest();

        Thread thread1 = new Thread(priorityTest.ThreadMethod);
        thread1.Name = "ThreadOne";
        Thread thread2 = new Thread(priorityTest.ThreadMethod);
        thread2.Name = "ThreadTwo";
        thread2.Priority = ThreadPriority.BelowNormal;
        Thread thread3 = new Thread(priorityTest.ThreadMethod);
        thread3.Name = "ThreadThree";
        thread3.Priority = ThreadPriority.AboveNormal;

        thread1.Start();
        thread2.Start();
        thread3.Start();
        // Allow counting for 10 seconds.
        Thread.Sleep(10000);
        priorityTest.LoopSwitch = false;
    }
}

class PriorityTest
{
    static bool loopSwitch;
    [ThreadStatic] static long threadCount = 0;

    public PriorityTest()
    {
        loopSwitch = true;
    }

    public bool LoopSwitch
    {
        set{ loopSwitch = value; }
    }

    public void ThreadMethod()
    {
        while(loopSwitch)
        {
            threadCount++;
        }
        Console.WriteLine("{0,-11} with {1,11} priority " +
            "has a count = {2,13}", Thread.CurrentThread.Name, 
            Thread.CurrentThread.Priority.ToString(), 
            threadCount.ToString("N0")); 
    }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:56,代码来源:ThreadPriority

输出:

ThreadOne   with      Normal priority has a count =   755,897,581
ThreadThree with AboveNormal priority has a count =   778,099,094
ThreadTwo   with BelowNormal priority has a count =     7,840,984


注:本文中的System.Threading.ThreadPriority枚举示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。