I would like to set up a persistent state for my application. Let me explain.
The startup time is kinda long (mostly due to many database requests to a remote server, which take 5 - 10 seconds, and even more since my users usually have too much applications running...) and I'd like to set up a way to hide & show my application when needed.
What I am doing now is to only reduce app to tray when user clicks on the red cross. (The application really exits only when a user chooses File -> Exit).
All users are launching an installer which is checking the version installed, then the version available online, and update the app if needed before launching it.
Now, I'd like it to first check on the process monitor (the one found in Task Manager, Processes tab), and if a process is already running for the application, it'll just show the window again. Otherwise, if no process is running, we can process the classic-check-for-update-then-launch steps.
This would especially remove a lot of stupid customer requests I regularly have ("hey, your application takes too long to load, so I clicked on it again 5 times and it launched 6 instances!!!!" :/ ) and therefore save me a lot of useless time spent asking them to stop launching 50 instances of the same application cause it won't make it any faster...
So my main question is: how to perform such a trick in C#/WPF?
For now, my minimization process is kinda simple (even though maybe too simple): I just hide the window & the task bar entry. Now I don't know how to show it back from my installer
Any ideas?
Your customers' requests can never be stupid - they pay you money.
To bring window to front - create system wide mutex and check its presence on application startup. If it's there - use interprocess communication mechanisms to send message to that other instance to bring its main window to front (a window message or named pipe - both are fine). Here is an example (make sure to check related answers too).
And by any means show splash screen as soon as you can to prevent relaunching application again and again. If it does not appear in 1-2 seconds (2 is too long) it's bad. Responsiveness of your application makes feeling like it works faster.
Is it something like this you're looking for?
foreach (var p in Process.GetProcesses())
{
if (p.ProcessName.Contains("myProcess"))
{
//process is already running
}
}
Or, with LINQ:
if (Process.GetProcesses().Where(p => p.ProcessName.Contains("myProcess")).Any())
{
//process is already running
}
If the users complain about startup times, maybe consider checking the version on exit, instead of startup.
I have answered a very similar question yesterday. The only bit that's missing there is how to hide and show the window: use Window.Visibility, set it to Visibility.Hidden to hide the window and to Visibility.Visible to show it again.
Related
I am hoping to check at the beginning of an automated test if an application is open. I can check if the process is running by doing the following
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.Contains(name))
{
return true;
}
}
However, the process I want to find starts up about a minute before the application actually opens and is ready to be used by the test methods (its a very slow starting application). The above code sample looks at all windows processes running, but I am wondering, is there a way to do a similar method but to look at windows applications running?
There is a method already in class Process that you can use to check if an app with a UI has fully started:
Process.WaitForInputIdle(int milliseconds)
This will wait up to milliseconds ms for the message loop to become idle (and returns a bool to indicate success status). Depending on the application you're waiting for, you might want to allow 30 seconds or longer.
This might work for you, but be aware that in my experience for some applications it is not totally reliable!
The Windows API documentation has more details about the Windows API function that WaitForInputIdle() calls behind the scenes.
When a process is started, you can say application has started.
What you want is to wait until application startup progress has completed or not.
This means, when process is started, application startup begins. When application startup is completed, is becomes ready for user input. So I think you should have a look at following question and its answers.
Programmatically, how does this application detect that a program is ready for input
Apllication is proces.
If you can modify app, at app start you can create file and at end delete it. So you can chceck file existance. If file exist app starting/started.
If you need info when main form is created use:
WINFORMS
Form.Shown event.
WPF Loaded Event
uITestControl.Exists did the trick for me.
This method will return a boolean value corresponding to the existence of the application window being open. This allows an if statement to be created that can open the application if not already open, or do nothing if its already open.
I am writing an application in c# to lock or freeze all programs untill user enters a value in the app's textbox and clicks ok.
The purpose of the app would be to get people to enter their time.
As far as I know you can set it to top most but they can end the app with task manager so am stuck here..
formName.TopMost = true;
Any help would be appreciated
Yes, that's correct. The Windows operating system allows multiple programs to run at one time. What you're experiencing is entirely by design.
If I remember correctly, the TopMost property applies only to windows in your process, and as you mention, it's all quite irrelevant: the user can still kill your application using the Task Manager.
There's no legitimate way of getting around that. It's not a "limitation", it's a feature. Any app that prevents itself from being closed by the Task Manager is treading dangerously closely on the category of software that we call malware. Nothing good can come out of pursuits like this.
Relevant reading: The arms race between programs and users
Perhaps a good compromise solution is to make your window/form actually top-most and disable the Close button so that the user knows they shouldn't try and close it. This is almost always enough to stop a user that is not determined to end your application by any means necessary, and that's about all you should ever be concerned with.
See the sample code here for how to make your window/form always appear on top of other running applications by setting the WS_EX_TOPMOST flag or toggling HWND_TOPMOST.
I've also already written a detailed answer here about disabling the Close button the correct way by setting the CS_NOCLOSE class style.
I got a bit of a problem.
Related to my earlier questions about Slipstreamed SP3 vs. patched SP3, we've come to the conclusion that there is an Internet Explorer process being started, instructed to load a simple .html file from the local disk, which contains javascript, which opens up the rest of a larger chat/meeting system. Internet Explorer is started from a Lotus Notes client.
Unfortunately, all we can see is the IExplore.exe process popping up in Task Manager, and some seconds later, disappear again.
If we try to open the local .html file, which we've found on disk, it gives us that information bar at the top, telling us that it has disabled active content. This, however, is not the real problem. We have another machine that has the same settings but where everything works, and loading the .html file manually gives us the same error there as well.
However, perhaps there is another error message being shown when IExplore is started from notes, but since this process is supposed to just kickstart the rest of the system, and this window is hidden, we can't see it, that is, the error message / problem.
So, I thought, perhaps I should try creating a small program that waits for IExplore.exe to start, then immediately shows the window, so that we can see the error message, or whatever the problem is. At least, hopefully we'll be able to see that.
So far so good, except that if I start a process from my own program, with a hidden window, the main window handle is 0, and thus I cannot show the window after all. I expect this IExplore.exe process started from Lotus Notes to have the same issue.
My monitoring program is written in C#, and basically runs this loop:
foreach (var process in Process.GetProcesses())
{
if (process.ProcessName.ToLower() == "iexplore")
result.Add(process);
}
This picks up all the IExplore.exe processes, windows or not, and with IE8, I get 2 processes for the first window, as expected. I run this over and over again, and handles the differences from the previous runs.
However, the process briefly has a window handle 0 at the start, so I changed it to this:
foreach (var process in Process.GetProcesses())
{
if (process.ProcessName.ToLower() == "iexplore" &&
process.MainWindowHandle != IntPtr.Zero)
{
result.Add(process);
}
}
but now it doesn't pick up anything at all, even after the window has gotten a handle (and yes, process.MainWindowHandle does have a non-zero handle value after the window has been shown, but in the case where the window is never shown, it stays at 0.)
So, the question is: Is there any way for me to take this hidden IExplore.exe process, and instruct it to show itself, when it doesn't have a window handle already? I doubt it, but perhaps someone can prove me wrong.
If not, my backup plan is to create a shim IExplore.exe program, that forwards all command line arguments to the original one, except that it specifies that the window is to be shown. Would this be a solution?
I do not believe there is any way to force an IE window which does not have a window handle to allocate a window handle for itself (or use a previously allocated one) and display itself.
As to your backup method: I think this would work, but you're working in dangerous territory there. I'd actually recommend writing your shim to just log every invocation of iexplore.exe and everything that goes into it, and use that to characterize your problem; only after thoroughly characterizing your problem with completely benign logging would I suggest possibly modifying the parameters to force iexplore.exe to display a window.
Okay I've spent the afternoon researching and haven't had much luck finding the answer to this. I am trying to prevent an application from launching via some sort of dll or background application. It is to be used in monitoring application usage and licenses at my institution. I have found leads here regarding WqlEventQuery and also FileSystemWatcher. Neither of these solutions appear to work for me because:
With WqlEventQuery I was only able to handle an event after the process was created. Using notepad as a test, notepad was visible and accessible to me before my logic closed it. I attempted to Suspend/Resume the thread (I know this is unsafe but I was testing/playing) but this just hung the window until my logic finished.
With FileSystemWatcher I was not able to get any events from launching a .exe, only creating, renaming and deleting files.
The goal here is to not let the application launch at all unless my logic allows it to launch. Is this possible? The next best solution I came up with was forcing some type of modal dialog which does not allow the user to interact with anything, once the dialog is closed the application is killed. My concern here is killing the application nicely and handling applications with high overhead when they load such as Photoshop or something. This would also interfere with a feature I was hoping to have where the user could enter a queue until a license is available. Is this my best route? Any other suggestions?
Thanks
edit: To clarify this is not a virus or anything malicious. It's not about preventing access to a blacklist or allowing access through a whitelist. The idea is to check a database on a case by case basis for certain applications and see if there is a license available for use. If there is, let the app launch, if not display a dialog letting the user know. We also will use this for monitoring and keeping track if we have enough licenses to meet demand, etc. An example of one of these apps is SPSS which have very expensive licenses but a very limited pool of people using it.
Could you use
System.Diagnostics.Process.GetProcessesByName
in a loop to look for the process?
It might work if you don't use too aggressive a polling rate.
You are indeed close, take a look at the WMI Management Events. http://msdn.microsoft.com/en-us/library/ms186151%28VS.80%29.aspx
Sample code from Microsoft: http://msdn.microsoft.com/en-us/library/ms257355(VS.80).aspx
Subscribing to the appropriate event will provide your application with the appropriate information to perform what you described.
Not sure if this is a GOOD solution but you could do something like pass a key into main so that if the key is not present or valid the application shuts down. Then when you open the application in your code, just pass the key in. Someone would then have to know the key in order to start the application.
This is assuming you have access to the application in question's source code, which upon reading your question again, I'm not so sure of.
I assume you don't have source for the application you want to prevent from loading...
Have you considered using a system policy? That would be the best-supported way to prevent a user from launching a program.
You could have a service running that force-kills any app that isn't "whitelisted", but I can't say how well that would work.
I wonder if you are taking the wrong approach. Back in the day there was a Mac app that would prevent access to the desktop and had buttons to launch a set list of applications.
IDEA
What if you had a wrapper for the approved apps then only allow your wrapper to run on the computer?
I would expect there is some way of hooking an application launch, but can't help directly on that front.
You may be able to improve your current approach by detecting the application's window opening and hiding it (move it offscreen) so that the user can't attempt to interact with it while you are trying to shut it down.
However, another approach that may be possible (depending on your circumstances) would be to write an application launcher. This simply is a replacement for the shortcut to the application that checks your licencing conditions, and then does a Process.Start to launch the real .exe at that point. This would work well for any application. (I used a system like this for starting up applications with specialised environment settings and it works beautifully)
You could combine this with your current approach as a fall-back for "clever" users who manage to circumvent your launcher.
If my understanding is right you want to create an application what will prevent the computer user to start any other process except ones for a white-list.
If this is the case, monitor the process list of processes (in a while loop) using System.Diagnostics.Process (the GetProcesses method gives the list of all running ones)
Just kill the process when it starts.
Or if your machines have Windows 7 (Windows 2008??) you can use AppLocker. http://www.microsoft.com/windows/enterprise/products/windows-7/features.aspx#applocker Just let Windows prevent the startup.
You might want to look at this product: http://www.sassafras.com/licensing.html Personally I can't stand it, but that's because it does what you describe. Might save you some coding.
You could actually edit the registry so when you click a psd, your launcher gets called instead of photoshop. Your launcher then checks for licenses and if there is one starts photoshop with the path of the file.
This is a long shot but you may find it helpful.
Perceived Types and Application Registration
http://msdn.microsoft.com/en-us/library/cc144150(VS.85).aspx
What is the best and cleanest way to close a console application on windows mobile?
The application by default is invisible and you cannot see it in the running programs, which is great for running a background process, but sometimes the user might need to close it..
Exit Main. Seriously. If you need someone to be able to exit is manually, there needs to be some mechanism like a shell icon and menu or a program in the Programs folder of something. How else would the user even know it's running? Any one of those visual cues would then set a named system event, and inside your Console app you'd have something listening for the same event (likely a worker). When it gets set, you take the actions required to shut down.
How would a user be able to close it if the application is not visible in the UI?
That's a great question. I once spent a long time trying to figure this out. Of course, we are assuming you can not (easily) return from Main. The correct answer on the desktop is System.Environment.Exit; But that method is conveniently not supported on CF.
An apparent second option is Application.Exit. That is on CF, but only applies to WinForms, and is in fact not guaranteed to exit your application.
So, throw an unhandled exception. ;)
EDIT: To kill it programatically from another app, you can look at Process.GetProcessById, and Process.Kill. Both of these are available on CF. You will have to somehow let the "killer" app figure out the "victim"'s ID. More convenient methods like Process.GetProcessesByName are not available on CF.
This technique isn't that elegant, though, and there may be permissions issues.
You could also consider some kind of IPC (inter-process communication), perhaps one overviewed in this previous Windows Mobile answer.
I decided to to read a boolean (keep alive) in the config file and have another application set it to false when I want to exit.
Its not that responsive but at least I can exit cleanly..