0

I have a very simple console exe application. This application will be executed by Windows Server R2 Task Scheduler after every 15 minutes. This was working fine for a long period of time but since last 2 days the application is not exiting some times. The problem is that Task Scheduler will never run the application again unless the previous started application exit. Here is my application code,

    static void Main(string[] args)
    {
        MyService.Process();
    }

    internal static void Process()
    {
        try
        {
            Logger.Log("Starting Service");
            // Synchronous Work
            Logger.Log("Ending Service");
        }
        catch (Exception ex)
        {
            Logger.Log("Unknown error occured: " + ex.Message);
        }
    }

When I check the log file I am seeing these lines at the end of file,

Starting Service
--------------------------
Ending Service

The Task Scheduler in Windows Server 2008 R2 showing me memory = 480), CPU = 0 and Threads = 1. What can be the possible cause that forbid the exe to exit. Also, note that it only happens some times. My application creates, moves, delete some files/directories using File and Directory class and send some data to server using WebClient.UploadValues

leppie
  • 115,091
  • 17
  • 196
  • 297
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • 1
    It is possible your app is creating a thread that is not marked as `IsBackground`. As long as such a thread sticks around, your app won't exit. So it is important you properly dispose all disposables within your "Synchronous work". You need to provide more details if you want help. – Eren Ersönmez Sep 09 '14 at 06:30
  • No I am not creating any thread. From task scheduler I am seeing Threads = 1 – Imran Qadir Baksh - Baloch Sep 09 '14 at 06:33
  • The [documentation for `UploadValues`](http://msdn.microsoft.com/en-us/library/9w7b4fz7(v=vs.110).aspx) states "This method blocks while uploading the data." but I'm not convinced that means it will stop the program from closing, you could simply comment this line out to find out if this causes the issue – Sayse Sep 09 '14 at 06:34
  • I didn't say _you_ are creating a thread, but something you call could be creating a thread. – Eren Ersönmez Sep 09 '14 at 06:34
  • @Sayse and Eren the interesting thing is that in my logs I am seeing the last line of has been code executed. – Imran Qadir Baksh - Baloch Sep 09 '14 at 06:35
  • @user960567 - You would because the work is still(may?) being carried out elsewhere on a foreground thread, the task scheduler may not even show this as it may be running on a separate process – Sayse Sep 09 '14 at 06:36
  • yes, and it probably executes on your main thread. Even if your main thread exists, the other thread could keep your app from closing. – Eren Ersönmez Sep 09 '14 at 06:36
  • Is there is way to find the foreground threads in Windows Server 2008 R2. I wanna to diagnose this. – Imran Qadir Baksh - Baloch Sep 09 '14 at 06:39
  • It may help to use something like this to [find child processes](http://stackoverflow.com/questions/7189117/find-all-child-processes-of-my-own-net-process-find-out-if-a-given-process-is) and see if there is any running.. (I'm not saying you should kill the processes like this however if possible) – Sayse Sep 09 '14 at 06:39
  • Will Procmon.exe help here? – Imran Qadir Baksh - Baloch Sep 09 '14 at 06:42
  • 1
    @Sayse the issue is not about a spawned process. It is about a single process that has a leftover thread. – Eren Ersönmez Sep 09 '14 at 06:42
  • @ErenErsönmez - It is guesswork. If it is the same process you can try [ProcessThread](http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread(v=vs.110).aspx) (Edited link - [Previous link](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.threads(v=vs.110).aspx)) – Sayse Sep 09 '14 at 06:45
  • If your program is ok to run two instance at the same time, in the Task Scheduler -> Settings Tab, at the bottom their is an option (select box) on "If the task is already running, the following rule applies" you can change the option. – ray Sep 09 '14 at 07:05
  • @ray, thanks wanna to avoid this option. – Imran Qadir Baksh - Baloch Sep 09 '14 at 07:10

0 Answers0