Solved

Most Efficient Way to Keep Module from Exiting

Posted on 2006-07-17
4
158 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 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 125 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

26 Experts available now in Live!

Get 1:1 Help Now