0
votes

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);
    }              
}
1
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. - John Saunders
If you don't want multiple concurrent executions of the executable, why are you creating multiple schedules that execute it all at the same time? It seems like you're saying, "I have a system set up to do X, but I don't want it to do X. How do I stop it?" It's unclear to me what your desired behavior is. - Jim Mischel
@Jim - as you can see country code (i.e. 34) is hardcoded over there but in reality this will be coming as an argument. so each scheduler will be using different country code and I asked for best design approach which is an expert opinion and we do have our own opinion within our team but we are considering all possible solutions and will pick the one that suits best for us. hope this helps. - SnowWhite
I don't know why my question shows -1 and why my reputation score is down? people should be encouraged to asked questions - SnowWhite

1 Answers

0
votes

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