2

for a task, potentially taking too long to complete, I'd like a mechanism

  1. to start the task
  2. return back to user interface (its a web page)
  3. periodically/randomly check if the task is complete
  4. cancel the executing task when user wishes so
  5. get notified when the task completes / fails

what are the possible solutions?

  1. Threads? Start a thread, save its ManagedThreadId, (can you get a thread by its id)

  2. write a windows service, send the request to service via shared objects/files/db? keep interacting with the service the same way (objects/files/db,etc) Services?

8
  • 1
    What version of .NET are you using? Sounds like the Task Parallel Library in .NET 4 would be ideal for this... Commented Oct 21, 2011 at 6:37
  • @JonSkeet TPL on a ASP.NET Web Page? You sure? Commented Oct 21, 2011 at 6:38
  • @Hasan - ah, hadn't seen that bit. Not in the web server itself, but quite possibly in a service backing the UI. Tasks could definitely still be relevant. Commented Oct 21, 2011 at 6:43
  • 1
    @HasanKhan: Well, if you start it as "long running" the scheduler should take that into account. Fundamentally if you're starting multiple threads, tasks, whatever then they will be contending for time. Commented Oct 21, 2011 at 7:34
  • 1
    @JonSkeet you're right. Just checked that LongRunningTask actually creates a new thread instead of using thread in a thread pool. Commented Oct 21, 2011 at 7:41

2 Answers 2

1

Host a WCF Service in a Windows Service that will perform the background tasks by adding/reading from a queue which can be maintained either using MSMQ or in a database.

When you add an item for processing; you should get a task id. You should be able to then log the completion/failed/cancel status of the task in db against the task id.

You can have following methods in your WCF contract

int ProcessItem(ItemDetails details); // returns task id bool CancelTask(int taskID); // returns true if successfully cancelled; false otherwise TaskStatus GetTaskStatus(int taskID); // returns Cancelled, Waiting, Failed or Completed 
Sign up to request clarification or add additional context in comments.

1 Comment

that looks like a good general solution, that can be paired up with a Request/Response Message system, sharing a RequestId.
0

You can do that in single process Have a look at the Task Paralell Library and / or TPL DataFlow

http://msdn.microsoft.com/en-us/library/dd460717.aspx http://go.microsoft.com/fwlink/?LinkId=205053

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.