Looking for some feature that can make my app start up even fast, I found the prelaunch feature in the Microsoft documentation that would help open the app faster. But even if I register the app to enable prelaunch, the OnLaunched event keeps getting false in e.PrelaunchActivated.
(I only could test this feature using the VS option 'Debug UWP Prelaunch' in debug mode).
Do I need a Microsoft certificate to use it?
How long does the OS take to understand that my app is eligible to prelaunch?
Does this influence the fact that I'm getting false at the event?
I made a test and the prelaunch works correctly. I'll post my code here and explain how the prelaunch works. Hope it will help you solve your question.
Here is the sample code that I used:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
bool canEnablePrelaunch = Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch");
// ********
// some other code here
// *********
//check if it is prelaunched
if (e.PrelaunchActivated == false)
{
// On Windows 10 version 1607 or later, this code signals that this app wants to participate in prelaunch
if (canEnablePrelaunch)
{
TryEnablePrelaunch();
}
// ******
// normal code
// ******
}
else
{
//prelaunched
// do some logic that don't require UI thread.
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), "PreLaunched");
}
// Ensure the current window is active
Window.Current.Activate();
}
}
As you could see, I add an else sentence to do the work when the app is prelaunched.
When testing via the Debug Universal Windows App Prelaunch mode in Visual Studio, the whole process should be like:
Choose the Debug Universal Windows App Prelaunch mode, then the app will be prelaunched.
The OnLaunched event will be fired and you will go into the else sentence because it is prelaunched.
You could do your work in the OnLaunched event for prelaunched
Then the app will go to suspend status.
Now, the user launches the app from Start Menu.
The OnLaunched event will be fired again and this time it will show e.PrelaunchActivated == false because it is launched by the user, it is not prelaunched. I suspect this is the behavior that you are getting.
So this is the whole process about how the prelaunch works. This is also mentioned here: App launch.
In my case, Windows does not use Prelaunch when app is deployed by Visual Studio, but after installing it from the Store it works as expected. User must run an app at least one time and next time you will see it in the Task Manager. Will it work or not, depends on the amount of available memory, but I found it working even on a device with 4Gb of RAM.
Related
As part of some work I need to get done for Windows 10, I have written a code in C# that essentially detects every minute whether a PC is in screen saver mode or not, and it writes to a table in MySQL the relevant status ("PC in use" if the screen saver is off, "available PC" if the screen saver is on).
I did this using (full link if required - https://www.codeproject.com/Articles/17067/Controlling-The-Screen-Saver-With-C):
// Returns TRUE if the screen saver is actually running
public static bool GetScreenSaverRunning( )
{
bool isRunning = false;
SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0,
ref isRunning, 0 );
return isRunning;
}
The code works flawlessly in console application mode (I made a loop to test it out over a minute with a check up on screen save status every 10 seconds), this means in MySQL the status was set correctly every time, depending on the screen save status at the moment of the check up.
The problem occurs when I use this code for a windows service. The service is installed correctly, the log on tab is set on Local System (I also tried with the logged in user instead, same results) and I allow the service to interact with the desktop, just in case, but the difference here is that no matter if the PC enters screen save or not, it always returns false on GetScreenSaverRunning(), thus setting the status of the PC in MySQL as "PC in use", even if the screen saver is on at the moment of check up.
I get the sense that the problem isn't in the code itself, since it works without any issues as a console application, but perhaps something behind the scenes. I tried to search here and on many other websites, haven't found anything related to such a problem.
Does anyone have any idea at all what might be the issue? Any help and/or suggestions will be greatly appreciated!
Thank you in advance.
(I could post the code if required, but it is pretty much straight forward and the main part of it, controlling the screen save detection, is taken from the website mentioned above, afterwards it's a simple if (GetScreenSaverRunning() == true) )
Ever since Vista, Services are barred from a Interactive Session. Even if they run under the same rights, they do not get a interactive Session. I would guess that is getting in the way here.
While you can overwrite this behavior in the Service settings, this is not adviseable for new code. Consider making this a Background Task started by the Task Sheduler instead.
Because the windows service runs in different session then the windows logon. You can't interact with the desktop related services unless you run the windows service in win logon session. There used to be an option in Windows service manager where you can set the properties to "Interact with desktop session" but I don't think that ever worked.
There's a work around to run the windows service using the win logo session.
See this helper class that can get the current logged on user session and interact with the desktop services. https://github.com/murrayju/CreateProcessAsUser/blob/master/ProcessExtensions/ProcessExtensions.cs
I use Appium for testing Android device in C#.
Every time i run a test case, i need to go to Main screen and then begin the use case. This can be achieved in two ways, using: LaunchApp and StartActivity function.
As far as i believe,
LaunchApp launches app again, that is killing the running app and start the app from the beginning. However i will end up in startup activity. This will free up memory space, so app will not go into stress or out of memory issues.
StartActivity will not kill the App, but switch to particular activity. Which will be same as real case testing.
Is my above statement true? or what is the exact difference between the both?
In general you got it right, but it worth to add more context here.
How it works:
launchApp() by default does the following:
checks if app installed
performs fast reset (including app stop, cache cleanup, etc.)
starts the app with launch activity
checks package + activity to match the ones you set in capabilities (appWaitPackage, appWaitActivity)
startActivity does the following:
runs command via adb shell to launch specified activity
checks package + activity to match the one you set as argument
Real life examples, e.g. app that has LoginActivity -> NavigationActivity -> WhateverActivity flow:
You were logged in to the app
a) launchApp() will clean the app and move you back to LoginActivity
b) startActivity(NavigationActivity) will just launch NavigationActivity, so you don't have to login to the app.
You were not logged in to the app
a) launchApp() will be same as in Q1
b) startActivity(NavigationActivity) will fail with Incorrect package and activity as app flow does not allow it.
I'm using startActivity before each test to get to start point, that helps to speedup tests suite by avoiding app reinstall and relogin multiple times.
How should I "properly" programmatically terminate a Windows Phone 8.1 app?
Application.Current.Exit (); doesn't work. My code continues right through it.
Here's the snippet:
public static async Task ShowAndGo (String MessCode, String MessText, Boolean Xit)
{
String Mess = ""; // Start out with an empty Message to tell Joe User.
String Title = ""; // And an empty title too.
MessageDialog messageDialog = new MessageDialog (Mess, Title);
await messageDialog.ShowAsync (); // Stop and wait for user to acknowledge.
if (!Xit) // We're done here if we're not goin' down.
return;
//
// If we're goin' down,
// we have a lot of cleanup to do.
//
Application.Current.Exit (); // This should stop us. But it doesn't.
}
How should I "properly" programmatically terminate a Windows Phone 8.1 app?
The short: There isn't a "proper" way, and it's recommend that you shouldn't
The remarks on the Application.Exit method states:
Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.
The guidelines say you should:
Design your app to suspend when the user switches away from it and resume when the user switches back to it.
Don't terminate the app when the user navigates away from the app or closes the app using the close button. The operating system ensures that there is a consistent way for the user to access and manage apps. Your app is suspended when it is no longer visible to the user. By leaving the application lifecycle to the system, you ensure that your user can return to your app as efficiently as possible. Doing so also provides the best system performance and battery life from the device.
Rather register an event handler for the Suspending event.
It is called when the app is suspended. You can use this event handler to save relevant application and user data to persistent storage.
It is good to read the Application Lifecycle page.
How to programmatically terminate a Windows Phone 8.1 app?
Windows Phone 8.1
Application.Current.Exit();
Windows Phone 8.1 (Silverlight)
Application.Current.Terminate();
Your code
I used your code as is for testing, and it worked as expected when I remove:
if (!Xit) // We're done here if we're not goin' down.
return;
Xit is never assigned in your code sample; if Xit is false then the termination line will not be reached. Just remember that bool values are false by default (if no value is given to them)
Try using Application.Current.Terminate(); instead of Application.Current.Exit ();
For example
private void Exit_Button(object sender, System.Windows.Input.GestureEventArgs e)
{
Application.Current.Terminate();
}
Similar to this question which invokes the Windows 10 store to allow a user to write a review or rate an app, I'd also like to be able to invoke the Windows 10 Feedback app and allow users to provide feedback there.
I cannot seem to find much information on:
How this works in general. Can any old app use this service? (I
notice it just kind of shows whatever apps I have running)
How to invoke the Windows Feedback app with my package id
In short - not that I can see.
Other apps are invoked via protocol activation. I haven't seen this documented for the feedback app though so I have to err on the side of 'we haven't made this available yet' (I'm still checking though)
Here's an overall guide to the process http://blog.jerrynixon.com/2012/10/walkthrough-using-windows-8-custom.html?m=1
When I look in the registry under HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol I see (shortened a tad)
[HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol\PackageId\Microsoft.WindowsFeedback...\ActivatableClassId\App.AppX7eaybq6p4x7d4jgd6w6jk7r5dg6yhmbf.mca\CustomProperties]
"Name"="windows-feedback"
So - give that a try via launching windows-feedback
If I do Windows Key-R (run): windows-feedback://
it works fine so this should work:
var uri = new Uri(#"windows-feedback://");
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
Update
I've done some searching and it seems the magic parameter there is
windows-feedback:?contextid=522
That launches the NFL feedback for example. This is a predetermined number - I'm not sure how one gets on this list though.
In my windows phone7(Silverlight) Application I have to display a message box asking, the user to confirm(Yes/No) before exit from the application [on device back button click].
The problem is I have to use a custom messagebox(using a popup) to get user confirmation, and I have no way to get exit from the application.(No method found which will exit the application like dispose() or close()).
if I didn't have to use a custom messagebox, the on the Device back key press event "OnBackKeyPress" I would have use the following logic and done my work
MessageBoxResult res = MessageBox.Show("Do you want to Exit?", "Exit", MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
}
else
{
e.Cancel = true;
}
The problem is I need to use the custom messagebox and done this work. Same problem arise if need to implement a button to exit the application with out using the device back button.
I found in several posts suggesting to throw an exception and make this done. Following are some of them
http://mobile.dzone.com/articles/windows-phone-mango-sample-3
http://imaginativeuniversal.com/blog/post/2010/08/22/How-to-Quit-a-WP7-Silverlight-Application.aspx
I don't think that this is a good practice and also not sure if the windows market place will certify this way. Would like to hear the thoughts of once who have experienced this issue, and any suggestion to Achieve this(Terminate the application). Thanks inadvance....!!!!
If you want to submit to the Marketplace you've got a couple of problems because of the following certification requirements:
5.2.4.2 Pressing the Back button from the first screen of an application must close the application.
5.2.2 A Windows Phone application is closed and terminated by the OS when the user navigates away from the application. When an application is started after being closed, its launch time must meet the requirements in Section 5.2.1 – Launch Time
5.2.3 A Windows Phone application is deactivated when the user presses the Start button or if the device timeout causes the lock screen to engage. A Windows Phone application is also deactivated with it invokes a Launcher or Chooser API.
This is a couple instances where you simply can't display a message box.
And technically using an exception to termniate the app is a violation:
5.1.2 The application must handle exceptions raised by the .NET Framework and not close unexpectedly. During the certification process, the application is monitored for unexpected closure. An application that closes unexpectedly fails certification. The application must continue to run and remain responsive to user input after the exception is handled.
As a developer, part of your job is communicating to users about requirements that are unrealistic or unreasonable.
Sorry, no way to do this. Before mango update you could Clear the back stack, then programmatically trigger the Back button. but as of SDK 7.1 (wp7.5) we can no longer do this.
My recommendation is to create a custom Exception type ApplicationXExitException and throw that to exit the app. The reason for the custom type is so that when you pull your exception logs from the marketplace, you'll know the ones that were indeed unintended exceptions crashing the app, vs your exception to intentionally exit the app.
You can hook an event raising after your custom messagebox closes. Event arguments will keep information about user's choice. Depending on that you will decide whether to exit app or not.