D
DBC User
I have a background process which reads a table to see if there are any
pending requests. If there are any, then it will start a worker thread
(only 10 allowed at a time) and executes a method. In this method, I
iniate a PROCESS and on completion, it reduces the available worker
thread and continue.
I have couple of questions;
1. Since I am launching multiple threads on a same method, do I have to
take care of locking so that each one doesn't step on each other or the
thread will creates its own address space and run the method there so I
need not worry about any locking. All I do in that method is, when the
method completes I reset the status of the request as complete or free.
2. Currently I am doing locking as you can see in the code I am adding
at the end.
Thanks for the input.
internal void ProcessRequests()
{
BatchRequests bProc = new BatchRequests();
while (true)
{
if (bProc.AnyRowAvailable())
{
bProc.Rewind();
do
{
if (bProc.LastAttempt.AddMinutes(15) <
DateTime.Now)
{
while (workingThread > 11)
System.Threading.Thread.Sleep(5000);
System.Threading.ThreadPool.QueueUserWorkItem(ProcessOneRequest,
(object)bProc);
UpdateAsInUse(bProc.AppName,
bProc.AppVersion);
System.Threading.Thread.Sleep(5000);
workingThread++;
}
} while (bProc.MoveNext());
}
else
{
System.Threading.Thread.Sleep(60000);
}
bProc = null;
bProc = new TaxAppBatchRequests();
}
}
protected void ProcessOneRequest(object request)
{
lock (request)
{
BatchRequests proc = (BatchRequests)request;
if (proc.ObjectImage.Length > 0)
{
Request req = (Request)request;
if (req.ProcessRequest())
UpdateAsComplete(proc.AppName,
proc.AppVersion);
else
UpdateAsFree(proc.AppName, proc.AppVersion);
}
workingThread--;
}
}
pending requests. If there are any, then it will start a worker thread
(only 10 allowed at a time) and executes a method. In this method, I
iniate a PROCESS and on completion, it reduces the available worker
thread and continue.
I have couple of questions;
1. Since I am launching multiple threads on a same method, do I have to
take care of locking so that each one doesn't step on each other or the
thread will creates its own address space and run the method there so I
need not worry about any locking. All I do in that method is, when the
method completes I reset the status of the request as complete or free.
2. Currently I am doing locking as you can see in the code I am adding
at the end.
Thanks for the input.
internal void ProcessRequests()
{
BatchRequests bProc = new BatchRequests();
while (true)
{
if (bProc.AnyRowAvailable())
{
bProc.Rewind();
do
{
if (bProc.LastAttempt.AddMinutes(15) <
DateTime.Now)
{
while (workingThread > 11)
System.Threading.Thread.Sleep(5000);
System.Threading.ThreadPool.QueueUserWorkItem(ProcessOneRequest,
(object)bProc);
UpdateAsInUse(bProc.AppName,
bProc.AppVersion);
System.Threading.Thread.Sleep(5000);
workingThread++;
}
} while (bProc.MoveNext());
}
else
{
System.Threading.Thread.Sleep(60000);
}
bProc = null;
bProc = new TaxAppBatchRequests();
}
}
protected void ProcessOneRequest(object request)
{
lock (request)
{
BatchRequests proc = (BatchRequests)request;
if (proc.ObjectImage.Length > 0)
{
Request req = (Request)request;
if (req.ProcessRequest())
UpdateAsComplete(proc.AppName,
proc.AppVersion);
else
UpdateAsFree(proc.AppName, proc.AppVersion);
}
workingThread--;
}
}