简体   繁体   中英

How can I check if a window is already open?

I have a program that creates a .png image and then opens, using the default program, the image to show the user the image.

My problem is that I don't want it to open again the image if the user left it open, what I mean is that if the user doesn't close the image I don't want a second window opening showing the same thing.

System.Diagnostics.Process myProcess = new Process();
myProcess.StartInfo.FileName = @"Labyrinth.png";
myProcess.Start(); 

Would anyone know a way to check if the image is open with the windows photo viewer or whatever default program you use.

You can use Process.MainWindowHandle and use Win32 Native API to check detail of that window.

Following link may help you on this.

Unexpected behaviour of Process.MainWindowHandle

Getting MainWindowHandle of a process in C#

As per my knowledge .NET framework not supported this thing directly but you can use P/Invoke to Win32 API and get detail.

Hope this help you.

The main problem here is that you basically know nothing about the program you're launching, only that it's associated with .png files, so checking for windows is possibly very situation-specific and not generally reliable.

What I would do is to check for the whole process instead. At the time of opening the second png, check if the first process is still alive and don't open if it's still is. The HasExited property serves that purpose. Try something like this:

private Process previousProcess = null;

public CreateImage()
{
    //Here put png creation as you already have
    //Now attempt to open it if the previous instance has closed

    if(this.previousProcess == null || this.previousProcess.HasExited)
    {
        //Either there was no previous image opened or it was already closed, go ahead and open it
        Process myProcess = new Process();
        myProcess.StartInfo.FileName = @"Labyrinth.png";
        myProcess.Start();

        //Cache the newly launched process to check for it afterwards
        this.previousProcess = myProcess;
    }
}

Note that this relies on your program to remain open within two consecutive generations, so it will not detect if you leave the image open, but close and reopen your program instead. Also, the user might reuse the same program opened for the image for doing something else, effectively closing the image but not the process, in which case further generated images will not be shown.

Of course a workaround to all this is to provide some way to display the image in your own program, and optionally giving the user an option to launch a program with it if the like, thus avoiding the problem entirely.

Edit

It seems that it's not necesary to Dispose() the previous object if the underlying OS process already has been terminated, so removed that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM