Question

I am writing a C# console application which calls a web service to pull a huge volume of data from database (time consuming process) and windows task scheduler will execute this console application at a scheduled time. Now, many task schedulers (for example at least 70 task schedulers) will be created to execute the same exe at the same time. What would be the best way to design such application so that this exe will not be interrupted while one task scheduler is executing and other task scheduler is trying to execute the same exe at the same time?

A sample program is given below

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);
    }              
}

No correct solution

OTHER TIPS

A crude hack that may work. I have not tested it though

Create a file say Lock. Lets say its in the c:\test directory

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;
       }
    }              
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top