i wanna know the current system state whether its suspend or resume. i already tried SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
log.DebugFormat("System Power event {0}", e.Mode.ToString());
}
i wanna fetch the status of a system on frequent interval. i dont wanna use WMI query also. is there any other way to detect the system state.
Then, you can use SystemEvents.PowerModeChanged event to know whether the system is getting suspended (When you hibernate or sleep the suspend will happen).
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
private static void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
MessageBox.Show("PowerMode: OS is resuming from suspended state");
break;
case PowerModes.Suspend:
MessageBox.Show("PowerMode: OS is about to be suspended");
break;
}
}
Hope this helps.
Related
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
here's what I've done in my universal windows app:
public MainPage()
{
InitializeComponent();
private LockApplicationHost lol=LockApplicationHost.GetForCurrentView();
}
private async void Lol_Unlocking(LockApplicationHost sender, LockScreenUnlockingEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
alarm.Pause();
Status.Text = "lolwtf";
});
}
I'm trying to know when the user unlocks his computer.
EDIT: also the error I keep getting is:
Delegate to an instance method cannot have null 'this'. and it highlights:
lol.Unlocking += Lol_Unlocking;
I'm trying to know when the user unlocks his computer.
You can hookup a SessionSwitchEventHandler. Obviously your application will need to be running. SessionSwitchEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event.
Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
//I left my desk
}
else if (e.Reason == SessionSwitchReason.SessionUnlock)
{
//I returned to my desk
}
}
You can have a look on the SessionSwitchReason Enumeration to find more about the uses the SessionSwitchReason class to represent the type of a session switch event.
lol.Unlocking += Lol_Unlocking;
should be lol.Unlocking += Lol_Unlocking(EventHandler_Unlocking); and EventHandler_Unlocking has to be defined in the program.
my understanding for LockApplicationHost.Unlocking is that it helps to unlock and lock the device whereas to determine if the device is unlocked and unlocked SessionSwitchEventHandler will be better approach. For more understanding on the LockApplicationHost.Unlocking check this
In my app, I want to be know when play state changes. But I don't know how to subscribe to the event and get the current state. How can I do that? thanks.
I see an statement in MSDN, but couldn't understand what it means and how to implement it:
In Windows Phone 8, you can check the PlayStateChangedEventArgs to
determine both the CurrentPlayState and the IntermediatePlayState that
occurred before the audio player entered the current play state.
Details:
in the main page I do this:
public MainPage()
{
BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(Instance_PlayStateChanged);
}
then
private void Instance_PlayStateChanged(object sender, EventArgs e)
{
var playerState = BackgroundAudioPlayer.Instance.PlayerState;
}
But I feel this is not the correct way to use event and eventargs. it also doesn't give me the correct latest value.
The PlayerStateChanged event is definitely the right way to determine changes, but it won't fire when you subscribe to it so you won't get the current state. Try something like this instead:
BackgroundAudioPlayer audioPlayer = BackgroundAudioPlayer.Instance;
public MainPage()
{
audioPlayer += OnPlayStateChanged;
OnPlayStateChanged(audioPlayer.PlayerState);
}
private OnPlayStateChanged(object sender, EventArgs e)
{
OnPlayStateChanged(audioPlayer.PlayerState);
}
private OnPlayStateChanged(PlayState state)
{
// Process state here
}
Having said that, there are two major things worth pointing out.
Firstly, BackgroundAudioPlayer is an extremely volatile API. It will commonly throw exceptions if not in the correct internal state. Feel free to use the extension methods I developed for Podcaster: https://gist.github.com/richardszalay/8552812
Secondly, PlayerStateChanged is not fired when the playback position changes. For that, I'd recommend using a DispatcherTimer and updating your display via my TryGetPosition method (but only when GetTrackOrDefault() returns non-null). I'd also recommend using a sub-second timer (200-300ms) to keep the "ticking" correct. When the PlayerState changes to FastForwarding or Rewinding, update the timer to 20-30ms, and restore it when it returns to Playing.
Use this solution as well as link you would get solution:-
enter link description here
void Instance_PlayStateChanged(object sender, EventArgs e)
{
switch (BackgroundAudioPlayer.Instance.PlayerState)
{
case PlayState.Playing:
playButton.Content = "pause";
break;
case PlayState.Paused:
case PlayState.Stopped:
playButton.Content = "play";
break;
}
if (null != BackgroundAudioPlayer.Instance.Track)
{
txtCurrentTrack.Text = BackgroundAudioPlayer.Instance.Track.Title +
" by " +
BackgroundAudioPlayer.Instance.Track.Artist;
}
}
Thread.Suspend() method is obsolete as you know. I want to suspend thread immidiately when button click event comes. I used Thread.Suspend() and it works perfect but everyone suggest that using Thread.Suspend() method is not a good method to suspend the task. I used a flag to suspend the task but every time when a button click event comes, i have to wait to exit from task. I used Thread.IsAlive flag to wait the thread to exit but this method freezes form.
void ButtonClickEvent(object sender, ButtonClickEventArgs e)
{
TheadExitFlag = false;
if(MyThread != null)
{
while(MyThread.IsAlive);
//MyThread.Suspend();
}
}
void MyTask(void)
{
while(TheadExitFlag)
{
// some process
Thread.Sleep(5000);
}
}
How can i suspend the thread immidiately?
There is no alternative with the same functionality, AFAIK. I am not sure if the problems with an OS Suspend() could be worked around within the language/libraries, but no attempt has been made to do so, so maybe it's too difficult or even sensibly impossible.
Until such an alernative exists, you are reduced to polling for a suspend flag and then waiting on some synchronization object for a 'resume' signal. I have used AutoResetEvent for this.
Use a ManualResetEvent to toggle between a running and idle state.
ManualResetEvent run = new ManualResetEvent(true);
void ResumeButton_Click(object sender, ButtonClickEventArgs e)
{
run.Set();
PauseButton.Enabled = true;
ResumeButton.Enabled = false;
}
void PauseButton_Click(object sender, ButtonClickEventArgs e)
{
run.Reset();
PauseButton.Enabled = false;
ResumeButton.Enabled = true;
}
void MyTask(void)
{
while (run.WaitOne()) // Wait for the run signal.
{
// Do work here.
}
}
There is an approximate solution to this problem at https://stackoverflow.com/a/45786529/3013473 using AspectJ and wait/notify.
I want to make my program aware of the computer being put to sleep or waking up from sleep, possibly have an event that is triggered when either of these occur. Is this possible?
You can subscribe to the SystemEvents.PowerModeChanged event.
SystemEvents.PowerModeChanged += OnPowerChange;
void OnPowerChange(Object sender, PowerModeChangedEventArgs e) {
switch ( e.Mode ) {
case PowerModes.Resume:
...
case PowerModes.Suspend:
...
}
}