473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

A Scenario in MultiThreading in C#

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

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
20
by: Dilip | last post by:
I understand the C++ standard does not talk about threading. My question here is directed more towards what happens when a STL container is used in a certain way. I'd appreciate any thoughts. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.