Search code examples
c#multithreadingwindows-servicesqueuelong-running-processes

Processing long-running operations from a windows service


Edit (again): Let me simplify my problem. I have a Windows Service that exposes some WCF endpoints with methods like:

int ExecuteQuery(string query) {
   // asynchronously execute query that may take 1 second to 20 minutes
   return queryId;
}

string GetStatus(int queryId) {
   // return the status of the query (# of results so far, etc)
}

What is the best way to implement the ExecuteQuery method? Should I just call ThreadPool.QueueUserWorkItem to get my query going?

Note that the actual work behind executing a query is done by load-balanced black box. I want to be able to have several queries going at the same time.

The analogy is a web browser that is downloading multiple files simultaneously and you have a download manager that can track the status of each file.


Solution

  • Either this is a trick question or a no-brainer... ThreadPool.QueueUserWorkItem is about the easiest way to go when you want to execute a piece of code concurrently. I'm sure you already knew that, so technically you have already answered your own question.

    So if this is not a trick question, then are you asking exactly how to pass the query in the ThreadPool.QueueUserWorkItem?