0

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);
    }              
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
SnowWhite
  • 190
  • 1
  • 3
  • 9
  • Use a Global Mutex: http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c – thepirat000 Feb 23 '14 at 03:16
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Feb 23 '14 at 04:38
  • 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 Feb 23 '14 at 06:08
  • @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 Feb 23 '14 at 16:38
  • 1
    I don't know why my question shows -1 and why my reputation score is down? people should be encouraged to asked questions – SnowWhite Feb 23 '14 at 16:42
  • @MiranChowdhery if you are going to have scheduler calling your code with different arguments, I would go for a windows service application with a config file containing all country codes. you can make your windows service run at a particular time everyday and inside your application you can either spawn a new thread for each country code or just process them serially – Dexters Feb 23 '14 at 17:16
  • @Dexters That's a very good solution and I proposed this windows service solution to our client but they strictly want the console application because they want to use task scheduler or control-M job. so we had to stick to console application. – SnowWhite Feb 23 '14 at 19:44
  • I still don't see what problem you're trying to solve. If you want to process all of the countries, but do them one at a time, then have the program read a configuration file that contains all of the country codes. The program starts, reads the file, and starts processing. You can then have the Task Scheduler schedule your program once an hour or once a day or whatever, and have Task Scheduler NOT start the program if it's already running. If that's not the problem you're trying to solve, then please provide a more clear explanation. – Jim Mischel Feb 23 '14 at 22:41

1 Answers1

0

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;
       }
    }              
}
Dexters
  • 2,419
  • 6
  • 37
  • 57
  • This is a "crude hack" in more ways than one. Not only does using a file for locking like this come with its own set of problems, but your example won't work as supplied. It's going to fail if the file doesn't exist. Use `FileMode.OpenOrCreate`. – Jim Mischel Feb 23 '14 at 06:18
  • @JimMischel edited it. I agree, just came up with answer based on what I know. There may be really good ways, just haven come across yet. – Dexters Feb 23 '14 at 06:24
  • Just an idea. Can we spawn a separate thread for each task scheduler? – SnowWhite Feb 23 '14 at 16:45