Solved

Most Efficient Way to Keep Module from Exiting

Posted on 2006-07-17
4
163 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: 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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