Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Most Efficient Way to Keep Module from Exiting

Posted on 2006-07-17
4
Medium Priority
?
164 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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