I would like to manage the behavior of my application when it closes (and not when it is suspended), but I couldn't find any method to override.
In this application, a user can log in with his profile: when he is in the main page of the application and presses back button, the app goes suspended and it's ok then that if it's resumed the user is still logged; what I want to do is that if the application is closed, the user won't be logged anymore, and if he opens the app again he has to log in again.
When the login is successful, I set local settings this way:
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["Logged"] = true;
So I would like to set to "false" the value of logged whenever the application is closed (not suspended), but like I said I couldn't find any kind of "on close" method to override.
The only way I've found to achieve what I want is to set
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["Logged"] = false;
in the "OnLaunched" method in App.xml.cs, but it's not working fine.
Define a handler vor the Application.Suspending event (https://msdn.microsoft.com/en-us/library/windows.ui.xaml.application.suspending).
The system suspends your app whenever the user switches to another app or to the desktop, and resumes your app whenever the user switches back to it. However, the system can also terminate your app while it is suspended in order to free up resources.
So there is no special event for closing the app.
Related
When creating a .NET Core Web app C# and debugging you can opt to Launch the browser when Running the app. The console launches and then the url launches in your preferred browser. Once you close the browser tab opened by the debug session, the app process it stop and is no longer running on the machine.
When I published the app, the page does not launch automatically so I am starting a new Process to do so. I've added an event handler to the process before starting it but the since the app didn't launch the process it doesn't receive any notification when the tab has been closed.
What is the proper way to go about launching your .NET Core web app and/or handle communicating between the two?
I even tried adding a Close button but JS won't allow me to close the window as I haven't opened it.
I'm creating my Process as follows:
var p = Process.Start("cmd", "/C start http://localhost:5000");
I've tried
p.WaitForExit();
p.EventHandler += newHandler;
and I also tried using ProcessStartInfo but the only thing that is working right now is if I set a Timer and then kill the process when the timer is activated but this doesn't seem too clean.
Any advice is appreciated!
I wanted to follow-up with the process I feel is the most graceful way that I am happy with.
A few pieces of info that may be helpful in creating your own process. It was a goal of mine to use .net core to self host a client side web app which communicates with a server hosted web api.
I am using Razor views and .net core 3.1
I am using Startup and utilizing applicationLifeTime to handle OnStartup where I start the process as I described in my original post.
At this point the web app opens and if the user decides to close the browser the process doesn't know and continues to run in the background.
In order to prevent it from continuing to run and allow the user to initiate it closing, I made a few the following additions.
First, I added a timer that uses a configurable length of time via appsettings. When starting the process that opens the browser I call and set the timer that will perform an "AutoShutdown" if the process is still running after the configurable length of time that I defaulted to 1hr. I am using Enviroment.Exit(0) in the event that handles closing the running process.
My View is using a Model that contains a property who's value will be set from the page via a hidden field.
I added a Close button to the View that is displayed to the user at startup. When the user clicks close, I use jquery to set a hidden field in the view and then I use jquery to submit the form.
When the Model is received, the value set by the client's request to close is true and you can handle the close without display errors if you use a Timer and Start it almost immediately after redirecting the View to a view with a message to the client. Whether they leave it open or close it, the process will have closed. Example of how I did it below.
Finally, in Main.cs I put a check in to check if there is an existing process running and close it before starting a new one. I added this because the client may not always close the browser and/or they may attempt to start it before the "AutoShutdown" timer has kicked in.
This is more "hacky" then I would like but I couldn't find anything else that didn't throw and show Bad Request when trying to use Envirnoment.Exit(0) when the client is still has the view open and I was unsuccessful at being able to allow console input so that I could send close to the console.
This is how I am handling the close request from the client in my Controller
if (ui.HandleCloseApp)
{
var app = Process.GetProcessesByName("NameOfYourWebApp");
if (app.Length >= 1)
{
foreach (var p in app)
{
if (p.ProcessName == "NameOfYourWebApp")
{
p.Close();
p.Dispose();
}
}
}
var t = new System.Timers.Timer(10);
t.Elapsed += OnClose;
t.Start();
return View("Close");
}
I hope this helps someone else in creating their own "graceful" process that shouldn't be :)
I'm still open if anyone has input to share. Thx!
I have created a simple weather application and I added the code below to let the user let it run on Startup:
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (startupCheck.Checked) {
rk.SetValue("WeTile", "\"" + Application.ExecutablePath.ToString() + "\"");
} else {
rk.DeleteValue("WeTile", false);
}
Now this runs fine on both my computers. But when I gave the app to my girlfriend. She said the app does not run on windows start up. I read it online that it could be because of the user permission or the location so I told her to move the app to c:/ and try checking the box again and then restarting. Now it works but on every startup she has the default windows message saying you want to run this app?
How do I get rid of this? What is the best way to add to windows startup that works with both windows 32/64 bit without any user permission disruptions?
It sounds like you may have run afoul of Windows' file blocking security function. Applications created on another computer are automatically blocked from executing unless the user specifically "unblocks" the file. Have your girlfriend right-click on the executable, select "Properties" and see if there is a button at the bottom of the dialog to unblock the file.
Once unblocked, you should no longer see the confirmation prompt at startup.
You could add it to the Windows startup folder, check if it's not there already and if not, add it (assuming this is what the user wants).
See How do I set a program to launch at startup
In wp7 app I simply call web browser task. But when I press back button, it closes the task but also wp7 app. So, How do I go back to app ? (with back button or something else it doesn't matter.)
WebBrowserTask task = new WebBrowserTask();
task.Uri = new Uri("myUri", UriKind.RelativeOrAbsolute);
task.Show();
If you launch a WebBrowserTask from your app, the user will return to your app by default after closing the browser with the back button.
It is possible that your code raises an exception when it is reactivated and then it crashes.
To test this, simply run it with F5 and you will see if you get an exception.
Okay, when I asked the question, I had no clue about things in Windows phone 7.
So if you navigate to a new page or start new task(like web browser), then you click the back button, it goes back to the page which called the method(navigate or new task). There's no problem with it.
However, if an exception raises when navigating back, or going back to the app from task, the program crashes with Debugger.Stop() in App.xaml.cs.
I was using PhoneApplicationService.Current.State to store datas because I was passing these datas from page to page. But this way the app stores the datas only for the thread you are using.
Well, I had the problem because when I started a web browser task , the app deleted the datas I stored with PhoneApplicationService.Current.State. When I come back from the task, I was using the this code
string str = PhoneApplicationService.Current.State["some_key"];
But datas were already deleted, so it was trying to get the values, but couldn't achieve this, so it raised an exception.
This problem doesn't appear when navigating page to page, but when starting tasks.
So my suggestion is if you want to store datas, you could use IsolatedStorageSettings.ApplicationSettings. You can find a reference here.
It 's a good approach, because not only you store the datas for the moment, you can use the datas when you open the app after closing.
I am developing an WP8 app that uses a webbrowser control that shows statefull server content. On WP8 you can switch between apps manually. E.g. if you want to copy&paste some information from one app into a browser input field. If you switch that way, the current app instance stays alive. That means the web session and the current page of the browser control will stay available.
Now I want another app to send some data directly into the app with the browser control - without restarting it...
From what I know, there are three ways to handle inter app communication:
register a file type that will open the app by launching that file from local storage
register an app protocol and use Launcher.LaunchUriAsync() to submit parameters in a query string
use a shared storage file
Detailed information can be found here.
I think the last approach is not usefull, because after you have started the second app, there is now way to activate the calling app or is there any usefull way to reactivate the webbrowser app?
I tried the second approach, but I am running in an issue, because it starts a new instance by design. That means InitializePhoneApplication is called. There is the entry point for the custom UriMapper that reads the incoming parameters. So the old app instance is killed and all session data, cookies and input fields are gone. With WP webbrowser control you are not able to store the cookie and page state, so a fast app resume is not possible also.
private void InitializePhoneApplication()
{
if (this.phoneApplicationInitialized)
{
return;
}
RootFrame = new TransitionFrame();
RootFrame.Navigated += this.CompleteInitializePhoneApplication;
RootFrame.UriMapper = new AssociationUriMapper();
//...
this.phoneApplicationInitialized = true;
}
Is there any other way or a possibility to use the shown approaches to send data between apps without restarting them using LanchUri()?
That means, to send some data back to a running instance without reinitializing the whole app, so that the page state and session state are still available on the target app.
Best regards
Holger
FastAppResume is the solution. I haven't used it and thought it also reinitiates the app. But it doesnt. This example shows how to reuse the existing instance.
Regards
Holger
I am developing a simple application where in we have predefined quotas on usage for each user on the system..
and if the quota is up..the system should logoff of the user account..
this will happen if the application is allowed to run..
but if the user is closing the application on his own ..the app should automaticaly logoff the account..
i did exactly that in writing forced logoff code in form-closing event..
but if we are closing the app/process using the taskmanager..
the form closing event is not being called.
and so the user is able to continue even if his quota of time is up..
can anybody helpme out with this..
This is nice question.
Maybe you can make an applicaton to watch task manager and user status. I am not sure about that could help you but I gonna write
for ex
Process[] myApp = Process.GetProcesses("My Application");
if (myApp.Length == 0)
{
// App closed, and check the user status
// If user is still up, make it logoff,
// Also you can track processes with ProcessId too GetProcessesById(5415)
}
else
{
// App is running, there is no problem
}