CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: VB6 timer?

  1. #1
    Join Date
    Jan 2011
    Posts
    6

    VB6 timer?

    Hi,

    I want to ask a question about a timer because I could not be sure about the timer behavior with my own experiments.

    Assume that there is a long code (maybe 10 minutes) in a timer and fired. While running this timer, what happens if the user click another object (commandbutton, listbox, displaying modal form etc) on the form? Does the execution jumps to clicked process and resume back to the timer? or timer continue to work seperatelly? or exit from timer sub?

    Thank you in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VB6 timer?

    Timer should run in the background. (It's how a clock is update, which doesn't break windows)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB6 timer?

    Quote Originally Posted by hartoksi View Post
    Hi,

    I want to ask a question about a timer because I could not be sure about the timer behavior with my own experiments.

    Assume that there is a long code (maybe 10 minutes) in a timer and fired. While running this timer, what happens if the user click another object (commandbutton, listbox, displaying modal form etc) on the form? Does the execution jumps to clicked process and resume back to the timer? or timer continue to work seperatelly? or exit from timer sub?

    Thank you in advance
    So if I understand correctly you are saying that once the timer fires that there would be code within the timer that takes about 10 minutes to complete. If this be the case then that code will take 10 minutes to execute during which time you will be able to do nothing else in your program unless you make provisions for it.

    VB code runs linearly so once a sub or function begins it continues until it is done. A long running procedure will make your form unresponsive, make your UI not redraw and may cause the program to appear frozen and Windows to show it as not responding. This doesn't matter if it is in a timer or some other location as they all behave the same way.

    There is of course the DoEvents statement that can be placed in long running code which will allow other events to fire which basically will cause you code to pause at that point to check for other events and process them then continue. This statement has its uses but can have unexpected results especially when used in some event subs as it is possible that event could fire again and reenter the code before it is done what it was already doing.

    If you use a DoEvents in a timer sub it is a good idea to disable the timer at the top of the timer sub and then enable it at the bottom to insure that the timer does not try to fire the event again via DoEvents while it is processing
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jan 2011
    Posts
    6

    Re: VB6 timer?

    Thank you for your explanations.

    As far as I understand from DataMiser, if the code in the timer sub is fired and runing then we can not click a commandbox on the form until timer code reaches the end.

    I am not sure this is a general rule of VB. You may be right while a sub consist of loops (for-next or do/lop etc). But if the code does not have loops but millions of havy calculations then form object might be clickable.

    Am I right?

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB6 timer?

    Nope, it does not matter what the code is. VB6 creates single threaded apps and if you place code in a timer sub or function or anywhere when that code starts to execute the only way it pauses to allow anything else to happen is if you insert code to make it do so.

    For example if you had a few lines of code that do some heavy work and may take 10 minutes to complete no matter if it be in a timer sub, user sub or somewhere else int he code once this code is running you will not be able to register a click anywhere in your app. If you send you app to the back ground then bring I back to the front it will not refresh the screen and you will quite likely see the app as not responding if you look in the task manager and possibly even see it show as not responding on the title bar and this is because your code is running and not allowing any of the Windows messages through at the time.

    This is why DoEvents exists in VB but it comes with its own issues so use carefully.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jan 2011
    Posts
    6

    Re: VB6 timer?

    Many thanks for your explanations. I got what you mean. In this case I need use a method for paralel working. It might be background aplication or secondary exe which takes data from main exe via a file and process it and bounce the result in a same way to the main exe. Thus, main exe can behave freely during calculation.

    In my project, I am using CDO for sending mails. If the mail is associated with a big size attachments ıt takes long time. So I wanted to overcome this matter. I think above two options is the only way I can imagine now. Do you have any more idea?

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB6 timer?

    It is possible to do multi threading in VB6 though I have never tried it. I would use VB.Net which is more geared toward multi threading or a secondary program
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: VB6 timer?

    Multithreading is available in VB6 ... unfortunately, you need to do everything to create, start, marshal and cleanup the threads yourself using API's ...

    Also, it's not always very 'Thread Safe'....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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

Featured