views:

129

answers:

2

I have build a windows service using a timer which is not ideal for what i want to do. I have a methord i want to call and as soon as its finnished i want it to start again and again and again. What is the best way of doing this and can you show me an example.

+1  A: 

Can you explain a little more what you are trying to accomplish.

When using a timer in a Service you will need to use System.Threading.Timer.

If you are constantly looping then your CPU usage is going to go through the roof. If you are trying to monitor a directory you can use a FileSystemWatcher but this can be troublesome if your host computer is not running Windows. If you are trying to query a table and get results you could have it look every 15 seconds for a new record and if a new record exists mark the function as running via a boolean so that if its still running 15 seconds later it doesn't launch again.

I am going to give you an example of a timer in a system service.

    Private myThreadingTimer As System.Threading.Timer
    Private blnCurrentlyRunning As Boolean = False

    Protected Overrides Sub OnStart(ByVal args() As String)
       Dim myTimerCallback As New TimerCallback(AddressOf OnTimedEvent)
       myThreadingTimer = New System.Threading.Timer(myTimerCallback, Nothing, 1000, 1000)
    End Sub

    Private Sub OnTimedEvent(ByVal state As Object)
        If Date.Now.Second = 1 Or Date.Now.Second = 15 Or Date.Now.Second = 30 Or Date.Now.Second = 45 Then
            If Not blnCurrentlyRunning Then
                blnCurrentlyRunning = True

                Dim myNewThread As New Thread(New ThreadStart(AddressOf MyFunctionIWantToCall))
                myNewThread.Start()
            End If
        End If
    End Sub

Public Sub MyFunctionIWantToCall()
   Try
       'Do Something
   Catch ex As Exception
   Finally
       blnCurrentlyRunning = False
   End Try
End Sub
Zman101
The problem im having with my timer is its launching a 2nd/3rd time before the 1st one is finnshed. but i need the polling time to be as low as possible e.g. 2second this is why im asking about the loop.
MartGriff
So you need to implement a boolean to know if its currently running and if its currently running don't re-execute the function/code. Basically the same as above. You can modify the code so that it fires every 2 seconds if you like by adding more or clause's to the OnTimeEvent function.
Zman101
+3  A: 
private AutoResetEvent  m_waitNextExec = new AutoResetEvent(false);
private int m_execTimer = 1000; //Every second
private bool m_isRunning = true;

private void SomeMethod()
{
    while (m_isRunning)
    {
        //Do something

        m_waitNextExec.WaitOne(m_execTimer);
    }   
}

This code gives you more control over the execution of your code. The auto reset event gives you the possibility to reduce the execution rate.

If you want to abort the execution, you just have to do:

m_isRunning = false;
m_waitNextExec.Set();
Francis B.
Would this cause problem on the Stack? if running 24/7
MartGriff