A Scenario in MultiThreading in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srikmr
    New Member
    • Mar 2007
    • 1

    A Scenario in MultiThreading in C#

    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(dat a)). 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(dat a).
    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:
    [U][B]MyApplication.cs[/B] [/U] 
    
    public class MyApplication
     {
        Thread ProcessThread;
    
         public void StartUp()
         {
          QueueProcessor.ProcessRunningStatus = true;
          ProcessThread = new Thread(new ThreadStart QueueProcessor.StartProcess));
         ProcessThread.Start();
          }
    
          public bool FillDBQueue(DataObject dObj)
          {
            // Store the Data in the QueueTable in Database.
           if (!QueueProcessor.ProcessRunningStatus)   // If the Status is false 
           {
             QueueProcessor.ProcessRunningStatus = true;
             ProcessThread.Resume();
           }
          }
    } // End of MyApplication Class
    
    ---------
    [U][B]QueueProcessor.cs[/B] [/U] 
    
    public static class QueueProcessor
     {
      private static bool bProcessRunningStatus;
      DataTable dtData = new System.Data.DataTable();
    
       public static bool ProcessRunningStatus
       {
          get { return bProcessRunningStatus; }
          set { bProcessRunningStatus = value; }
       }
    
       public static void StartProcess()
       {
         while (ProcessRunningStatus)
          {
           dtData = GetDataFromQueueTable(); // Get the Data from the database.
           if (dtData.Rows.Count == 0)//If no data present in the DB for given condition.
           {
            ProcessRunningStatus = false;
            Thread.Suspend(); // can't find the Suspend method
            } // End of If
            else // If Data exists.
            {
             ProcessRunningStatus = true;
             for(int i=0; i < dtData.Rows.Count ; i ++)
             {
                   // Process the Data
             }
            } // End of Else
           } // End of While
       } // End of StartProcess Method
    } // End of QueueProcessor class
Working...