CeaganP
CeaganP

Reputation: 21

C# Application.Run - Close Open File

I have created a program to run on all of my users computers. When the application is launched from my server (\\fs3\netlogon\app.exe) it stays open throughout the lifetime of the program. I identify this by seeing it as an "open file" in Computer Management. I can force the "open file" to close from Computer Management and the program still runs fine.

public partial class Primary : Form { *my code* }

Process[] procList = Process.GetProcessesByName(programName);

        if (Process.GetProcessesByName(programName).Length == 1)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Primary());
        }
        else
        {
            foreach (Process p in Process.GetProcessesByName(programName))
            {
                if (Process.GetCurrentProcess().Id != p.Id)
                {
                    p.CloseMainWindow();
                    p.Close();
                    p.Kill();
                    p.Dispose();
                }
            }
            Main();

Example of many instances of the file loaded

Before I go ahead and spend the time to develop an entirely new method I wanted to ask to see if I make my current format work.

My goal is to launch the app, close the "open file" and the app continues to run no problem.

This is likely a misunderstanding on my part.

Upvotes: 0

Views: 266

Answers (1)

Steve Todd
Steve Todd

Reputation: 1270

You're not explicitly closing the current instance if you find an existing one.

   if (Process.GetProcessesByName(programName).Length == 1)
   {   
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new Primary());
   }
   else
   {
       Application.Shutdown();
   }

Edited: fixed reasoning and solution

Upvotes: 1

Related Questions