Resolved Stop/START Outlook fails ... sometimes ?

ksor

Active member
Joined
Jul 4, 2019
Messages
33
Programming Experience
10+
I have a console program doing automatic backup of my mailsystem every sunday managed by Windows Scheduler.

My program can do a lot of different thing, but the issue here is STOPPING, copying the OST-file and then STARTING Outlook again.

I use the following code snippet:

C#:
                        startInfo.WindowStyle = ProcessWindowStyle.Minimized;
                        Process.Start(startInfo);
                    }
                    else if (args[0].ToUpper()=="GO") {
                        if (ThisIsRunning("Outlook")) {
                            StopExecutionOf("Outlook");                                             <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< not always working
                        }
                        // Her testes på OM ALLE parametre ER angivet !
                        if ((Properties.Settings.Default.Datafil.ToString()=="") |
                            (Properties.Settings.Default.SikkMappe.ToString()=="") |
                            (Properties.Settings.Default.Modtager.ToString()=="") |
                            (Properties.Settings.Default.Bruger.ToString()=="") |
                            (Properties.Settings.Default.Password.ToString()=="")){
                            handleParams();
                        }
                        else{
                            File.Copy(Properties.Settings.Default.Datafil.ToString(), Properties.Settings.Default.SikkMappe.ToString(), true);
                            sendMail(Properties.Settings.Default.Modtager.ToString(), txtSuccess);
                            int milliseconds = 1000;
                            System.Threading.Thread.Sleep(milliseconds);
                            ProcessStartInfo startInfo = new ProcessStartInfo("Outlook.exe");            <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< not always working
                            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
                            Process.Start(startInfo);
                        }
                    }
                    else {
                        args=null;
                        ShowWindow(handle, SW_SHOW);
                        someThingWrong();

Outlook is not allways stopping - if I take a taskmaneger I can see Outlook is running - but else I can't see it on the screen !
WHEN Outlook is left 'running' - sort of hidden - THEN my command to start it again fails !

I have to manually stop the 'running Outlook' and then start Outlook again.

Here are the code I'm using for STOP of Outlook:

C#:
        private static Boolean StopExecutionOf(String r) {
            foreach (Process clsProcess in Process.GetProcesses()) {
                if (clsProcess.ProcessName.ToLower()==r.ToLower()) {
                    try {
                        clsProcess.Kill();
                    }
                    catch {
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }

Here are the code I'm using for testing for a running Outlook:

C#:
        private static bool ThisIsRunning(String r) {
            bool Result = false;
            System.Diagnostics.Process[] aProcess = System.Diagnostics.Process.GetProcesses();
            for (int ProcessIndex =0; ProcessIndex < aProcess.Length; ProcessIndex++) {
                if (aProcess[ProcessIndex].ProcessName.ToUpper() == r.ToUpper()) {
                    Result = true;
                    break;
                }
            }
            return Result;
        }

WHY is Outlook SOMETIMES left running as 'sort of hidden' process I HAVE to kill via the Taskmaneger ?
 
Last edited:
Outlook can be left in a zombie state if you (or someone else) is using the Outlook Object Model and not releasing all the interfaces/objects obtained from it properly.
 
Also this from the documentation:
Kill causes an abnormal process termination and should be used only when necessary. CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. If CloseMainWindow fails, you can use Kill to terminate the process.
 
>Skidiver
THX for your proposals, but isn't just using a 'soft' killing method instead of just going right for the heart in the first place ;-) - but I'll try this softer method !
 
The only reason I know about zombie Outlook processes is because I had the idea of writing a quick and dirty Outlook add-in that on the surface looked to only be about two day's worth of work: .75 day to up the learning curve of creating an add-in, and monitor when new messages are sent or received -- no need to go up the ribbon creation learning curve; .75 day for some testing automation; and .50 day as buffer time. That quickly blew up and became a 2 week project when Outlook zombie processes started showing up. During that process, reaching out to the Outlook devs revealed that one of the worse things I could do after not properly releasing COM objects, was doing a force Kill() because it could corrupt data files. And another bad thing was using COM objects across threads, even though they had marked their objects as free-threaded -- they were not really free-threaded.
 
Now I've tried ... but nothing seems to happen when I use this code:

C#:
        private static Boolean StopExecutionOf(String r) {
            foreach (Process clsProcess in Process.GetProcesses()) {
                if (clsProcess.ProcessName.ToLower()==r.ToLower()) {
                    try {
                        clsProcess.CloseMainWindow(); // Kill();
                    }
                    catch {
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }

Output window in VS is showing:

Build started...
1>------ Build started: Project: KopierOutlook, Configuration: Release Any CPU ------
1> KopierOutlook -> X:\2 - Måneds\01 VisualS\Projects\KopierOutlook\KopierOutlook\bin\Release\KopierOutlook.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I then tried my original Kill() again and it works WITHOUT leaving this zombie Outlook processes ... but will it last ... I wonder !
 
I've gone back to my original Kill() and made it so I can test my program via parameters - STOP, START and GO

When I use the parameter STOP Outlook in fact stops - leaving NO zombie process !
And I can use the START parameter to again start Outlook !

Then I tried the whole backup procedure by using the GO parameter and I works nicely !!!!

BUT ...

When I let the Windows Scheduler handle the backup procedure (= start my program with the parameters GO) - then Outlook WONT stop - it leaves this zombie process, that blocks the starting of Outlook again - it copies the OST-file and send an email to tell the result og the backup, but it WONT start Outlook again !

Here is my code for sending this email:

C#:
        private static void SendMail(String Til, string bdy)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("Support@KS.NET");
            mail.To.Add(Til);
            mail.Subject = "Sikkerhedskopiering af Outlook";
            mail.Body = bdy;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(Properties.Settings.Default.Bruger.ToString(),
                                                                      Properties.Settings.Default.Password.ToString());
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
        }

I'm very confused now !

What does the Scheduler do to sort of damaging my stopping of Outlook - and leaves this zombie process ?
 
Your code above to send mail doesn't even need Outlook since you are talking directly to the SMTP server.
 
As for that code that calls CloseMainWindow(), where are you waiting for the process to end, or time out after a while and eventually call Kill()? Read the documentation for Kill() which talks about this.
 
Ha, ha, I know that, but the user does need Outlook ... that's why it should be started ... OK, they can start it themselves but first they have to kill the zombie process and THEN it's too much !
 
As for the code running as a Scheduled Task, you may need to play with who the task is running as, as well as the check boxes to allow desktop interaction.
 
Now I've tried to run my backup command in a CMD-windows - just like that:

kopierOutlook.exe GO

where "kopierOutlook.exe" is my console program and the "GO" is a argument for going the whole procedure 1) STOP, COPY, (send an e-mail) and START Outlook and it works nicely !

In Scheduler it wont - I sthink it's the START of Outlook that fails and leaves a 'zombie background' process of Outlook and the zombie HAS to be killed in the Taskmanager for Outlook can start again !

I then tried to wrap my command "kopierOutlook.exe GO" into an ordinaity BATCH file and control that batch-file via the Scheduler ... and it works - strange but that's my path out of this mess !

THX to all for your time !
 

Latest posts

Back
Top Bottom