I'm making a WPF application to simulate traffic. I want the Cars to have a reaction delay of 1 second befor changing their acceleration, without stopping the whole application. To do so, I want to acces the elapsed variable from my Car class. The elapsed variable stores how much time has passed.
The code in MainWindow:
namespace TrafficTester
{
public partial class MainWindow : Window
{
Timer timer = new Timer();
public MainWindow()
{
InitializeComponent();
//create the timer
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Interval = timerInterval;
timer.Enabled = true;
//...
void OnTimedEvent(object source, ElapsedEventArgs e)
{
timer.Enabled = false; //stop timer whilst updating, so updating won't be called again before it's finished
update(); //
timer.Enabled = true;
elapsed += timerInterval;
}
}
}
The code in the Car class:
namespace TrafficTester
{
public class Car
{
//...
public void changeAccel(double val)
{
int time = MainWindow.elapsed;
int stop = MainWindow.elapsed + reactDelay;
while (time < stop)
{
time = MainWindow.elapsed;
}
accel = val;
}
}
}
accel is the current acceleration and val is the new acceleration. MainWindow.elapsed should call the elapsed variable from MainWindow, but it doesn't. How can I call it from the Car class?
I saw at least 2 problems:
- If you want to access the timer intance, it needs to be public.
- Then you can access it via an instance of your Mainwindow.
To get the elapsed time, like you probably want, you need to go from you ElapsedEventHandler and do the timing action there!
public partial class MainWindow : Window
{
public System.Timers.Timer myTimer = new System.Timers.Timer();
public MainWindow()
{
//create the timer
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); // Where is it?
myTimer.Interval = 5;
myTimer.Enabled = true;
}
//...
void OnTimedEvent(object source, ElapsedEventArgs e)
{
myTimer.Enabled = false; //stop timer whilst updating, so updating won't be called again before it's finished
//update(); //
myTimer.Enabled = true;
// Timer.Elapsed += 5;
}
}
public class Car
{
public void changeAccel(double val)
{
var myWin = (MainWindow)Application.Current.MainWindow;
int time = myWin.myTimer.Elapsed; //<-- you cannot use it this way
}
}
Related
i want to run a task every 5 minutes. i've tried to solve it with an IntentService and AlarmManager, my code:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var tkrServiceIntent = new Intent(this, typeof(GpsDataHandler));
var tkrServicePendingIntent = PendingIntent.GetService(this, 0, tkrServiceIntent, 0);
long interval = 5000;
var firstStart = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) + 1000;
var am = (AlarmManager)GetSystemService(Context.AlarmService);
am.SetInexactRepeating(AlarmType.RtcWakeup, firstStart, interval, tkrServicePendingIntent);
Toast.MakeText(this, "Service started", ToastLength.Long).Show();
}
i receive the toast, that the service is started, but if i look in running services, there is no service for my application. Can you tell me where the problem ist?
IntentService in an "activity" (if we can call it) runing in Background of the app, so finnally it will call the OnDestroy() ..
You can use the timer to fix your problem , like :
using System;
using System.Threading;
class TimerExampleState {
public int counter = 0;
public Timer tmr;
}
class App {
public static void Main() {
TimerExampleState s = new TimerExampleState();
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus);
// Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s, 1000, 1000);
// Keep a handle to the timer, so it can be disposed.
s.tmr = timer;
// The main thread does nothing until the timer is disposed.
while (s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate.
static void CheckStatus(Object state) {
TimerExampleState s = (TimerExampleState) state;
s.counter++;
Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
if (s.counter == 5) {
// Shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);
Console.WriteLine("changed...");
}
if (s.counter == 10) {
Console.WriteLine("disposing of timer...");
s.tmr.Dispose();
s.tmr = null;
}
}
}
Source : https://developer.xamarin.com/api/type/System.Threading.Timer/
Hope this code helps you:-
async void StartTimer()
{
await Task.Delay(60000); //60 seconds
// Do your code
StartTimer(); // Again Call
}
Call "StartTimer()" method where you want to. Call only once time then it calls automatically after 60 seconds.
Thanks !!!
you can create your own timer using xamarin forms device class
sample timer class:
public class Timer {
public Timer(int interval)
{
_interval = TimeSpan.FromMilliseconds(interval);
}
private bool _isRunning;
private readonly TimeSpan _interval;
private Action Tick;
public void Start(Action tick)
{
_isRunning = true;
Tick = tick;
Xamarin.Forms.Device.StartTimer(_interval,() =>
{
Tick?.Invoke();
return _isRunning;
});
}
public void Stop()
{
_isRunning = false;
Tick = null;
}
}
Create a service class. Call DoWork method in OnStartCommand method. Check whether the log is getting printed after every 5 seconds.
public void DoWork()
{
var t = new Thread(() =>
{
while (true)
{
Log.Debug("Service", "Service running");
Thread.Sleep(5000);
}
});
t.Start();
}
I have to create several timers dynamically but I need the name of them when they fire.
Here is my code:
timersDict = new Dictionary<string, System.Timers.Timer>();
int i = 0;
foreach (var msg in _messages.CurrentMessages)
{
var timer = new System.Timers.Timer();
timer.Interval = int.Parse(msg.Seconds);
timer.Elapsed += Main_Tick;
timer.Site.Name = i.ToString();
i++;
}
I thought I could set it from timer.Site.Name but I get a null exception on Site.
How do I set the name of the timer I am creating?
EDIT
I am creating this since I need to know what timer is firing when it gets to the elapsed event. I have to know what message to display once it fires.
Can I pass the message with the timer and have it display the message based on what timer it is?
Here is the rest of my code:
void Main_Tick(object sender, EventArgs e)
{
var index = int.Parse(((System.Timers.Timer)sender).Site.Name);
SubmitInput(_messages.CurrentMessages[index].MessageString);
}
I was going to use it's name to tell me which timer it was so I know what message to display since all of the timers are created dynamically.
I'd recommend wrapping the System.Timers.Timer class and add your own name field.
Here is an example:
using System;
using System.Collections.Generic;
using System.Threading;
namespace StackOverFlowConsole3
{
public class NamedTimer : System.Timers.Timer
{
public readonly string name;
public NamedTimer(string name)
{
this.name = name;
}
}
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
var timer = new NamedTimer(i.ToString());
timer.Interval = i * 1000;
timer.Elapsed += Main_Tick;
timer.AutoReset = false;
timer.Start();
}
Thread.Sleep(11000);
}
static void Main_Tick(object sender, EventArgs args)
{
NamedTimer timer = sender as NamedTimer;
Console.WriteLine(timer.name);
}
}
}
I'm curious as to why dispatcher timer doesn't work in console mode. I created a simple alarm that does something when the timer reaches it's limit.
Can you use dispatcher timer with UnitTest or in Console mode?
DailyAlarm works when I run it in a form.
Here's my code to call the timer
[TestClass]
public class UnitTest1
{
bool runTest = true;
[TestMethod]
public void TestDailyAlarm()
{
DateTime alarmTime = new DateTime();
alarmTime= DateTime.Now;
alarmTime = alarmTime.AddSeconds(5);
// MessageBox.Show(alarmTime.ToString());
DailyAlarm alarm = new DailyAlarm(alarmTime);
alarm.DailyAlarmEvent += alarm_DailyAlarmEvent;
alarm.Start();
while (runTest)
{
System.Threading.Thread.Sleep(1000);
}
}
void alarm_DailyAlarmEvent(EventArgs e)
{
MessageBox.Show("Alarm On");
runTest = false;
}
}
Here's my timer code
public class DailyAlarm
{
#region Timer
DispatcherTimer timer;
#endregion
#region AlarmTime
DateTime _alarmTime;
#endregion
#region Event
public delegate void DailyAlarmHandler(EventArgs e);
public event DailyAlarmHandler DailyAlarmEvent;
#endregion
public DailyAlarm(System.DateTime alarmTime)
{
if (alarmTime < DateTime.Now)
{
alarmTime = alarmTime.AddDays(1);
}
_alarmTime = alarmTime;
TimeSpan timeRemaining = alarmTime.Subtract(DateTime.Now);
timer = new DispatcherTimer();
timer.Tick += AlarmEvent;
timer.Interval = timeRemaining;
}
public void Start()
{
timer.Start();
}
private void AlarmEvent(object sender, EventArgs e)
{
DailyAlarmEvent(null);
// Calculate next Alarm
_alarmTime = _alarmTime.AddDays(1);
TimeSpan timeRemaining = _alarmTime.Subtract(DateTime.Now);
Utilities.DispatcherTimer_ChangeInterval(ref timer, timeRemaining);
}
public void Stop()
{
if (timer != null)
timer.Stop();
}
}
The console and unit test environment by default don't have a dispatcher to run your dispatcher timer.
You can still use Dispatcher.CurrentDispatcher to create a Dispatcher to run your code.
There's an example of its usage at http://consultingblogs.emc.com/crispinparker/archive/2010/10/22/unit-testing-a-wpf-dispatchertimer-method.aspx
With this DispatcherHelper you can test your code with:
[TestMethod]
public void TestMethod1()
{
Action test = () =>
{
var dailyAlarm = new DailyAlarm(DateTime.Now.AddSeconds(5.0));
dailyAlarm.DailyAlarmEvent += dailyAlarm_DailyAlarmEvent;
dailyAlarm.Start();
};
DispatcherHelper.ExecuteOnDispatcherThread(test, 20);
}
void dailyAlarm_DailyAlarmEvent(EventArgs e)
{
// event invoked when DispatcherTimer expires
}
DispatcherTimer fires its Tick event on the UI thread. And you are running your code in a console mode. That's is the answer, I think!
I am trying to wrap my head around threading in C# but I am having difficulty implementing this behavior.
I need a simple yes/no dialog that returns DialogResult.No when 30 seconds are passed.
What I've got so far is this:
Thread th = new Thread(() =>
{
try
{
result = message.ShowDialog();
}
catch (Exception)
{
}
});
th.Start();
Thread.Sleep(30000);
th.Abort();
When I select Yes or No on the dialog it still waits out the 30 seconds, I need the thread to stop when response is received.
This is probably a no brainer but I'm relatively new to C# and could really use some help on this.
You could use a Timer when you initialize your Form.
When the timer expired, you close your Form.
Timer time1 = new Timer();
time1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
time1.Interval = 30000; // 30 secs
...
time1.Enabled = true; // Start the timer
message.ShowDialog();
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Close your Form
message.Close();
// Maybe you could set a variable, that indicates you, that the timer timed out
}
You have to overwrite ShowDialog() and ShowDialog(owner). When your show dialog will be called you start a timer and forward to the base.ShowDialog(). When the timer raises the tick event simply call Close().
Here an example implementation:
public partial class FormTimed : Form
{
private String _OriginalText;
private DateTime _StartTime;
private Timer _Timer;
public FormTimed()
{
InitializeComponent();
InitializeTimer();
Duration = TimeSpan.FromSeconds(10);
}
[DefaultValue(typeof(TimeSpan), "00:00:10")]
public TimeSpan Duration { get; set; }
public override string Text
{
get
{
return _OriginalText;
}
set
{
_OriginalText = value;
base.Text = value;
}
}
public void DisableTimer()
{
_Timer.Stop();
base.Text = _OriginalText;
}
public void ResetTimer()
{
_StartTime = DateTime.UtcNow;
_Timer.Start();
}
public new DialogResult ShowDialog()
{
StartTimer();
return base.ShowDialog();
}
public new DialogResult ShowDialog(IWin32Window owner)
{
StartTimer();
return base.ShowDialog(owner);
}
private void InitializeTimer()
{
_Timer = new Timer();
_Timer.Interval = 100;
_Timer.Tick += OnTimerTick;
}
private void OnTimerTick(object sender, EventArgs e)
{
var finishTime = _StartTime + Duration;
var remainingDuration = finishTime - DateTime.UtcNow;
if (remainingDuration < TimeSpan.Zero)
{
Close();
}
base.Text = _OriginalText + " (" + (int)remainingDuration.TotalSeconds + ")";
}
private void StartTimer()
{
_StartTime = DateTime.UtcNow;
_Timer.Start();
}
}
I have a maze game and I'm trying to create two Timers at a time.
1st (Exits the game after 300 secs)
t1.Interval = 30000;
t1.Enabled = true;
t1.Elapsed += new ElapsedEventHandler(hiddenTimer);
public static void hiddenTimer(object source, ElapsedEventArgs e)
{
Console.Clear();
Environment.Exit(1);
}
2nd (Displays the time remaining every 1 sec (like a real timer))
t2.Interval = 1000;
t2.Enabled = true;
t2.Elapsed += new ElapsedEventHandler(showTimer);
public static void showTimer(object source, ElapsedEventArgs e)
{
Console.Write(timeLeft);
}
I would want to pass declare timeLeft globally but it says that "An object reference is required for the non-static field, method, or property..."
How would I declare it properly?
By making a static property:
public static Double TimeLeft { get; set; }
This is if you want to Publicliy accessable from your entire context, if you want it private, just change public to private.
Just a side note, the built in Timer doesn't support polling for the remaining time until the next elapse. Either you decrease TimeLeft in each Elapse-event on the 1sec timer or you can have a look at this.
Edit
Here is one way to do it with one timer, first I declare two properties and one constant field that I use, don't bother that they are static, it's just easier to run it as a console application this way.
public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }
private const double CycleInterval = 1000;
Then in my Main-method I have the following to initiate my Timer
SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;
SystemTimer.Start();
Having this, the Cycle-event handler can look like this:
static void Cycle(object sender, ElapsedEventArgs e)
{
Elapsed += CycleInterval;
if ((Elapsed%5000) == 0.0)
{
Console.WriteLine("5 sec elapsed!");
// Do stuff each 5 sec
}
if ((Elapsed % 10000) == 0.0)
{
Console.WriteLine("10 sec elapsed!");
// Do stuff each 10 sec
}
Console.WriteLine("Elapsed: {0}", Elapsed);
}
You could also have Elapsed being a TimeSpan, but you can refactor this as you like.
Here's my complete source code that I used:
using System;
using System.IO;
using System.Timers;
namespace ConsoleApplication5
{
class Program
{
public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }
private const double CycleInterval = 1000;
static void Main(string[] args)
{
SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;
SystemTimer.Start();
while (true) ;
}
static void Cycle(object sender, ElapsedEventArgs e)
{
Elapsed += CycleInterval;
if ((Elapsed%5000) == 0.0)
{
Console.WriteLine("5 sec elapsed!");
// Do stuff each 5 sec
}
if ((Elapsed % 10000) == 0.0)
{
Console.WriteLine("10 sec elapsed!");
// Do stuff each 10 sec
}
Console.WriteLine("Elapsed: {0}", Elapsed);
}
}
}
And this is what it looks like when I run it:
First of all, you should declare your timeLeft as a static if you want it to behave like a global variable.
Secondly I'd use one timer and keep track of the time separately for each event:
static DateTime startTime = DateTime.Now;
static DateTime lastTime = DateTime.Now;
In your timer, which should be set to something to give more accuracy like 1/10 of a second, do this:
if (DateTime.Now - lastTime > new TimeSpan(0, 0, 1))
// Update the time here for your 1s clock
lastTime = DateTime.Now;
if (DateTime.Now - startTime > new TimeSpan(0, 0, 300))
// Exit the game
Your timings will be more accurate this way.
Mark it static:
public static int TimeLeft;
Your timeLeft memeber is not static.
Make it static or make showTimer method non static.
Regards.