C
C#14mo ago
Shinigami

✅ best way to snooze winforms

I have a win forms app with a snooze button, once the snooze button is clicked i need to make the form disappear and make it appear after 30 mins and rn I'm doing a form.hide and thread.sleep(30 mins in millisecs). Is this the right way or is there anything else i can do here?
12 Replies
Jimmacle
Jimmacle14mo ago
thread.sleep bad, use a timer with a callback to show the form again otherwise you're blocking your form's thread for 30 minutes and if a message comes in for it to process it won't be able to
Shinigami
Shinigami14mo ago
Right, although in my case nothing will happen in 30 mins but i want to use best practices. How do i get started with timer?
Jimmacle
Jimmacle14mo ago
you won't do anything in 30 minutes, but there are other things happening in the background to make the window "work"
Jimmacle
Jimmacle14mo ago
Timer Class (System.Timers)
Generates an event after a set interval, with an option to generate recurring events.
Jimmacle
Jimmacle14mo ago
there are multiple types of timer but this one should work for what you're doing
Shinigami
Shinigami14mo ago
Thank you, imma look into it 🙂
Jimmacle
Jimmacle14mo ago
basically sleeping your main thread for that long will make the form look like it's not responding
Shinigami
Shinigami14mo ago
Right I'm hiding it before sleeping it tho I get what you're saying
Jimmacle
Jimmacle14mo ago
i mean the OS will also think it's not responding if you look at the process in task manager or something
Shinigami
Shinigami14mo ago
Righttt, gotcha in task manager it might say not responding/ suspended And user might kill it too
Mayor McCheese
Mayor McCheese14mo ago
I concur timer is the right way to go
Shinigami
Shinigami14mo ago
Thanks guys, i did some researching and made it work