Intercept Opening Window - C# - c#

Is there some way to watch for / intercept a window opening in Windows and then to block said window from opening? This would be for an application that's already running and I wouldn't want to stop the process, just close the popup window it spawns.
Update:
A process is already running (e.g. it's in Task Manager) but has no visible windows.
At some random time, that process will popup a window
I manually close the window (click "OK").
The process continues to run at this point and will, again, popup a window a bit later (repeating these steps).
I want to automate step #3, where I have to manually close the popup, by intercepting that window opening and closing it or hiding it or never letting it open in the first place. I don't want to do this by polling the open windows. I want to receive some event that a window is about to open.
I do not control this other application, so I can't otherwise change it. And I don't want to kill the process, itself.

If you're trying to do what I asked (allow only one instance of a program to run at a time), here is an elegant solution using a Mutex. You can probably copy and paste most of that code to achieve what you want.

Related

How to distinguish between a program’s GUI and Console window?

I have written a code in c# that gets processes by name, activates the window of the desired process and sends some hotkeys to this process. The problem is that the desired process has two windows, a console window and a GUI window, as a result sometimes the GUI window is activated and everything works fine, but sometimes the console window is activated and nothing happens. So my question is, is there a way to distinguish between that process’ GUI and console window, so by program won’t get confused and only activate the GUI window ?
(By activating I mean bringing this process’ window to the foreground in order for the sending hotkeys to work. I have no access to the code of the desired process so I cannot change the way it starts up).

Force to close current window first in WPF

I'm working WPF application, Currently it has one window and at specific time (As per my requirement). It's displayed on top most priority.
this.Visibility = Visibility.Visible;
this.Topmost = true;
Now, I want like user should close first this windows(WPF application window), till cannot access any other thing from system.(user cannot able access even other application also) Seems like to force close first this window
I've been searched, but not getting anything.
How Can i do that?
You can't.
Imagine another application tried to do the same thing at the same time - you can't close window A before window B, but you can't close window B before window A, but...
Edit: the short version above can be misunderstood, so in more detail:
Let's say, your application has this very important message for the user, so it opens a special window. The special property of this window is that as long as it exists, the user can't access any other window. This also means, the user cannot close any other window.
Now while the user reads the message, another application has an important message, so it opens a window, using the same method as you to block access to other windows until it is closed.
The user cannot close the second window, because the first window is special and must be closed first. The user cannot close the first window, because the second window is special and must be closed first => the UI is completely locked. The poor user must use Task Manager to kill one of the two applications or reboot the system.
See also Raymond Chen's more detailed discussion of this principle.

Process does not kill on form close

I have just built version one of my testing application using Windows Forms. I have noticed that when running the application, it runs completely fine no hitches, exactly like the debug view. When it comes to closing the application I have noticed that the actual executable/process name hangs within Task manager and does not correctly close.
Upon further inspection I have noticed that when calling another form without hiding the previous form, a new process is spawned (kinda expected). When closing the new form (containing a few text boxes, labels and a DataGridView) the newly spawned process does not kill it's self, but remains. Then closing the main window the window disappears from the taskbar/view, but still, the processes remain active using 8,268k - 8,308k Memory
private void ClientSearch_Click(object sender, EventArgs e)
{
ClientSearch Clientsearch = new ClientSearch();
Clientsearch.Show();
}
Standard explanations for this behavior:
Hiding your main window when you display another window and forgetting to unhide it. There is no visible window anymore, nor can the user do anything to unhide it, but your app keeps motoring of course.
Starting a thread and not making sure that it is terminated when the main window closes. Setting the thread's IsBackground property to true is a workaround for that.
Calling Application.DoEvents() in your code. A very dangerous method that permits you to close the user interface but doesn't stop the loop in which it was called so the main thread of your app does not exit either.
This kind of problem is readily visible as well when you debug your app. You might have gotten in the habit of using the red rectangle on the toolbar (aka Debug + Stop Debugging) to force the debugger to quit. The Debug + Windows + Threads debugger window can provide insight into the cause of the last two bullets. Or you can use Tools + Attach to Process to attach the debugger to a zombie process.
Call
Application.Exit();
on form close/closing.
Your applications should only be creating one process per run. A new form should not be creating a new process.

How to tell whether closing via close button or via Application.Shutdown in WPF?

I added a code for standard "Are you sure you want to exit?" message inside the this.Closing
event of my main window.
It works as expected for closing from the gui, however I would like to disable this check when the application is closed via Application.Shutdown().
There is no event in the Application fired before the window gets closed and the Shutdown method cannot be overriden.
How can I detect what invoked the closing of the window?
Disclaimer: I'd make this a comment rather than an answer if I could as it is a dirty hack (not a clean one). But if you really need something and no better solutions present itself I'll post it anyway to hopefully help...
If you're definitely not in a position to control / hook the parts of the code base that are calling application shutdown, then the possibility is to find something else that responds to the shutdown that you can trip before your window closes.
My (not so ideal) thought on that is to set up another hidden window (SO: Loading a WPF Window without showing it) that the user wont interact with but the application will close on shutdown.
In the closing event of that window you can set a flag to indicate the application is shutting down. Then in your main window's closing event you can react to that flag.
The big issue to tackle is configuring things so that the hidden window will always close before the Main Window(s) safely.
I did some limited tests with MainWindow.Xaml as the application Startupuri, and creating the hidden window from the application.onstartup event and found the hidden window would close first - but I wouldn't want to gaurantee that in all scenerios! Actually getting this working and tested adequately could be a lot of work - as I said it's a last restort dirty hack.
Who calls Application.Shutdown()? If you are in Charge of that just set a flag indicating that you did it and check for that flag in the Closing event handler.

My WPF app still running if I open another Window

I'm writting an WPF application using the mvvm toolkint.
In the main windows I have a command in a button that open another window using:
catView.ShowDialog();
Well, I close that window (using a close button or the X) and when I close the main window, the app is still running and I have to kill it.
If I don't open the second window, the app shutdown normally.
Why if I open another window I can't close the app normally?
I have this in the close button of the second window:
this.DialogResult = true;
this.Close();
On the other hand, I start the app in this way (mvvm toolkit way):
Views.MainView view = new Views.MainView();
view.DataContext = new ViewModels.MainViewModel();
view.Show();
Thank you very much.
The problem is probably unrelated to opening and closing the window but is somthing inside that window.
This usually happens when you have another thread still running when you close the application, check for anything that might be creating a new thread inside the window's code (including System.Threading.Thread, ThreadPool, BackgroundWorker and 3rd party components), make sure all background threads shut down before closing the application (or if you can't shut them down at least mark them as background threads).
Also look for anything that can open another (invisible) window, it's common to use window messages to an invisible window as an inter-process communication mechanism, again look for 3rd party code that might be doing that.
Nir is correct, a thread is probably still running in your other window.
You can fix this by terminating the application's thread dispatcher when the window closes.
public Window1()
{
InitializeComponent();
// This line should fix it:
this.Closed += (sender, e) => this.Dispatcher.InvokeShutdown();
}
I'm happy to be corrected if this is not the right way to do things. Worked well for me though.
PS.
If your other window is designed to run in a different thread, then read this by Eugene Prystupa:
running-wpf-application-with-multiple-ui-threads
I don't know if this is causing your issue or not, but you don't need the call to Close() in your second window. Setting the DialogResult automatically closes the window.

Categories

Resources