User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 375,216 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,319 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 204 | Replies: 0
Reply
Join Date: Apr 2008
Posts: 9
Reputation: macu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
macu macu is offline Offline
Newbie Poster

Threading Code - Your Opinion

  #1  
May 15th, 2008
For the purpose of long running tasks in an ASP.NET application I've written some test code that I plan to use to fire off long tasks in another thread so that the web request can return. This is my first real world usage of multi-threading so I'm interested in any comments on my approach and the locking I've used in the following code. In my test code I've split this into two classes, firstly AsyncThreadAbstractTask:

  1. public abstract class AsyncThreadAbstractTask
  2. {
  3. protected delegate void TaskCompleteHandler();
  4.  
  5. private TaskCompleteHandler _callback;
  6.  
  7. private Boolean _started = false;
  8. private Object _lock = new Object();
  9.  
  10. protected Boolean BeginTask(TaskCompleteHandler callback)
  11. {
  12. Boolean start = false;
  13. lock (_lock)
  14. {
  15. start = !_started;
  16. _started = true;
  17. }
  18.  
  19. if (start)
  20. {
  21. _callback = callback;
  22. Thread thread = new Thread(ThreadRunTask);
  23. thread.Start();
  24. }
  25.  
  26. return start;
  27. }
  28.  
  29. protected void EndTask()
  30. {
  31. _callback = null;
  32. _started = false;
  33. }
  34.  
  35. private void ThreadRunTask()
  36. {
  37. RunTaskCore();
  38. if (_callback != null) _callback();
  39. }
  40.  
  41. protected abstract void RunTaskCore();
  42. }

This begins the task and ensures that two threads can't start the same task (from the same created object which will be stored in Session). Other mechanisms (locking at db level) will stop two separately created objects running the same task.

An example of an inheriting class is AsyncDBTask:

  1. public sealed class AsyncDBTask : AsyncThreadAbstractTask
  2. {
  3. private Boolean _complete = false;
  4.  
  5. public Boolean Complete
  6. {
  7. get { return _complete; }
  8. }
  9.  
  10. public Boolean StartTask()
  11. {
  12. _complete = false;
  13. return base.BeginTask(this.TaskComplete);
  14. }
  15.  
  16. protected override void RunTaskCore()
  17. {
  18. DBTask task = new DBTask();
  19. task.DoUpdate("whatever");
  20. }
  21.  
  22. private void TaskComplete()
  23. {
  24. base.EndTask();
  25. _complete = true;
  26. }
  27. }

UI code can start the task, and monitor progress by checking the Complete property. The actual task in the example is just delegated to another class DBTask which does some updates and inserts.

Any comments appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C# Marketplace
Thread Tools Display Modes

Other Threads in the C# Forum

All times are GMT -4. The time now is 3:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC