views:

36

answers:

1

I have an asp.net application that uses the file (something like hand-made DB). It opens the file in non-shared mode. That's why only a single instance of application can use it.

But the problems begin when I'm updating the application on server (via app_offline.htm trick)

Sometimes the updated instance of application started before the old application had stopped. I have 2 instances of application working at the same time.

Is there any standard way to prevent it?

PS: I know about "Overlapped recycle" setting in Application Pool, but this is another thing. It is for multiple application pools working at the same time, but I have single application pool with several instances of the same application.

A: 

I found the following solution

  internal class AppInstanceSyncronizer
    {
    bool m_HasHandle;
    EventWaitHandle m_Event;

    const int START_TIMEOUT_MILLISECONDS = 60 * 1000;

    const string GLOBAL_EVENT_NAME="{D22555A0-BF47-41fd-AFC9-D83B4EADF75C}";

    internal bool OnAppStart()
      {
      bool createdNew;
      m_Event = new EventWaitHandle(false,EventResetMode.AutoReset,GLOBAL_EVENT_NAME,out createdNew);
      if(createdNew) { m_Event.Set(); }
      //--- first time created event
      m_HasHandle = m_Event.WaitOne(START_TIMEOUT_MILLISECONDS);
      return m_HasHandle;
      }

    internal void OnAppEnd()
      {
      //--- Release the event
      if(m_HasHandle)
        m_Event.Set();
      }

Just call OnAppStart and OnAppStart where your application starts and stops

murad