【问题标题】:Database operation via Threadpool or Asyn programming通过 Threadpool 或 Asyn 编程进行数据库操作
【发布时间】: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


【解决方案1】:

如果您不使用 .net 4.0 - 使用 ThreadPool 我将创建一个函数来接收它必须处理的行(如果将行转换为 DAL 中的强类型对象,则事件会更好),并执行与它相关的操作 - 类似于

using System;
using System.Threading;

namespace SmallConsoleAppForTests
{
class Program
{

    private static AutoResetEvent[] events;

    static void Main(string[] args)
    {

        int dataLength = 3;
        // creating array of AutoResetEvent for signalling that the processing is done
        AutoResetEvent[] events = new AutoResetEvent[dataLength];

        // Initializing the AutoResetEvent array to "not-set" values;
        for (int i = 0; i < dataLength; i++)
            events[i] = new AutoResetEvent(false);

        //Processing the data
        for (int i = 0; i < dataLength; i++)
        {
            var data = new MyWorkItem { Event = events[i], Data = new MyDataClass() };

            ThreadPool.QueueUserWorkItem(x =>
            {
                var workItem = (MyWorkItem)x;
                try
                {
                    // process the data

                }
                catch (Exception e)
                {
                    //exception handling
                }
                finally
                {
                    workItem.Event.Set();
                }
            }, data);
        }

        //Wait untill all the threads finish
        WaitHandle.WaitAll(events);
    }




}

public class MyWorkItem
{
    public AutoResetEvent Event { get; set; }
    public MyDataClass Data { get; set; }
}

// You can also use DataRow instead
public class MyDataClass
{
    //data
    //
}
}

如果您使用的是 .net 4.0 - 查看任务和并行扩展 (PLINQ)

【讨论】:

  • 如果您打算编写 Windows 服务 - 查看 Quartz.Net - 这是一个很棒的调度程序服务。您所要做的就是在您的代码中实现 IJob 接口,并配置 Quartz 来运行它。
猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多