0

Im trying to make my programm behave like others (e.g. Steam) do. If the program is minimized and you start the .exe of it again, it shouldnt start a second instance, but maximise the first one. I already archived that with a little use of the winapi (User32.dll) like this:

if (AlreadyRunning(ref secondProcess))
{
    if(secondProcess != null)
    { 
        //forces a minimize
        ShowWindow(secondProcess.MainWindowHandle, 11); 
        //shows the window
        ShowWindow(secondProcess.MainWindowHandle, 1); 
    }
    //Stops the second instance
    Process.GetCurrentProcess().Kill(); 
}

The problem I now have is, that my programm often runs with Visibillity = Visibility.Hidden on the MainWindow. When this is the case and the function^ gets called, nothing happens. I have no idea how to change the property from the outside.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Marv
  • 119
  • 3
  • 14
  • Can't you write an event-handler for when the window get's shown and change the visibility if needed? – Oceans Nov 12 '15 at 09:31
  • There is no such event on the MainWindow I could attach a eventhandler to, and I don't know how to build own events :/ – Marv Nov 12 '15 at 09:44
  • Instead of killing the second process, don't start it in the first place. I think the correct way to do it is using a Mutex. – E Mett Nov 12 '15 at 10:17
  • @EMett I though a Mutex is used to help control multithread applications, how can it help me in this case? – Marv Nov 12 '15 at 13:02
  • 1
    @Marv: A mutex is a synchronization primitive, that **can** be used to coordinate multiple threads in a single process. It can also be used, to synchronize threads across process boundaries, since a (named) mutex is accessible from all processes running in the same desktop. On startup, create a mutex. If it already exists, show the other process' main window and terminate. Otherwise continue execution. When the process terminates, the mutex object is destroyed by the OS. – IInspectable Nov 12 '15 at 14:15
  • @IInspectable Thank you for your approach, that sounds clean and nice. I will give it a try next week. I was also thinking about just using another timer to work around that Problem, since changeing the visibillity does not take any effect when I try it with the an event. – Marv Nov 27 '15 at 15:25
  • @IInspectable As the last commentator and ine of the duplicate markers I adress you, I have made an easier approach that works very well for me, without having to use complicated mechanisms such as mutex or even the Winapi. Where would you sugesst me to put my answer? – Marv Dec 03 '15 at 08:27
  • @Marv: You would normally answer your own question. Since this question is closed, you can post an answer to the referenced question. Although I'm having a feeling, that not using a mutex is the wrong approach. For one, you'll run into race conditions, and a solution that doesn't use atomic primitives (such as a mutex) will occasionally fail. – IInspectable Dec 04 '15 at 00:06
  • @IInspectable I don't use any Multithread aside of timers, which just call methods in the ui thread, so I don't see where I could possibly end up in a race. My solution works with the creation of a temp file that is instantly deleted when my program detects it (it peeks evry half second). It might not be the cleanest solution but it works. – Marv Dec 04 '15 at 07:44
  • @Marv: There are 2 processes involved, so that makes at least 2 threads. Using file operations on Windows is not just unclean, it's simply wrong. You cannot force to delete a file using [DeleteFile](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915.aspx). It has unnecessary side effects, and polling is wrong, no matter how you look at it. I don't see how that is any easier than calling [CreateMutex](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682411.aspx) and checking the return value. – IInspectable Dec 04 '15 at 10:14
  • @IInspectable You are correct, regarding to the two Processes, but I check for the process runing evry time i start the programm to see if there is a second one and only in this case I create the tempfile and afterwards i kill the process. Also i dont use the deletefile from API I use the .net File.Delete() Method. Maybe im still too rookie to see my fault but i cant see why this is completely wrong. – Marv Dec 04 '15 at 10:30
  • @Marv: [File.Delete](http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,c4e7406e837331d5) calls [InternalDelete](http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,1167ef90427e1824), which in turn calls the native `DeleteFile` API. And who is going to clean up the file if your application crashes in between creating and deleting it? Besides, polling is the moral equivalent of continuously asking your parents: "Are we there yet? Are we there yet? Are we...". It burns clock cycles for no good. Just use a Mutex and event-based programming. – IInspectable Dec 08 '15 at 23:47
  • @IInspectable Okay, thank you I guess I got what you mean. But back to the Mutex, you said I sould simply call CreateMutex and check the return value, but from my point of view that would just do the same like just checking for the process running twice. How does that fix my problem, that Iam not able to call methods or change attributes from the outside of the application? – Marv Dec 11 '15 at 08:33

0 Answers0