使用多线程c#同时从多个数据库服务器获取数据

时间:2011-09-23 07:13:51

标签: c#

我想同时从C#programe调用相同的数据库程序到多个数据库服务器。我们可以使用多线程或任何替代方法来实现。

例如server1需要2分钟,server2需要3分钟,server3需要1分钟

总时间= 6分钟。

我想运行C#programe来运行与所有服务器并行的数据库过程,这样我就可以在2分钟内得到结果。

2 个答案:

答案 0 :(得分:1)

听起来很合理。您可以为执行所需作业的每个算法创建一个方法。然后使用ThreadPool并行调用此任务。

答案 1 :(得分:0)

if we have to use same function then i would have to implement multiple time? like this:

using System;
using System.Threading;
public class mythread
{
    public static void Main()
    {
        // Queue the task.
        Console.WriteLine(DateTime.Now);
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc1));
        //ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));

        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
        Console.WriteLine(DateTime.Now);
        Console.Read();
    }

    // This thread procedure performs the task.
    static void ThreadProc1(object obj)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello world!!,this is ONE.");
    }
    static void ThreadProc2(object obj)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello world!!,this is TWO.");
    }
}