TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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 )
MyApplication.cs
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
---------
QueueProcessor.cs
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
Reply
Answers (
0
)
Urgent Help: Connect to multiple Servers
regarding windows service