tags:

views:

183

answers:

3

Hi I have created a little application to move some files around and put them into a database. This is working well except that the application needs a timeout.

If my app has not completed the task within 2 hours it re opens and locks out everything. I need to say that if my application has been open for 2 hours close it.

One thought was can you set a time out in an application. I have had a google for this and found some stuff in system.threading about timeout=infinite and threading.sleep=200 but don't really understand this.

Another thought, is using the timer and counting up to 2 hours and then calling a close method, this seams like a bit of a cheat.

Any ideas on time out in either C# or Vb


Sadly this is not for a db connection this is not where the application is failing.

A: 

What about another option? Set a flag which says whether the process is currently running or not.

If the process is running then dont start the second one or close the first one or may be be set a timer and check again after few minutes(?)

Shoban
This could work, although if the application falls over and fails this would not remove the flag and it would not run
Paul
A: 

Maybe a timeout for the DbConnection/DbCommand objects is more appropriate (have a look at MSDN)

Johannes Rudolph
+1  A: 

What about allowing just one instance of your application to run at the same time?

I guess you are scheduling runs: have a look at this code as a way of preventing multiple instances of your application without changing the scheduling.

static void Main(string[] args)
{
    Mutex flag = new Mutex(false, "UNIQUE_NAME");
    bool acquired = flag.WaitOne(TimeSpan.FromMilliseconds(0));  // 0 or more milliseconds
    if (acquired)
    {
        Console.WriteLine("Ok, this is the only instance allowed to run.");

        // do your work, here simulated with a Sleep call
        Thread.Sleep(TimeSpan.FromSeconds(5));

        flag.ReleaseMutex();
        Console.WriteLine("Done!");
    }
    else
    {
        Console.WriteLine("Another instance is running.");
    }
    Console.WriteLine("Press RETURN to close...");
    Console.ReadLine();
}

Please, don't abandon your Mutex... ;-)

HTH

Fabrizio C.