Results 1 to 7 of 7

Thread: [RESOLVED] Meny click runs above timer or waits?

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Resolved [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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  4. #4

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Meny click runs above timer or waits?

    Quote Originally Posted by sapator View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  7. #7

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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
  •  



Click Here to Expand Forum to Full Width