本文整理汇总了C#中ThreadPriority类的典型用法代码示例。如果您正苦于以下问题:C# ThreadPriority类的具体用法?C# ThreadPriority怎么用?C# ThreadPriority使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ThreadPriority类属于命名空间,在下文中一共展示了ThreadPriority类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ThreadPool
public ThreadPool(int initialThreadCount, int maxThreadCount, string poolName,
int newThreadTrigger, int dynamicThreadDecayTime,
ThreadPriority threadPriority, int requestQueueLimit)
{
SafeWaitHandle = _stopCompleteEvent.SafeWaitHandle;
if (maxThreadCount < initialThreadCount)
{
throw new ArgumentException("Maximum thread count must be >= initial thread count.", "maxThreadCount");
}
if (dynamicThreadDecayTime <= 0)
{
throw new ArgumentException("Dynamic thread decay time cannot be <= 0.", "dynamicThreadDecayTime");
}
if (newThreadTrigger <= 0)
{
throw new ArgumentException("New thread trigger time cannot be <= 0.", "newThreadTrigger");
}
ExceptionHelper.ThrowIfArgumentNullOrEmptyString(poolName, "poolName");
_initialThreadCount = initialThreadCount;
_maxThreadCount = maxThreadCount;
_requestQueueLimit = (requestQueueLimit < 0 ? DEFAULT_REQUEST_QUEUE_LIMIT : requestQueueLimit);
_decayTime = dynamicThreadDecayTime;
_newThreadTrigger = new TimeSpan(TimeSpan.TicksPerMillisecond * newThreadTrigger);
_threadPriority = threadPriority;
_requestQueue = new Queue<WorkRequest>();
_threadPoolName = poolName;
}
示例2: PipeServiceClientChannel
internal PipeServiceClientChannel(PipeServiceClient client, ThreadPriority listenThreadPriority, int receiveTimeout, int sendTimeout)
{
this.client = client;
this.receiveTimeout = receiveTimeout;
this.sendTimeout = sendTimeout;
}
示例3: NUnitConfiguration
/// <summary>
/// Class constructor initializes fields from config file
/// </summary>
static NUnitConfiguration()
{
try
{
NameValueCollection settings = GetConfigSection("NUnit/TestCaseBuilder");
if (settings != null)
{
string oldStyle = settings["OldStyleTestCases"];
if (oldStyle != null)
allowOldStyleTests = Boolean.Parse(oldStyle);
}
settings = GetConfigSection("NUnit/TestRunner");
if (settings != null)
{
string apartment = settings["ApartmentState"];
if (apartment != null)
apartmentState = (ApartmentState)
System.Enum.Parse(typeof(ApartmentState), apartment, true);
string priority = settings["ThreadPriority"];
if (priority != null)
threadPriority = (ThreadPriority)
System.Enum.Parse(typeof(ThreadPriority), priority, true);
}
}
catch (Exception ex)
{
string msg = string.Format("Invalid configuration setting in {0}",
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
throw new ApplicationException(msg, ex);
}
}
示例4: BenchmarkWithThreads
/// <summary>
/// Замеряет и выводит на экран время потраченное на решение уравнений с использованием потоков
/// </summary>
/// <param name="totalEquationCount">Общее количество уравнений которые необходимо решить. Делится между потоками</param>
/// <param name="threadCount">Кол-во потоков которые следует использовать для параллельного решения уравнений</param>
/// <param name="priority">Приоритет потока</param>
private static void BenchmarkWithThreads(int totalEquationCount, int threadCount, ThreadPriority priority = ThreadPriority.Normal)
{
if (totalEquationCount % threadCount != 0) throw new Exception();
Console.Write("Время на решение с потоками: ");
int itemsPerThread = totalEquationCount / threadCount; // Кол-во уравнений для каждого потока
Stopwatch watch = Stopwatch.StartNew();
Thread[] solveThreads = new Thread[threadCount];
for (int i = 0, offset = 0; i < solveThreads.Length; i++, offset += itemsPerThread)
{
solveThreads[i] = new Thread(SolveThread)
{
Name = String.Format("Решатель уравнений #{0}", i + 1),
Priority = priority
};
solveThreads[i].Start(itemsPerThread);
}
foreach (Thread solveThread in solveThreads)
{
solveThread.Join();
}
watch.Stop();
Console.WriteLine("{0:F4} сек. Кол-во потоков: {1}. Приоритет: {2}", watch.Elapsed.TotalSeconds, threadCount, PriorityToString(priority));
}
示例5: ChangeThreadPriority
public void ChangeThreadPriority(ThreadPriority priority)
{
if (_thread != null)
{
_thread.Priority = priority;
}
}
示例6: Repeat
public void Repeat(string name, Action action, Action<Exception> onException, int interval = 100, ThreadPriority priority = ThreadPriority.Normal, CancellationToken? cancellationToken = null)
{
var thread = new Thread(
() =>
{
try
{
while (true)
{
if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested)
break;
Sleep(interval);
action();
}
}
catch (Exception e)
{
onException(e);
}
}
);
thread.Run(priority);
Register(thread, cancellationToken, name);
}
示例7: TaskSchedulerWithCustomPriority
public TaskSchedulerWithCustomPriority(int maxThreads, ThreadPriority threadPriority)
{
if (maxThreads < 1) throw new ArgumentOutOfRangeException("maxThreads");
this.maxThreads = maxThreads;
_threadPriority = threadPriority;
}
示例8: SingleThreadTaskScheduler
public SingleThreadTaskScheduler(string name = null, ThreadPriority priority = ThreadPriority.Normal, ApartmentState apartmentState = ApartmentState.STA)
{
#if DEBUG
_allocStackTrace = new StackTrace();
#endif
_thread = new Thread(
() =>
{
try
{
foreach (var task in _queue.GetConsumingEnumerable())
TryExecuteTask(task);
}
finally
{
_queue.Dispose();
}
});
_thread.IsBackground = true;
_thread.Name = name;
_thread.Priority = priority;
_thread.SetApartmentState(apartmentState);
_thread.Start();
}
示例9: Thread
internal Thread(ThreadStart threadStart, ThreadPriority priority, SIP sip, TaskHandle task)
{
Task = task;
_threadStart = threadStart;
_priority = priority;
_sip = sip;
}
示例10: SetThreadPriority
public static void SetThreadPriority(ThreadPriority priority)
{
if (Thread.CurrentThread.Priority != priority)
{
Thread.CurrentThread.Priority = priority;
}
}
示例11: EvoThreads
public EvoThreads(int threads, ThreadPriority priority)
{
// If the thread count is less than one or greater than the number of processors, throw an argument out of range exception
if (threads < 1 || threads > Environment.ProcessorCount)
throw new ArgumentOutOfRangeException("threads");
// Initialize the threads array
this._threads = new Thread[threads];
// Initialize the event arrays
this._resume = new AutoResetEvent[threads];
this._waiting = new ManualResetEvent[threads];
for (int i = 0; i < threads; i++)
{
// Create each thread in the threads array
this._threads[i] = new Thread(new ParameterizedThreadStart(this.ThreadStart));
//Set Priority
this._threads[i].Priority = priority;
// Set the properties for each thread in the threads array
this._threads[i].IsBackground = true;
// Create each element in the event arrays
this._resume[i] = new AutoResetEvent(false);
this._waiting[i] = new ManualResetEvent(true);
// Start each thread in the threads array
this._threads[i].Start(i);
}
}
示例12: Work
public Work()
{
_state = WorkState.INIT;
_description = string.Empty;
_priority = ThreadPriority.Normal;
_eventArgs = new WorkEventArgs(this);
}
示例13: BackgroundTaskQueueProvider
public BackgroundTaskQueueProvider(ThreadPriority priority)
{
// set up the task queue
_queue = new List<string>();
// spin up the threads - two per logical CPU
_maximumThreads = Environment.ProcessorCount * 2;
#if DEBUG
// if we're debugging, multithreading is a pain in the ass
_maximumThreads = 1;
#endif
for (int i = 0; i < _maximumThreads; i++)
{
_threadStates.Add(i.ToString(), false);
}
for (int i = 0; i < _maximumThreads; i++)
{
Thread thread = new Thread(Processor)
{
Priority = priority,
IsBackground = false,
Name = i.ToString(CultureInfo.InvariantCulture)
};
thread.Start();
_threadPool.Add(i.ToString(), thread);
}
}
示例14: SingleThreadExecutor
/// <summary>
/// Creates a new <c>SingleThreadExecutor</c> using a custom thread priority.
/// </summary>
/// <param name="priority">
/// The priority to assign the thread.
/// </param>
public SingleThreadExecutor(ThreadPriority priority) {
_actions = new BlockingQueue<Action>();
_running = new AtomicBoolean(false);
_shuttingDown = new AtomicBoolean(false);
_priority = priority;
}
示例15: ThreadBackground
public ThreadBackground(ThreadBackgroundFlags flags)
{
this.flags = flags;
this.currentThread = Thread.CurrentThread;
this.oldThreadPriority = this.currentThread.Priority;
if ((flags & ThreadBackgroundFlags.Cpu) == ThreadBackgroundFlags.Cpu &&
(activeFlags & ThreadBackgroundFlags.Cpu) != ThreadBackgroundFlags.Cpu)
{
this.currentThread.Priority = ThreadPriority.BelowNormal;
activeFlags |= ThreadBackgroundFlags.Cpu;
}
if (Environment.OSVersion.Version >= OS.WindowsVista &&
(flags & ThreadBackgroundFlags.IO) == ThreadBackgroundFlags.IO &&
(activeFlags & ThreadBackgroundFlags.IO) != ThreadBackgroundFlags.IO)
{
IntPtr hThread = SafeNativeMethods.GetCurrentThread();
bool bResult = SafeNativeMethods.SetThreadPriority(hThread, NativeConstants.THREAD_MODE_BACKGROUND_BEGIN);
if (!bResult)
{
NativeMethods.ThrowOnWin32Error("SetThreadPriority(THREAD_MODE_BACKGROUND_BEGIN) returned FALSE");
}
}
activeFlags |= flags;
++count;
}