I want the timer to continue when the app is pushed to the Background.
It seems the Timer pauses and resumes when the app is brought back to the forground.
_timer = new System.Timers.Timer();
_timer.Interval = 30000;
_timer.Elapsed += OnTimedEvent;
_timer.Enabled = true;
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
AutoSaveMethod();
}
Related
I am developing an UWP application, and one of my page is actually for user to take photo of them. In the page, I have timers for user to select before they take picture.
However, I wish to have a timer shown, counting down in the camera screen, so that the user know how much time is left for them to prepare, before the picture is taken.
Any idea on how I can do that? Thank you!
Just in case it is needed, here is my codes for the timers and the take picture buttons:
private async void PhotoButton_Click(object sender, RoutedEventArgs e)
{
//If preview is not running, no preview frames can be acquired
if (!_isPreviewing) return;
await Task.Delay(TimeSpan.FromSeconds(_seconds));
await TakePhotoAsync();
await GetPreviewFrameAsSoftwareBitmapAsync();
PreviewFrameBackground.Visibility = Visibility.Visible;
}
private void Timer_3sec_Click(object sender, RoutedEventArgs e)
{
Timer_5sec.Opacity = 0.2;
Timer_7sec.Opacity = 0.2;
Timer_3sec.Opacity = 1.0;
_seconds = 3;
}
private void Timer_5sec_Click(object sender, RoutedEventArgs e)
{
Timer_3sec.Opacity = 0.2;
Timer_7sec.Opacity = 0.2;
Timer_5sec.Opacity = 1.0;
_seconds = 5;
}
private void Timer_7sec_Click(object sender, RoutedEventArgs e)
{
Timer_3sec.Opacity = 0.2;
Timer_5sec.Opacity = 0.2;
Timer_7sec.Opacity = 1.0;
_seconds = 7;
}
You can use a DispatcherTimer to solve your problem.
Here a little code sample how you can do that (The sample dont show how to take the capture or to show the remaining seconds, just to calculate them!)
Class-Parameters:
private int _startTime;
private DispatcherTimer _timer = new DispatcherTimer();
Methods:
private void StartTimer()
{
_timer.Interval = TimeSpan.FromMilliseconds(500);
_timer.Tick += Timer_Tick;
_startTime = Environment.TickCount;
_timer.Start();
}
private void Timer_Tick(object sender, object e)
{
var remainingSeconds = _seconds - TimeSpan.FromMilliseconds(Environment.TickCount - _startTime).Seconds;
if(remainingSeconds <= 0)
{
_timer.Stop();
_timer.Tick -= Timer_Tick;
timerText.Text = "0 Seconds";
//Capture Image
} else
{
timerText.Text = "" + remainingSeconds + " Seconds";
}
}
You need to call the StartTimer-Method in you Click-Methods, after setting the _seconds.
I'm developing an application for UWP raspberry, I need to read input of a coin selectors via serial port, I followed the example https://github.com/ms-iot/samples/tree/develop/SerialSample / CS, but I can not fit it into a timer that waits for input from the coin selectors
Thanks
public MainPage()
{
var timer = new DispatcherTimer();
timer.Tick += DispatcherTimerEventHandler;
timer.Interval = new TimeSpan(0, 0, 0, 1);
timer.Start();
}
private void DispatcherTimerEventHandler(object sender, object e)
{
coin();
}
private void coin()
{
//Runs every second.
}
In MainPage I have one button. When user clicks on that button, I'm initializing the progress indicator variable. But it is not initializing and it showing null while debugging.
So, how should we show Progress Indicator in windows phone 8 for some task.
Below is my code.
ProgressIndicator pi;
private void search_button_clicked(object sender, RoutedEventArgs e)
{
pi = Microsoft.Phone.Shell.SystemTray.ProgressIndicator;
//Show the indicator
pi.IsVisible = true;//Here I'm getting null reference exception
........here the code for download xml file by calling web service............
}
I don't understand why Progress Indicator variable is not initializing.
you may try this ...
private void ShowProgressIndicator(String msg)
{
if (ProgressIndicator == null)
{
ProgressIndicator = new ProgressIndicator();
ProgressIndicator.IsIndeterminate = true;
}
ProgressIndicator.Text = msg;
ProgressIndicator.IsVisible = true;
SystemTray.SetProgressIndicator(this, ProgressIndicator);
}
private void HideProgressIndicator()
{
ProgressIndicator.IsVisible = false;
SystemTray.SetProgressIndicator(this, ProgressIndicator);
}
and then in your button click
private void search_button_clicked(object sender, RoutedEventArgs e)
{
ShowProgressIndicator("Searching");
}
i hope this might help you .....
I have the below code which works fine except that it gives the last location details it tracked when application is restarted.
And it gives the correct location details after I click on btnGetLocationDetails button 2 or 3 times.
Any idea how I can fix this issue so that every time app is launched user doesn't need to click on this button 2 or 3 times to get the correct current location?
Code:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher = null;
Location location=null;
private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.Permission == GeoPositionPermission.Granted)
{
watcher.MovementThreshold = Convert.ToDouble("100");
}
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);
// PositionChanged events occur whenever your position changes
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
watcher.Start();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//get the coordinates
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
location.Altitude = e.Position.Location.Altitude;
}
void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled)
MessageBox.Show("The location service is turned off.");
else if (e.Status == GeoPositionStatus.NoData)
MessageBox.Show("No location data is available. ");
}
}
Trying to test my WP7 app that uses location following this tutorial.
I have additional tools open, start the emulator from VS, let the app launch and then I place a pin in Live-mode in the Additional Tools Location utility, but no event is fired.
Is there anything wrong with my code?
public MainPage()
{
InitializeComponent();
InitWatcher();
}
private void InitWatcher()
{
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
}
private void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var lol = e;
}
private void geoWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
var FK = e;
}
The problem is that you have to start the GeoCoordinateWatcher:
geoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
geoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(geoWatcher_StatusChanged);
geoWatcher.Start();
Do you need to call the Start method on your GeoCoordinateWatcher instance?
http://msdn.microsoft.com/en-us/library/ee808853.aspx