I have a database with a lot of records. (database values are updated via webservice)
Each record / line is of type (id, xmlfilename, operation, parameters, operationId, status), i.e. we perform the operation indicated by the "operation" in the xmlfile file, the specified "xmlfilename" with the parameters for the operation, indicated by the "parameters" .. the status part is "intimate" free, as it is updated as with reqd.
I need to perform this (every line) operation using Threads .. one thread per 'id' number of records. As long as there are "id" lines in the db selection and "operations" are performed
how can I do this efficiently with maximum parallelism and / or concurrency.
Is there a better way?
What is threadpool or user thread or asyn programming best for?
Edit Added: Here is the pseudo code I tried
string operationId=null;
while(operationId = DBWorker.getNextFreeOperationId ())
{
DataRowCollection dbRows=DBWorker.retrieveQueuedEntries(operationId);
MyWorkItem workItem = new DBWorker.MyWorkItem();
workItem.DataRows = dbRows;
workItem.Event = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(workItem.DoWork),workItem);
}
--------------
MyWorkItem.DoWork(obj x){
for each DataRow row in this.DataRows
{
performOperation(row);
}
---------------
bool performOperation(DataRow row)
{
string operation = (string)row["operation"];
switch(operation)
{
case Operations.Add:
...
}
}
------------
the above code uses .net2.0 .. does not achieve multithreading .. the script may be, the database is updated from one end asynchronously, while the code will work as a Windows service at the same time.
THX
Amit
source
share