【发布时间】:2009-12-01 22:46:26
【问题描述】:
我有更多记录的数据库。(数据库值通过网络服务更新)
每条记录/行的类型为 (id,xmlfilename,operation,parameters,operationId,status) 即。 我们已经对 xmlfile 执行了由“操作”指定的操作 由“xmlfilename”指定,带有“parameters”指定的操作参数。 状态部分“最初”是免费的,因为在 reqd 时会更新。
我必须使用线程来执行这些(每一行)操作..即每个“id”一个线程 条目数。只要数据库中存在“id”行并执行“操作”
我怎样才能以最大的并行度和/或并发性有效地做到这一点。
有没有更好的方法?
什么最适合 Threadpool 或自定义线程或异步编程?
编辑添加:这是我尝试过的伪代码
string operationId=null;
while(operationId = DBWorker.getNextFreeOperationId ())
//how do i check this?another query??untill there are operations with status "Free"
//,keep selecting operationids
//note db is updating asynchronously.
{
//retrieve all rows with operationid=operationId eg:800 . 1 thread/qid???? and
// status="free" ...
//there are multiple operations with same operationIds
DataRowCollection dbRows=DBWorker.retrieveQueuedEntries(operationId);
MyWorkItem workItem = new DBWorker.MyWorkItem();
workItem.DataRows = dbRows;
workItem.Event = new AutoResetEvent(false);
//MyWorkItem.DoWork will do the necessary "Operation"
ThreadPool.QueueUserWorkItem(new WaitCallback(workItem.DoWork),workItem);
}
--------------
MyWorkItem.DoWork(obj x){
//for brevity
for each DataRow row in this.DataRows
{
performOperation(row);//use row["operation"] ..
}
---------------
bool performOperation(DataRow row)
{
string operation = (string)row["operation"];//retrieve other similarly
switch(operation)
{
case Operations.Add: //call Add operation ..
...
}
}
------------
以上代码是使用.net2.0..没有实现多线程.. 场景可能是数据库从一端异步更新,而上面的代码将同时作为windows服务运行。
谢谢
阿米特
【问题讨论】:
-
这整个逻辑将作为 Windows 服务的一部分传输......它将持续监控数据库的插入/更新
标签: c# database multithreading