How to automatically close uwp applications - c#

I have two questions, first: how to let the uwp application perform a function, automatically close the uwp through the code instead of letting the user click the close button on the top right; second: how to minimize the uwp application to the taskbar at startup, not directly on the desktop, thank you.

You can use the static exit method:
public void CloseApp()
{
CoreApplication.Exit();
}
or Use the non-static method ():
public void CloseApp()
{
Application.Current.Exit();
}

One thing you have to keep in mind is that using CoreApplication.Exit and Application.Current.Exit closes the app, but it does so without going through the normal app suspension - for the system it appears as a "unexpected termination". I also think the certification guidelines are against this approach.
As for taskbar minimization - this is currently not supported. If you need this functionality, you will need to build a WPF app packaged as UWP app using the Desktop Bridge.

Related

How to create a console application that runs in background?

I created app that open every time that I start pc. So its so annoying to close it every time so I'm wondering if its some code that will hide my console app. I saw videos and tutorials on forms but idk how to do it with console app.
The easiest way to do this is change your console app to a windows app.
Console apps get a console made for them by Windows. But if you change it to a windows forms app, then windows expect the application to make a window, so if you never make a window, then it will never show
The other way is to turn your application into a service. This has some additional requirements in terms of programming
Option 1
You can use this run command:
start /min "SomeTitle" MyConsoleApp.exe myarg1 myarg2
Thus it will be on the taskbar minimized.
Option 2
If you use a file link in the start menu, select the start minimized option for the exe.
Option 3
Using a WinForm app you will be able to use a tray icon by setting the main form as not visible, to say it simply because it can be complex according to the expected behavior, and it will not be in the taskbar too.
Option 4
If you don't want a main form, create a win form app, delete the form file and the code in the main method, and you're done, without GUI nor console, no main input and no main output but you can show MessageBox and some forms when necessary, just a background process only visible in the Task Manager.
With that you can add a tray icon to to offer exit and some status information for example.
Option 5
Also you can create a windows service:
.NET console application as Windows service
Note
In all cases, if you don't use an internal message events dispatcher like the WinForms Application pattern or WPF and so on, be carefull to not saturate the CPU with the processings like with loops and use Thread.Sleep() between iterations or any thread idleing pattern or some timer if necessary.

Is it possible to detect the use of floating/multi-windowed apps while my app is running?

Our project is a mobile based examination app. The app should be able to detect the use of floating apps/multiwindow and app switching while taking an exam. We're supposed to use xamarin for this project.
If the student used a floating/multiwindow app or switched to another app, the exam activity should terminate.
Talking about iOS. It is possible. System has a bunch of execution states:
Apple Documentation I will talk about one of them.
Inside of the AppDelegate you can get the exact moment that users are leaving the app.
This method is called to let your app know that it is about to move from the active to inactive state...
// Swift
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func applicationWillResignActive(_ application: UIApplication) {
// do something
}
}
I hope this can help you.

How to programatically exit Xamarin Forms app without leave it running on second plan?

I'm trying to close Xamarin Forms App from a logout button. However the code I find (example below) leaves the app in a second plan without terminate it completely.
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
Is it possible to completely close the app without leave it running in a second plan? If so, please provide example code.
There is no approved way of doing this. At the very least not for iOS. There are private APIs or workarounds but if your plan is to have your app in the App Store(s) it will never pass the review process.
A simple way to make it work cross platform is by this command:
System.Diagnostics.Process.GetCurrentProcess().Kill();
However, Apple strongly discourages terminating apps. From here.So suggest that not doing this in IOS.^.^

My Windows Store app is still running in debug mode after I close it

I'm writing my first Windows Store app (windows 8.1) and I notice that when I run it in debug mode, and I close the app (by clicking the x in the top right, or by dragging from the top of the screen to the bottom), it is still running in Visual Studio. My first question is, is this a problem? It seems like it's a problem.
I started from a template, I'm not doing anything with threads, and there is only one page (MainPage.xaml) at the moment. I have looked at questions which seem similar, in particular this one:
WPF App Doesn't Shut Down When Closing Main Window
but I am unable to get their suggestions to work.
When I add ShutdownMode="OnExplicitShutdown" to my app.xaml, I get these errors in my Error List:
The member "ShutdownMode" is not recognized or is not accessible.
The property 'ShutdownMode' was not found in type 'Application'.
Also I notice that there is no StartupUri specified, nor can I add one (same errors as above.)
The other suggestion was to override OnClosed in MainWindow.xaml.cs and close the application there. I have no MainWindow.xaml.cs; I have MainPage.xaml.cs instead, and it does not have an OnClosed.
The Application class is of type Windows.UI.Xaml.Application.
If I pause VS after closing the app, it takes me to this (generated) code:
#if !DISABLE_XAML_GENERATED_MAIN
public static class Program
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
static void Main(string[] args)
{
global::Windows.UI.Xaml.Application.Start((p) => new App()); //<==here
}
}
#endif
Can anyone tell me what's going on?
This is entirely normal, the debugger tells you what is really going on. All Modern UI apps work this way. Just check it out with Task Manager, Details tab. Observe how dismissing the window doesn't terminate the process, it just suspends it.
You don't just have a modern UI, you have modern operating system behavior as well. A user doesn't have enough information available to judge if terminating a process is actually useful. If the machine has plenty of resources then there isn't any point. Better to keep the process running so that when the user starts it again, it instantly wakes up. Which is nice, users like that.
Conversely, if the OS requires resources for another process and not enough are available than it will automatically terminate a process without the user's assistance. The life-cycle for Modern UI apps supports this. Nothing particularly revolutionary btw, mobile operating systems like Android do this as well. Also the way I use my desktop apps these days, I just leave them running. Until I run out of taskbar space, cleanup then. Annoying :)
Truly stopping the process is easy, just click the Red Button on the VS toolbar.

Make a program which will be started by a scheduled task and must not show any window

I've a small c# application which does some sync between two libraries.
This has to run regularly, and the use of scheduled has been choosed.
So for now everything is working fine, except one thing, when the task is scheduled, I got a console windows which opens.
Is there any way(in the scheduler or in the application) to don't have any windows visible when the program is running?
My sync library can put in what I want.
(what if instead of using a console application, I create a windows application, without any windows, and the code started in the app.xaml.cs ? Is this a good idea? I've the impression this isn't the right way to do it).
Thank you!
In the Project Properties window, Change the output type from Console Application to Windows Application. Your application will still run, but it will have no window at all. You might also consider making a Windows Service instead.
Set up the task to run as a different user, then it won't popup to the currently logged in user. You could run it as SYSTEM i.e.
Yes, using a Windows App without any windows is the way to go. You can even show a tray icon in an app without windows if that might be useful.

Categories

Resources