-
Mar 25th, 2021, 12:49 AM
#1
[RESOLVED] Meny click runs above timer or waits?
Hi.
I have a forms.Timer and also a ContextMenuStrip.
When the timer ticks it runs a long sub() .
My question is, if I select something on the ContextMenuStrip, while the sub of the timer is running, will it wait for the sub to finish or interfere?
My 2 cents goes on waiting to finish since this is not a tread.timer but I'm not completely sure, since, for example if I show a messagebox , I can see that the timer is running on the background,
even if the messagebox halts the function until keypress. That is strange because I was under the impression that the messagbox runs on the UI thread and it will block the timer, but?...
If that is also the case when clicking a ToolStripMenuItem while the item is running then , how can I prevent this?
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 25th, 2021, 03:16 AM
#2
Re: Meny click runs above timer or waits?
If I understand you correctly you want to disable a ToolStripMenuItem while a timer is running, is this correct? If so, just set the ToolStripMenuItem's "Enabled" property to false to prevent clicking on it from doing anything.
-
Mar 25th, 2021, 09:58 AM
#3
Re: Meny click runs above timer or waits?
If that long running sub is executing on the UI thread at the time you attempt to click on the menu, you won't even be ABLE to click on the menu. In fact, the screen will appear to be frozen until the sub ends.
Any interaction with the UI triggers an event. The act of clicking on the menu triggers a click event. Those events just get posted to the message queue, and the message queue gets processed as soon as the UI thread is free to process them, at which point they are processed in the order they were received. So, if the Sub is occupying the UI thread, then the message will be posted to the queue, but it will just sit there until the UI thread is free. It will NOT interrupt executing code. This is true of timers, as well. All they are doing is adding a tick event to the message queue, where it will sit until the UI thread gets around to processing messages in the queue. If the UI thread is busy, those timer ticks won't happen, either. That's a major reason why timer ticks don't happen at consistent intervals: Something else is getting in the way, and it won't interrupt anything.
Of course, if the sub is running on a background thread, then the UI thread is free to process events, and will do so.
My usual boring signature: Nothing
-
Mar 25th, 2021, 11:50 AM
#4
Re: Meny click runs above timer or waits?
Shaggy that is the general idea I have also but what troubled me was the messagebox. When the messagbox is shown then the timer will tick and work fine. Isn't the messagebox on the UI thread? Isn't the timer on the UI thread? Why the timer is running? That's what troubled me initially.
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 25th, 2021, 12:04 PM
#5
Re: Meny click runs above timer or waits?
Originally Posted by sapator
Shaggy that is the general idea I have also but what troubled me was the messagebox. When the messagbox is shown then the timer will tick and work fine. Isn't the messagebox on the UI thread? Isn't the timer on the UI thread? Why the timer is running? That's what troubled me initially.
Thanks.
What kind of timer?
-
Mar 25th, 2021, 01:07 PM
#6
Re: Meny click runs above timer or waits?
Can't say I've ever tried that. While a messagebox is just sitting there, then nothing is happening on the UI thread, so it is free to process messages. In fact, it pretty much HAS to be able to process messages, or else how would it respond to the click of the button that hides the messagebox?
So, were you expecting that, with the modal messagebox preventing you from interacting with the background form, it would also prevent the timer from ticking?
My usual boring signature: Nothing
-
Mar 25th, 2021, 02:45 PM
#7
Re: Meny click runs above timer or waits?
OK.
To be on the safe side I stop the timer when the menu is visible and start it after but thanks for the info.
@dbasnett I specify on the first post that it's forms.timer
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|