控制台应用程序由多个Windows任务调度程序同时执行

时间:2014-02-23 02:20:59

标签: c# multithreading parallel-processing console-application asmx

我正在编写一个C#控制台应用程序,该应用程序调用Web服务从数据库中提取大量数据(耗时的过程),Windows任务调度程序将在预定时间执行此控制台应用程序。现在,将创建许多任务调度程序(例如,至少70个任务调度程序)以同时执行相同的exe。设计此类应用程序的最佳方法是什么,以便在执行一个任务调度程序并且其他任务调度程序试图同时执行同一个exe时,不会中断此exe?

下面给出了一个示例程序

class Program
{
    static void Main(string[] args)
    {
        var countryCode = "34";
        WebServiceClient client = new WebServiceClient(); //ASP.NET web service
        var output = client.Process(countryCode);//this is a time consuming process and takes 5 minutes or more
        File.WriteAllText("c:\\test\\country.txt", output);
    }              
}

1 个答案:

答案 0 :(得分:0)

可能有效的粗暴黑客。我没有测试过它

创建一个文件Lock。让我们说它在c:\test目录

class Program
{
    static void Main(string[] args)
    {
        try{
           File.OpenOrCreate(@"c:\test\lock", FileMode.Open, FileAccess.Read, FileShare.None);

        var countryCode = "34";
        WebServiceClient client = new WebServiceClient(); //ASP.NET web service
        var output = client.Process(countryCode);//this is a time consuming process and takes 5 minutes or more
        File.WriteAllText("c:\\test\\country.txt", output);
        }
        catch(Exception e)
        {
           //you can check if there is file if already open message
           return;
       }
    }              
}