locked
Batch Job misbehaves when Application is restarted in IIS Manager RRS feed

  • Question

  • I created a simple batch job to automatically start in AppFabric 1.1 on IIS 7.5:

    using System;
    using System.Threading;
    namespace AutoStart
    {
        public class test
        {
            int cnt = 0; Timer tm;

            public test()
            { tm = new Timer((object src) => Log("#" + cnt++.ToString()), null, 2000, 10000); Log("Start"); }

            ~test() { tm.Dispose(); Log("Stop"); }

            void Log(string s)
            {
                lock (this) using (var f = new System.IO.StreamWriter(@"c:\temp\log.txt", true))
                    f.WriteLine(String.Format("{0:T} {1} {2}", DateTime.Now, 
                        System.Diagnostics.Process.GetCurrentProcess().Id, s));
            }
        }
    }

    AppStart.cs in App_Code folder has AppInitialize() method creating my test class:
    public class AppStart { 
       public static void AppInitialize() { var cur = new AutoStart.test(); } 
    }

    I works fine, logging messages every 10 sec. I can recycle the pool, restart IIS, recompile my DLL, etc., except for Stop/Start Application from IIS Manager. This will produce truly bizarre result: all other Worker Processes supporting any pools configured to autostart will start running my timer!!! Bellow is a typical log:
    1:20:18 PM 8944 Start   // 8944 is my w3wp.exe
    1:20:20 PM 8944 #0
    ...
    1:21:20 PM 8944 #6
    1:21:29 PM 10956 Start  // Stop and Start Application in IIS Manager
    1:21:29 PM 4620 Start   // 10956 and 4620 are two other w3wp.exe
    1:21:30 PM 8944 #7
    1:21:31 PM 10956 #0
    1:21:31 PM 4620 #0

    Why this it happening? What do I do wrong and how to fix it?
    Any help is appreciated.
    Friday, October 19, 2012 5:50 PM