How do I get timer to run independently & allow menu to activate something else

Posted on 2004-11-16
Last Modified: 2010-04-23
I have a form which immeiately starts a timer, it reads a database & compiles the number of pages for a report

The result of this, then changes the text on a menu to indicate the number of pages when the timer is finished

this timer can or cannot be time consuming depending on the number of pages it has to calculate

I want the timer to run independently, but it is stopping other functions that are in the menu

I get a message saying, 'not responding' on the form text, however this is resolved when the timer is finished

I want the user to be able to activate menu options whilst the timer is running until completion

I thought about adding a 2nd menu control, but it appears to be inactive, because when I run the app it is not visible, so I don't quite understand the rules for adding a second menu

any help much appreciated
Question by:bemson
    LVL 28

    Expert Comment

    YOu can rn timer in a different thread

    instead of

    (in the toolbox under "components", not "windows forms")

    The raised event will run in a different thread.


    Author Comment

    Thanks for your reply

     It sounds a logical solution, however,

    using the original concept I had where the timer activates on load of form 2, I still get the same effect with the component timer

    trying a new approach, I still get the same result
    form1 loads the form with the timer in it, so, to try & use another logic I started the timer in the 1st form (at it's conclusion), before loading the form that needed the result, I still get same result where I get the message 'Not responding' (when selecting the menu)

    it seems that nothing will activate on the form until the timer has finished.

    I have even put in doevents which does not seem to help

    thanks for taking an interest in my problem
    LVL 28

    Expert Comment

    What if you put timer completely into different thread

    Author Comment

    Thanks for your prompt reply

    I am not exactly sure of the term thread, I presumed it meant activating that process at a different logical point in the app which I thought I had done by getting the timer to run from form 1 instaed of form 2

    could you please enlighten me further on your interpretation of thread & how to achieve your solution
    LVL 28

    Accepted Solution

    here is tutorial on multythreading

     If your application is going to perform a task that could take a
    long time, such as parsing through a large recordset or performing a complex series
    of mathematical calculations, you can push that processing off to its own thread so
    that the rest of your application is still accessible. In VB6, , the best you could do to
    keep the rest of the application from appearing to be locked was to use the DoEvents
    Examine this code, which is written for VB.NET. Here you have some code for
    button4. This code calls the BeBusy routine, which has a loop in it to just to take up
    time. However, while in this loop, you are consuming the thread for this application,
    and the UI will not respond while the loop is running.
    The good news is the VB.NET IDE runs on
    a separate thread, so if you find yourself waiting forever, just click on the IDE and
    choose Stop Debugging from the Debug menu.

    Private Sub button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button4.Click
    End Sub

    Sub BeBusy()
    Dim i As Decimal
    For i = 1 To 10000000
    ‘do nothing but tie up app
    End Sub

    To create a thread, you must use the System.Threading.Thread class. In the creation
    of the class, you pass in the name of the procedure or method you want to run on that
    thread. You preface the procedure or method name with the AddressOf operator. Your
    code would look like this:

    Dim busyThread As New System.Threading.Thread(AddressOf BeBusy)

    To fix the code and keep BeBusy from consuming the main program thread, you have
    now created a new thread and will run BeBusy on that thread. However, that line of
    code isn’t enough. Next, you must call the Start method on that new thread. With
    VB.NET, calling BeBusy on its own thread would look like this:

    Private Sub button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button4.Click
    Dim busyThread As New System.Threading.Thread(AddressOf BeBusy)
    End Sub

    No changes are required to the BeBusy procedure. If you now run this code, the interface
    will remain active while BeBusy is running. The Beep in BeBusy will let you
    know when the procedure has finished running.

    Author Comment

    Thanks for your info re threads. I am using 2002

    I will try to relate to this new process (for me) & attempt to apply it & see what transpires

    I have to go out & will not be back for 7 hours, I will then attempt to implement your suggestion as described above, so I do not expect to be able to get back to you for about 24 hours

    your assistance is much appreciated.
    LVL 28

    Expert Comment

    take your time and good luck

    Author Comment

    I have been able to successfully implement your suggestion

    Your interest & information is much appreciated

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now