Solved

Most Efficient Way to Keep Module from Exiting

Posted on 2006-07-17
4
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…

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