programmatically close win8 app - c#

I've begun tinkering with making windows 8 apps And I want to make an exit button.
The problem is that Environment.Exit() and this.Close() that I'd use in winforms isn't in scope here.
anyone know how to close the app Programmatically?

Application.Current.Exit();
But closing a Metro app is not recommended. It is usually suspended.

Application.Exit
But be careful. You can fail certification by having code which forcefully closes the App. Make sure to read up on Certification Guidance if you plan to release to the store:
Your app must neither programmatically close nor offer UI affordances to close it. Windows 8 Process Lifetime Management closes Windows Store apps automatically.

Application.Current.Exit();
This was not allowed, but requirement 3.6, which used to not allow this, seems to be removed :
http://msdn.microsoft.com/en-us/library/windows/apps/hh694083.aspx#acr_changelog

Related

How to exit or close an UWP app programmatically? (Windows 10)

I need it for their own exit button. Tell me please?
I try this:
this.Close(); //or Exit dont work(
You can use the CoreApplication class. It provides a static exit method:
public void CloseApp()
{
CoreApplication.Exit();
}
However, the documentation states the following:
Note Do not use this method to shut down an app outside of testing or debugging scenarios.
Sadly, the reason behind that is left unkown.
Further more, you can use the old-fashioned Application.Exit method (non-static):
public void CloseApp()
{
Application.Current.Exit();
}
Here you should also take a look in the remarks:
Use this method to provide UI that enables users to exit your app. Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.
tl;dr:
Both Exit methods will terminate the app, rather than suspending it. You should ask yourself if this really is the action you want to do.
This is the supported way of exiting a UWP app:
Application.Current.Exit();
It is however relatively rare that you should use it. Consider carefully the UI experience related to the scenario where you would use this method. For example it may be justified to programmatically exit an application if some account has expired or security permissions managed remotely were revoked. It is rare that you have your own "Exit" button sitting in the middle of your screen without contravening Windows guidelines.
If you want to suspend the app instead of terminating try to use ApplicationView.TryConsolidateAsync(). For example, if app implements only one ApplicationView try calling ApplicationView.GetForCurrentView().TryConsolidateAsync() to close the app.
The obvious benefit of this method is app is closed just as you will do by pressing close button in titlebar, the closing is graceful, animation is the same and app is suspended instead of abruptly exiting.
Also, when you launch your app again after closing by this method, app starts in the same position and size as you closed it before while using Application.Current.Exit() and CoreApplication.Exit() doesn't start the app in the same position and size.

How to launch my C# application when any application starts in Windows 8?

I want to launch my application, like the windows security prompt, before any application is launched in Windows 8.
Is there any event handler, which gets notified whenever any application is launched?
My use Case : I want an App similar to a child lock(Lets call it myCustomApp).
When any user runs a game(say Solitare), i want myCustomApp to check the process name, and kill the process immediately.
P.S. : i am quite new to programming.
Thanks in advance!
Is there any event handler, which gets notified whenever any application is launched?
Yes: you get use WMI events to detect new instances of Win32_Process.
But these are created with process creation, not before.
Doing something between the call to ProcessCreate that creates the new process, and the process actually being created is going to be, at best hard (you might need to do it in the kernel), but quite possibly impossible.
Why do you want to do this? What problem are you trying to solve? This really does sound like an X-Y problem.
Edit:
The term you need to use is hook: the interception of some operation on windows. Eg. "Is it possible to hook the creation of windows globally so I can control where the windows are placed on the screen?"
There is a direct way in the kernel: PsSetCreateProcessNotifyRoutine
There are helpers in user mode (eg. EasyHook), but these require injected your code into each process (which anti-malware tools are likely to object to).
But you should still start out by looking for better approaches to you underlying problem.

How to disable Suspending || Closing || Terminating event for my Metro Style App on Windows 8 Pro

I'm a french developer and I need to develop an Metro Style app for Windows 8 Pro who is always launched. I wanted to know how can I disable the close event of my app. My app need to be in front all the time and the user couldn't quit the app.
I thought I could disable all the shortcut with the GPO but the close gesture (drag the app from the top to the bottom) need to me disabled too.
I hope I was clear and everybody will understand the question :-). Feel free to ask me more specific questions.
Cordially Renaud.
The short answer is: You can't.
The operating system and the user control the lifetime of Metro Style apps, you can't block the user from switching away from your app and once your app is no longer in the foreground, the application is suspended and the system can terminate the application at any moment.
Similarly, the user close gesture cannot be blocked.
In Windows 8.1 you may find Kiosk Mode to be what you need.

Metro App can no longer be programmatically killed?

I'm new to Win 8 Metro application development, and discovered that lots of things seem to be changed from the classic WPF.
What troubles me the most is that there's no way to close the app. This is extremely annoying when it comes to debugging the app. Therefore I'm looking at ways to add a "close" button in my app.
However, the old WPF way of:
Application.Current.Shutdown()
no longer exists. And I couldn't find the Process class in System.Diagnostics any more.
Does anyone know how to do this?
You're looking for App.Current.Exit()
The WinRT reference documentation for the developer preview states that:
CoreApplication.Exit | exit method
Shuts down the app.
Source: http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.core.coreapplication.exit.aspx
This is what I found to close the app.
App.Current.Exit();
As far as I know you can't close a Metro app (by design) (using the Task-Manager is the only option that works) and as far as I know there is no way to exit a Metro app programatically (by design too).
You could try to throw an unhandled exception (I wouldn't recommend that).
Try this.. It worked
App.Current.Terminate();
I used crash code to exit Windows 8 Metro APP.
char *p = nullptr;
*p = 1;

Closing a Windows Mobile Console Application

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..

Categories