C
C#2y 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
Jimmacle2y 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
ShinigamiOP2y 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
Jimmacle2y ago
you won't do anything in 30 minutes, but there are other things happening in the background to make the window "work"
Jimmacle
Jimmacle2y ago
Timer Class (System.Timers)
Generates an event after a set interval, with an option to generate recurring events.
Jimmacle
Jimmacle2y ago
there are multiple types of timer but this one should work for what you're doing
Shinigami
ShinigamiOP2y ago
Thank you, imma look into it 🙂
Jimmacle
Jimmacle2y ago
basically sleeping your main thread for that long will make the form look like it's not responding
Shinigami
ShinigamiOP2y ago
Right I'm hiding it before sleeping it tho I get what you're saying
Jimmacle
Jimmacle2y ago
i mean the OS will also think it's not responding if you look at the process in task manager or something
Shinigami
ShinigamiOP2y ago
Righttt, gotcha in task manager it might say not responding/ suspended And user might kill it too
Mayor McCheese
I concur timer is the right way to go
Shinigami
ShinigamiOP2y ago
Thanks guys, i did some researching and made it work
Want results from more Discord servers?
Add your server