ThreadPool - , ; DoAsyncStuffHere , , , - . , , .
The static constructor ensures that it is called only once, and only one thread should be started (unless there is some strange case with a .NET cache that I don't know about).
Here's the layout for an example - you probably need to implement some locking in the queue and add a bit more complex work to the workflow, but I used this template earlier with success. The object WorkItemmay contain the state of the information that you want to transfer to the workflow.
public static WebService()
{
new Thread(WorkerThread).Start();
WorkQueue = new Queue<WorkItem>();
}
public static void WorkerThread()
{
while(true)
{
if(WorkQueue.Any())
{
WorkQueue.Dequeue().DoWork();
}
else
{
Thread.Sleep(100);
}
}
}
public static Queue<WorkItem> WorkQueue { get; set; }
[System.Web.Services.WebMethod]
public List<Foo> SampleWebMethod(string id)
{
WorkQueue.Queue(newWorkItem());
}
source
share