How listen timer tick globally?
I want to have a single main timer in my wpf c# app, that´s counting/running independent of current shown page.
My software create different custom controls at truntime, and inside of that controls I need the ability of listen the main tick.
namespace window6
{
public partial class Window6 : Window
{.......
I already try this:
public static DispatcherTimer GlobalTimer = new DispatcherTimer();
GlobalTimer.Interval = TimeSpan.FromMilliseconds(500);
GlobalTimer.IsEnabled = true;
Then on any custom control I fire a timer event:
window6.Window6.GlobalTimer.Tick += (global_Elapsed);
The problem is this code act like a new timer on every custom control I use, so every timer event is fired inside every custom control, run as a new timer running not in sync. Not act as a single timer.
c# wpf winforms
add a comment |
I want to have a single main timer in my wpf c# app, that´s counting/running independent of current shown page.
My software create different custom controls at truntime, and inside of that controls I need the ability of listen the main tick.
namespace window6
{
public partial class Window6 : Window
{.......
I already try this:
public static DispatcherTimer GlobalTimer = new DispatcherTimer();
GlobalTimer.Interval = TimeSpan.FromMilliseconds(500);
GlobalTimer.IsEnabled = true;
Then on any custom control I fire a timer event:
window6.Window6.GlobalTimer.Tick += (global_Elapsed);
The problem is this code act like a new timer on every custom control I use, so every timer event is fired inside every custom control, run as a new timer running not in sync. Not act as a single timer.
c# wpf winforms
add a comment |
I want to have a single main timer in my wpf c# app, that´s counting/running independent of current shown page.
My software create different custom controls at truntime, and inside of that controls I need the ability of listen the main tick.
namespace window6
{
public partial class Window6 : Window
{.......
I already try this:
public static DispatcherTimer GlobalTimer = new DispatcherTimer();
GlobalTimer.Interval = TimeSpan.FromMilliseconds(500);
GlobalTimer.IsEnabled = true;
Then on any custom control I fire a timer event:
window6.Window6.GlobalTimer.Tick += (global_Elapsed);
The problem is this code act like a new timer on every custom control I use, so every timer event is fired inside every custom control, run as a new timer running not in sync. Not act as a single timer.
c# wpf winforms
I want to have a single main timer in my wpf c# app, that´s counting/running independent of current shown page.
My software create different custom controls at truntime, and inside of that controls I need the ability of listen the main tick.
namespace window6
{
public partial class Window6 : Window
{.......
I already try this:
public static DispatcherTimer GlobalTimer = new DispatcherTimer();
GlobalTimer.Interval = TimeSpan.FromMilliseconds(500);
GlobalTimer.IsEnabled = true;
Then on any custom control I fire a timer event:
window6.Window6.GlobalTimer.Tick += (global_Elapsed);
The problem is this code act like a new timer on every custom control I use, so every timer event is fired inside every custom control, run as a new timer running not in sync. Not act as a single timer.
c# wpf winforms
c# wpf winforms
edited Nov 17 '18 at 13:54
Akspa
asked Nov 17 '18 at 13:43
AkspaAkspa
235
235
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can't be referencing the same timer, somehow.
I created a static class:
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
I then added a simple window with animation:
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
and
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
in Mainwindow I added a button to instantiate and show multiple window1
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
When I spin that up and click the button several times I have 3 instances of window1 and their animations are in sync.
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351792%2fhow-listen-timer-tick-globally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't be referencing the same timer, somehow.
I created a static class:
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
I then added a simple window with animation:
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
and
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
in Mainwindow I added a button to instantiate and show multiple window1
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
When I spin that up and click the button several times I have 3 instances of window1 and their animations are in sync.
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
add a comment |
You can't be referencing the same timer, somehow.
I created a static class:
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
I then added a simple window with animation:
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
and
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
in Mainwindow I added a button to instantiate and show multiple window1
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
When I spin that up and click the button several times I have 3 instances of window1 and their animations are in sync.
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
add a comment |
You can't be referencing the same timer, somehow.
I created a static class:
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
I then added a simple window with animation:
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
and
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
in Mainwindow I added a button to instantiate and show multiple window1
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
When I spin that up and click the button several times I have 3 instances of window1 and their animations are in sync.
You can't be referencing the same timer, somehow.
I created a static class:
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
I then added a simple window with animation:
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
and
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
in Mainwindow I added a button to instantiate and show multiple window1
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
When I spin that up and click the button several times I have 3 instances of window1 and their animations are in sync.
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
using System.Timers;
namespace wpf_GlobalTimer
{
public static class TimerParent
{
public static Timer GlobalTimer { get; set; } = new Timer(3000)
{
AutoReset = true,
Enabled = true
};
}
}
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
<Window.Resources>
<Storyboard x:Key="TestStoryboard">
<DoubleAnimation
From="200"
To="0"
Storyboard.TargetProperty="(Rectangle.Height)"
Storyboard.TargetName="Rect"
FillBehavior="Stop"
Duration="0:0:2"
/>
</Storyboard>
</Window.Resources>
<Grid>
<Rectangle Name="Rect" Height="200" Fill="Green"/>
</Grid>
</Window>
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
public partial class Window1 : Window
{
private Timer timer = null;
private Storyboard sb = null;
public Window1()
{
InitializeComponent();
timer = TimerParent.GlobalTimer;
timer.Elapsed += OnTimedEvent;
sb = this.Resources["TestStoryboard"] as Storyboard;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(new Action(() =>
{
sb.Begin();
}));
}
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window win = new Window1();
win.Show();
}
answered Nov 17 '18 at 16:25
AndyAndy
3,0681106
3,0681106
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
add a comment |
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Thanks so much !!!! I love when person like you take the time to explain with a very super clear with a working example!!! Thanks thanks thanks!!
– Akspa
Nov 17 '18 at 17:38
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
Note that i had limited time to do this so it's a bit quick n dirty. Dispatchertimer fires on the ui thread and you could maybe avoid that invokeasync stuff with that.
– Andy
Nov 17 '18 at 18:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351792%2fhow-listen-timer-tick-globally%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown