I am developing application in windows phone 10
For some reason, I must handle application state (go to background, enter foreground). I have handle event suspend and resume at App.xaml.cs but it does not works, OnSuspending and OnResuming are not reached. Please help me check my source code and show me how to handle those events.
Here is my code:
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
Application.Current.Resuming += new EventHandler<Object>(OnResuming);
}
private void OnSuspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}
private void OnResuming(object sender, object e)
{
// do some thing
}
You should use the Lifecycle Events dropdown of Visual Studio 2015. It lets you choose between Suspend, Resume or Suspend and Shutdown states.
Whenever you run your app in debug, it never goes to suspended state on its own.
Some doc here: https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/
You are subscribing to suspend event twice
this.Suspending += OnSuspending;
Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
better leave classic
this.Suspending += OnSuspending;
this.Resuming += App_Resuming;
And also same way add resuming event
private void App_Resuming(object sender, object e)
{
// TODO
}
Do you debug is suspend/resume event works like it's described in this article:
How to trigger suspend, resume, and background events for Windows Store apps in Visual Studio
Related
I have a C# Windows Form Application that has a menu called Start Download Files and thi menu have then two instantiated user controls(submenu, tab menus).
For each tab (user control) i have a download button and a timer that runs it every 5 minutes.
I am using a private background worker which is created every time the control loads and runs a method to start download the files. That gives me a lot of troubles which i still can't find a solution for because:
- when i enable the timer for both controls they start the download multiple times for each and i get into concurrency accessing the files or
- cross thread exceptions
Does someone experienced something similar and maybe can give me a hint?
My code looks like this:
public partial class ucGeneralInfo : UserControl
{
private BackgroundWorker backgroundWorker;
private void TimerDownloadFrequency_Tick(object sender, ElapsedEventArgs e)
{
if (backgroundWorker.IsBusy != true)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
backgroundWorker.RunWorkerAsync(Branch);
}
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Download_Process((string) e.Argument);
}
private void Download_Process(string Branch)
{
// copying files
// processing files
}
}
}
For concurrent access, use lock.
lock(fileLock)
{
wc.DownloadFileAsync (/*...*/);
}
For Cross-thread exception use Invoke .
Invoke(new MethodInvoker(delegate
{
timer.Enable = true;
}));
I am desperately trying to implement a "Session Inactivity Timeout" for an IT security policy.
sss
However, my inactivity reset logic isn't firing. What am I doing wrong? I can't attach a breakpoint due to Visual Studio crashing on attaching to Silverlight binary. I am wondering if the problem is something very subtle, such as at the time the App constructor is called, there is no "current RootVisual" yet. Tagging with #WPF as well since #Silverlight is dead/obsolescent. Sample code below.
namespace TestApp
{
public partial class App : Application
{
System.Windows.Threading.DispatcherTimer sessionTimer = new System.Windows.Threading.DispatcherTimer();
public App()
{
ResetSessionTimer();
this.RootVisual.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(rootVisual_MouseLeftButtonDown), true);
//this.RootVisual.MouseLeftButtonDown += rootVisual_MouseLeftButtonDown;
InitializeComponent();
}
private void ResetSessionTimer()
{
sessionTimer.Stop();
sessionTimer.Tick -= sessionTimer_Tick;
sessionTimer = new System.Windows.Threading.DispatcherTimer();
sessionTimer.Interval = TimeSpan.FromMinutes(1);
sessionTimer.Tick += sessionTimer_Tick;
sessionTimer.Start();
}
private void sessionTimer_Tick(object sender, EventArgs e)
{
System.Windows.Browser.HtmlPage.Document.GetElementById("LogoutButton").Invoke("click", null);
}
private void rootVisual_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// added this alert to see if code is even firing.
System.Windows.Browser.HtmlPage.Window.Invoke("alert", "hello, world");
ResetSessionTimer();
e.Handled = false;
}
}
}
I figured this out - somewhere in the code, the previous developer was overriding the this.RootVisual element with a new MainPage() in order to enable their homegrown dependency injection / MVVM architecture. I moved the logic from the App() constructor to right after this assignment operator, and it works like magic.
Unable to call this system event in Windows service, how can we call it in Windows service?
Microsoft.Win32.SystemEvents.TimeChanged += SystemEvents_TimeChanged;
void SystemEvents_TimeChanged(object sender, EventArgs e)
{
AnyMethodExample();
}
This event is only raised if the message pump is running. In a Windows
service, unless a hidden form is used or the message pump has been
started manually, this event will not be raised.
https://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.timechanged(v=vs.110).aspx
It depends where you are calling within windows service. You can post complete code for further support. Simply,
class Program
{
static void Main(string[] args)
{
SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
Console.ReadKey();
}
static void SystemEvents_TimeChanged(object sender, EventArgs e)
{
Console.WriteLine("TimeChanged: {0}", DateTime.Now);
}
}
Caution: Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
I am developing a WinRT app. One of the requirements is that the app should have a "timed logout" feature.
What this means is that on any screen, if the app has been idle for 10 mins, the app should logout and navigate back to the home screen.
The brute force way of doing this obviously would be to hook up pointer pressed events on every grid of every page and resetting the timer if any of these events is fired but I was wondering if there was a more elegant and more reliable way of doing this.
Thanks,
Rajeev
With the use of DispatcherTimer & several events you can achieve that.
DispatcherTimer Timer;
private void InitializeTimer()
{
Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;
Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
Timer = new DispatcherTimer();
Timer.Interval = TimeSpan.FromMinutes(10);
Timer.Tick += Timer_Tick;
Timer.Start();
}
private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
{
Timer.Start();
}
private void CoreWindow_PointerMoved(CoreWindow sender, PointerEventArgs args)
{
Timer.Start();
}
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
Timer.Start();
}
private void Timer_Tick(object sender, object e)
{
Timer.Stop();
//TODO: Do logout.
}
I'm not aware of anything built in, but rather than attaching to Grids and etc., I'd suggest you attach event handlers to the current CoreWindow (documentation) for the various types of events that you would need to track to determine idleness.
If you did attach to a Grid for example, you'd find controls that use Popup won't trigger the events. A ComboBox for example wouldn't be tracked by the event handlers.
For example, you might do this:
var core = CoreWindow.GetForCurrentThread();
core.KeyDown += (sender, kdArgs) => {
// reset timeout
};
core.PointerMoved = core.PointerMoved = (sender, pmArgs) {
// reset timeout (maybe with a bit of logic to prevent tiny mouse drift from
// causing false positives
};
// etc. (whatever else makes sense)
The code relies on the GetForCurrentThread call (documentation) which returns the instance of the CoreWindow that is the host for all content.
I want to save the application state when an metro style app receive the suspend event.
I found in this link the code below, but I donĀ“t find in .Net the class ApplicationStateModel:
public App()
{
InitializeComponent();
this.Suspending += new SuspendingEventHandler(App_Suspending);
this.Resuming += new Windows.UI.Xaml.EventHandler(App_Resuming);
}
void App_Resuming(object sender, object e)
{
// Write code to update ui only for items that are outdated.
// This is resume from suspended state, so it does not lose any data
}
async void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
// Write code to store data so that when the application is terminated the state can be recovered.
// Allowed only 5 seconds to do the storage
SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();
await **ApplicationStateModel**.SaveAllApplicationDataAsync();
await ApplicationStateModel.SaveSessionStateAsync();
deferral.Complete();
}
You can use Windows.Storage.ApplicationData to save local settings.
Take a look at the Sample SDK app for a running app that saves your settings.