Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Most Efficient Way to Keep Module from Exiting

Posted on 2006-07-17
4
Medium Priority
?
166 Views
Last Modified: 2010-04-23
I have a program that runs as a service so there is no UI. It checks for a file every 30 seconds and does it thing to the file if it finds one, if not it waits for another 30 seconds. I have the timer portion worked out, what I am having problems with is keeping the program from exiting.

The sub main is pretty much

Sub main()
        Dim timeCB As TimerCallback = AddressOf ProcessDocs

        Dim t As Timer = New Timer(AddressOf ProcessDocs, Nothing, 0, 1000)
End Sub

Right now it loads and thens exits. I have put different things in the program such as a pause sub I have:
    Public Sub Pause(ByRef iSeconds As Short)
        Dim lStart As Integer
        lStart = VB.Timer() ' Set start time.
        Do While VB.Timer() < lStart + iSeconds And Not gbStop
            System.Windows.Forms.Application.DoEvents() ' Yield to other processes.
        Loop
    End Sub

Have set it to 60 seconds and see that it works fine, of course after a minute it exists.

The question is: What is the most efficient way of keeping the program from exiting? Right now I have an infinite loop with a pause in the body.

There must be a more elegant, less processor intensive way of doing this, maybe I am being picking, but having to run two timers to get this thing to work makes me think I am just missing something. Any ideas? Remember the entire project consists of a module, there is no UI because it will be ran from a service. At least if I did a message box nobody would every click on it.


Thanks
0
Comment
Question by:randymiller
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 17127789
Since you have NO GUI why not simply....

    Sub Main()
        While True

            ' do some stuff....

            System.Threading.Thread.Sleep(30000) ' 30 second pause
       Wend
    End Sub
0
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 17127807
Are you going to put this into an actual service or are you going to leave it as a console application?    Inside of a service the application will not stop after running and will not need the loop.  The timer tick will still happen.   In the console application it is set up to exit after all the code runs.  This is the issue you are seeing.   I believe the only way you can do this is to loop consistently.. However.. For the pause during the loop you could maybe use thread.sleep(30000)  you can find documentation at http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx      This should cut down your processor usage by not just running the loop consecutively.

Ex.

Do While VB.Timer() < lstart + iseconds And Not gbStop
    Thread.sleep(30000)
Loop
0
 

Author Comment

by:randymiller
ID: 17128115
Thanks the thread.sleep doesn't seem to take any CPU time. I have yet to pull the loop from the service and try it, but if it isn't needed jjardine, then it is even better.

Randy
0
 
LVL 14

Expert Comment

by:jjardine
ID: 17129411
Once you have the code added to an actual service project you can just add the Timer_Tick event and set your timer to tick every 30 seconds.  In the Service_OnStart you can set up the timer and add your delegates  and then the service will sit and wait (not exiting) just like a form application does.  
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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