-
Hi, I'm trying to understand why these two variations on the same "concept" are not working identical. This first one is a timer which runs UpdateLabel and SetNeedsDraw() on that label. Instead of updating every 100 milliseconds it only updates if the mouse is over the window or moving or lost/got focus; and not at a rate of every 100ms. It's like its ignoring the SetNeedsDraw() request and only refreshing because of some other events firing. internal class Program
{
private static readonly System.Timers.Timer _timer = new(100) { Enabled = false };
private static readonly Window _window = new() { Title = "Test", Y = 1, Height = Dim.Fill(1) };
private static readonly Label _labelView = new() { X = 0, Y = 0, Width = Dim.Fill(), Height = Dim.Fill(1) };
static void Main(string[] args)
{
Application.Init();
_window.Add(_labelView);
UpdateLabel();
_timer.Elapsed += (s, e) =>
{
UpdateLabel();
_labelView.SetNeedsDraw();
};
_timer.Start();
Application.Run(_window);
_timer.Stop();
Application.Shutdown();
}
private static void UpdateLabel()
{
_labelView.Text = DateTime.Now.ToString("s.fff") + "\n";
for (int i = 0; i < 10; i++)
_labelView.Text += new string(' ', i) + DateTime.Now.ToString("T") + "\n";
}
} Versus this second one; which instead of the timer, uses Application.AddTimeout and updates the window every 100 milliseconds as expected. So wondering why the timer based approach doesnt give the same result as the below. internal class Program
{
private static readonly Window _window = new() { Title = "Test", Y = 1, Height = Dim.Fill(1) };
private static readonly Label _labelView = new() { X = 0, Y = 0, Width = Dim.Fill(), Height = Dim.Fill(1) };
static void Main(string[] args)
{
Application.Init();
_window.Add(_labelView);
UpdateLabel();
Application.AddTimeout(TimeSpan.FromMilliseconds(100),
() =>
{
UpdateLabel();
return true;
});
Application.Run(_window);
Application.Shutdown();
}
private static void UpdateLabel()
{
_labelView.Text = DateTime.Now.ToString("s.fff") + "\n";
for (int i = 0; i < 10; i++)
_labelView.Text += new string(' ', i) + DateTime.Now.ToString("T") + "\n";
}
} Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Because _timer.Elapsed += (s, e) =>
{
Application.Invoke(() => {
UpdateLabel();
_labelView.SetNeedsDraw();
});
}; |
Beta Was this translation helpful? Give feedback.
Because
Application.AddTimeout
force an iteration which will redraw again. For the timer work as well you have to callApplication.Invoke
inside theElapsed
event.