0

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.

2
  • Are you explicitly opening the filestream in code?...or is this a side effect of running your app? Commented Jun 5, 2019 at 13:46
  • 2
    It is a misunderstanding, it is not a FileStream. You are seeing the side-effect of the memory-mapped view the CLR uses to load an assembly. Forcing the file to close is liable to crash the app when the just-in-time compiler needs to generate more code. ClickOnce deployment is the other way to do it, keeps the view open on a local copy of the assembly. Commented Jun 5, 2019 at 14:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Wouldn't you want to check that GetProssesesBYName returns anything before wrapping it in a using statement? Or at least check for null before the length?
GetProssesesBYName returns an array of zero or more objects. The using statement will never fail.
Alright. I wasn't sure if it did or not. A good note to have I suppose.
Mea culpa, read the wrong section in the docs, try the above.
I uploaded the code I use to handle if a process is already found to be running. This solution doesn't resolve my problem.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.