0

I have one batch file which run a console application written in C#.

This application will move some files from one folder to another application and make an entry in a databse.

The batch file is scheduled to run every 10 minutes.

Would there be any issues if one .exe runnig and after 10 minute another instance of same .exe is started?

What will happen if another instance of the .exe starts after 10 minute and the previous instance of the .exe is still running?

Servy
  • 202,030
  • 26
  • 332
  • 449
Urvashi
  • 484
  • 3
  • 6
  • 20
  • 4
    Why don't you just try out what will happen? ;) – lorenz albert Sep 05 '12 at 14:04
  • There shouldn't be an issue running an app multiple times unless the apps access the same resource. – 001 Sep 05 '12 at 14:05
  • addendum to Johnny's comment: some resources, like the database, may be designed for multiple concurrent accesses, while others, like the files you are moving around, may not be ok to access concurrently. – Sam Axe Sep 05 '12 at 14:08
  • We can't know whether or not it will work. It's entirely dependant on the specifics of your code. It could have been written in such a way that it won't have any problems, it could be written in such a way that problems are possible but unlikely, or it could be written in such a way that problems are rather likely. – Servy Sep 05 '12 at 14:13

3 Answers3

1

I would suggest forcing your executable to allow only a single instance.

there was a question about how to run a single instance in C# with a mutex the link provided in the answer shows a great example.

a single instance would defiantly prevent any database/file access exceptions. if it runs every ten minutes I don't think canceling it as another instance is already running would be a bad idea, if not you could always schedule a new one a few minutes ahead.

Community
  • 1
  • 1
StrikeForceZero
  • 2,379
  • 1
  • 25
  • 36
1

There are various things you could try.

Perhaps, query the running processes and exit if a previous instance is running.

Alternatively, you could check to see if the file is in use inside the file copy program (a typical method used in file watchers).

Robbie Dee
  • 1,939
  • 16
  • 43
1

I had a similar problem, what I did was write a quick console app that acted as an interface for the app.

within the main loop I wrote code to grab the processes running on the computer and checked to see if an instance is already launched. If not I launched my executable.

Process[] iProcesslist = Process.GetProcesses();
foreach (Process process in iProcesslist)
{
    if (process.ProcessName == "programName")
    {
        Log.Debug("ProgramName is currently running. Please wait for it to exit before launching again");
        return 0;
    } 
    else
    {
        using (var p = new Process())
        {
            try
            {
                p.StartInfo.FileName = @"C:\Users\username\Documents\Programming_Projects\programName\ProgramName\bin\Debug\programName.exe";
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                Log.Debug(ex.Message);
            }
        }
    } 
}

May not be the best solution, but it was quick and does what I want.

Robert H
  • 11,520
  • 18
  • 68
  • 110