C
C#•7mo ago
Shiv

Auto refresh inside windows form

Team , We are using windows form c#. Is there any library for auto refresh (need to few logic every 1 min. Please see the attachment ) . ? These setting is within the app. Thanks Shiv
No description
9 Replies
Dharmang
Dharmang•7mo ago
this should suffice.
var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));

while (await timer.WaitForNextTickAsync())
{
Console.WriteLine("Hello World after 10 seconds")
}
var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));

while (await timer.WaitForNextTickAsync())
{
Console.WriteLine("Hello World after 10 seconds")
}
Also this aint outlook bro 🤣
Shiv
Shiv•7mo ago
Thanks @Dharmang . It is working . Can we stop this ?
Dharmang
Dharmang•7mo ago
try timer.Dispose()
Shiv
Shiv•7mo ago
No timer.Dispose() is not working. It keeps on triggering every 10 secs @Dharmang
Dharmang
Dharmang•7mo ago
share the code
Shiv
Shiv•7mo ago
private async void checkBox2_CheckedChanged(object sender, EventArgs e) { var timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); if (checkBox2.Checked == true) { while (await timer.WaitForNextTickAsync()) { MessageBox.Show("Triggered"); } } else { timer.Dispose();
} } it is inside checkbox checked event inside windows form
Dharmang
Dharmang•7mo ago
can you also share the xaml code
public PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(3));

private async void CheckBox_Checked(object sender, RoutedEventArgs e)
{
while (await timer.WaitForNextTickAsync())
{
Debug.WriteLine("triggered");
}
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
timer.Dispose();
}
public PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(3));

private async void CheckBox_Checked(object sender, RoutedEventArgs e)
{
while (await timer.WaitForNextTickAsync())
{
Debug.WriteLine("triggered");
}
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
timer.Dispose();
}
this is winui3 code but similar
Shiv
Shiv•7mo ago
OK let me try this public PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(3)); Should this code go inside constructer? We need to trigger it somewhere right? timer.Dispose(); worked if I move this outside the checkbox changed event public PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(3));
but again it wont start if i enable it . public partial class Form1 : Form { PeriodicTimer timer; public Form1() { InitializeComponent(); } private async void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { timer = new PeriodicTimer(TimeSpan.FromSeconds(5)); while (await timer.WaitForNextTickAsync()) { MessageBox.Show("Triggered"); } } else { timer.Dispose(); } } } this worked ... there is no CheckBox_Unchecked in windows form so i am checking inside the main check changed event
Dharmang
Dharmang•7mo ago
ya this would work maybe dispose absolutely clears the initiated object
Want results from more Discord servers?
Add your server
More Posts