本文整理汇总了C#中System.Threading.ThreadPool.QueueUserWorkItem方法的典型用法代码示例。如果您正苦于以下问题:C# ThreadPool.QueueUserWorkItem方法的具体用法?C# ThreadPool.QueueUserWorkItem怎么用?C# ThreadPool.QueueUserWorkItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ThreadPool
的用法示例。
在下文中一共展示了ThreadPool.QueueUserWorkItem方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Threading;
public class Example
{
public static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(ThreadProc);
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem, so stateInfo is null.
Console.WriteLine("Hello from the thread pool.");
}
}
输出:
Main thread does some work, then sleeps. Hello from the thread pool. Main thread exits.
示例2: Fibonacci
//引入命名空间
using System;
using System.Threading;
public class Fibonacci
{
private ManualResetEvent _doneEvent;
public Fibonacci(int n, ManualResetEvent doneEvent)
{
N = n;
_doneEvent = doneEvent;
}
public int N { get; }
public int FibOfN { get; private set; }
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Console.WriteLine($"Thread {threadIndex} started...");
FibOfN = Calculate(N);
Console.WriteLine($"Thread {threadIndex} result calculated...");
_doneEvent.Set();
}
public int Calculate(int n)
{
if (n <= 1)
{
return n;
}
return Calculate(n - 1) + Calculate(n - 2);
}
}
public class ThreadPoolExample
{
static void Main()
{
const int FibonacciCalculations = 5;
var doneEvents = new ManualResetEvent[FibonacciCalculations];
var fibArray = new Fibonacci[FibonacciCalculations];
var rand = new Random();
Console.WriteLine($"Launching {FibonacciCalculations} tasks...");
for (int i = 0; i < FibonacciCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
var f = new Fibonacci(rand.Next(20, 40), doneEvents[i]);
fibArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
}
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");
for (int i = 0; i < FibonacciCalculations; i++)
{
Fibonacci f = fibArray[i];
Console.WriteLine($"Fibonacci({f.N}) = {f.FibOfN}");
}
}
}
// The output is similar to:
// Launching 5 tasks...
// Thread 3 started...
// Thread 4 started...
// Thread 2 started...
// Thread 1 started...
// Thread 0 started...
// Thread 2 result calculated...
// Thread 3 result calculated...
// Thread 4 result calculated...
// Thread 1 result calculated...
// Thread 0 result calculated...
// All calculations are complete.
// Fibonacci(35) = 9227465
// Fibonacci(27) = 196418
// Fibonacci(25) = 75025
// Fibonacci(25) = 75025
// Fibonacci(27) = 196418
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
public static void Main()
{
ThreadPool.QueueUserWorkItem(delegate {
Console.WriteLine(Assembly.GetCallingAssembly().FullName);
});
Thread.Sleep(100);
}
}