Sri Kumar

Sri Kumar

  • NA
  • 1
  • 0

Handling a multithread scenario

Feb 10 2008 8:35 PM
I have a scenario which needs some expert advice from people who worked on multithreading in C#.
Here is the scenario:

I have an Application (Let’s name it as MyApplication) which is invoked by another application. This external application calls the StartUp() method on my application initially.

My requirement is:
I have a Table in Database (Lets say QueueDBTable) which is filled up every now and then with data. The data is entered into this Table by a method in my application (lets say the method as FillDBQueue(data)). This method is invoked by an external application.

(The frequency of entry to this table is not dependent on time; rather it depends on some other external events).

I have to create a Thread in StartUp() method to direct to function in another class of my application. (let’s say the class be QueueProcessor and the method be StartProcess()).

I need to create and invoke a thread to this methos in my application’s StartUp() method.

Now in the StartProcess() method of my class of QueueProcessor, I run a query to get the data (some rows in the Table QueueDBTable matching a condition) and apply some business rules on that data and process. As I mentioned, a thread will be created and started in the StartUp() method which makes the thread running on this function. This is because the QueueDBTable might contain data even before this application is started.
After processing the query results, the same query need to be executed again and again to process any further data which might have been entered recently. (Like in a loop till no data exists to process in the table). If there is no Data, then the thread should be stopped/paused.

Again this thread should be invoked when some entry happens in the Table via the method FillDBQueue(data).
Note: Here I don’t want to call the StartProcess() for every entry into the table. I need to restart the Thread only when it is stopped/paused when there was no data in the table to process.

Below is a proto type code which I have thought of. You inputs will be of help.
Code: ( text )
  1. MyApplication.cs
  2.  
  3. public class MyApplication
  4.  {
  5.     Thread ProcessThread;
  6.  
  7.      public void StartUp()
  8.      {
  9.       QueueProcessor.ProcessRunningStatus = true;
  10.       ProcessThread = new Thread(new ThreadStart QueueProcessor.StartProcess));
  11.      ProcessThread.Start();
  12.       }
  13.  
  14.       public bool FillDBQueue(DataObject dObj)
  15.       {
  16.         // Store the Data in the QueueTable in Database.
  17.        if (!QueueProcessor.ProcessRunningStatus)   // If the Status is false
  18.        {
  19.          QueueProcessor.ProcessRunningStatus = true;
  20.          ProcessThread.Resume();
  21.        }
  22.       }
  23. } // End of MyApplication Class
  24.  
  25. ---------
  26. QueueProcessor.cs
  27.  
  28. public static class QueueProcessor
  29.  {
  30.   private static bool bProcessRunningStatus;
  31.   DataTable dtData = new System.Data.DataTable();
  32.  
  33.    public static bool ProcessRunningStatus
  34.    {
  35.       get { return bProcessRunningStatus; }
  36.       set { bProcessRunningStatus = value; }
  37.    }
  38.  
  39.    public static void StartProcess()
  40.    {
  41.      while (ProcessRunningStatus)
  42.       {
  43.        dtData = GetDataFromQueueTable(); // Get the Data from the database.
  44.        if (dtData.Rows.Count == 0)//If no data present in the DB for given condition.
  45.        {
  46.         ProcessRunningStatus = false;
  47.         Thread.Suspend(); // can't find the Suspend method
  48.         } // End of If
  49.         else // If Data exists.
  50.         {
  51.          ProcessRunningStatus = true;
  52.          for(int i=0; i < dtData.Rows.Count ; i ++)
  53.          {
  54.                // Process the Data
  55.          }
  56.         } // End of Else
  57.        } // End of While
  58.    } // End of StartProcess Method
  59. } // End of QueueProcessor class