Home All Groups Group Topic Archive Search About

VB.NET Windows Service Question (Timing)

Author
16 May 2006 5:33 PM
ggillard
Hi,

I'm new to writing services and found it pretty easy using VB.NET.

My service has an internal timer that  fires every 3 seconds.  Each
time it fires it opens a SQL DB and looks for a specific set of
records.  Each record it finds it sends to SAP and then changes their
status so they won't be sent again.

When I run this code out side of a service ( I built a small
userinterface for testing) it runs flawlessly.  However the service
will start duplicating (sending records 2-4 times).  In this example I
put about 500 records in the table and start the service.  It works
great for the first 30 records.

It seems as if it gets interrupted before it gets a chance to flag a
record as already processed and then the next time it fires it finds
the "already processed" data as needing to be processed.

What am I missing with a "windows service" that would interupt my code
AND not resume at the point of interruption?  It's almost as if the
program re-starts over an over again.

Any help or ideas are appreciated.

Author
16 May 2006 6:03 PM
sfbell09
How is your service working? Is it starting a new thread every 3
seconds to perform the required actions?  Or are is it one thread in a
forever type loop with a 3 second pause at the bottom?

Next, how is the database being checked/updated? If you aren't doing
so, I would suggest a stored procedure that performs the check and
updates each record before moving on to the next.

Can you show some / all of the code? It would help out.


ggill***@gmail.com wrote:
Show quoteHide quote
> Hi,
>
> I'm new to writing services and found it pretty easy using VB.NET.
>
> My service has an internal timer that  fires every 3 seconds.  Each
> time it fires it opens a SQL DB and looks for a specific set of
> records.  Each record it finds it sends to SAP and then changes their
> status so they won't be sent again.
>
> When I run this code out side of a service ( I built a small
> userinterface for testing) it runs flawlessly.  However the service
> will start duplicating (sending records 2-4 times).  In this example I
> put about 500 records in the table and start the service.  It works
> great for the first 30 records.
>
> It seems as if it gets interrupted before it gets a chance to flag a
> record as already processed and then the next time it fires it finds
> the "already processed" data as needing to be processed.
>
> What am I missing with a "windows service" that would interupt my code
> AND not resume at the point of interruption?  It's almost as if the
> program re-starts over an over again.
>
> Any help or ideas are appreciated.
Author
16 May 2006 6:57 PM
ggillard
I believe it's single threaded since I didn't do anything to (nor do I
know enough) to make it multi-threaded.

There is an event timer that fires every 3000 milliseconds (no loop
code).

This VB.NET App can't pass the operation off to SQL stored proc because
it has to pass the data it finds in the SQL app through IBM Websphere
MQ to be received and processed by SAP.  If a transaction fails I won't
know until SAP responds.

Records have status of (simplified)
1=Being created (don't process)
2=Done and ready for service to send to SAP
3=Sent to SAP (waiting for response)
4=SAP responded success
5=SAP responded failure

100s of users add records to the SQL DB.  This service just looks for
records with a status of 2 and watches for SAP responses to previously
posted records.

Nearly all of the code is bundled into a number of class modules
compiled into a DLL.

If I reference this DLL from a standard VB.NET (with forms) which also
has a 3000 millisecond timer it performs flawlessly.  Even if it wakes
up to find 5000 records to process.

The same code referenced from a VB.NET service starts screwing up after
about 30 records.  It seems like it probably has something to do with
how a service gets "time slices" or as you said "threading".  This is
where I lack the knowledge and it's tough to debug/troubleshoot a
VB.NET service.  The problem does not occur unless there are enough
records to keep the service busy for a few seconds.

Thanks for the response.
Author
16 May 2006 6:51 PM
Jim Wooley
Show quote Hide quote
> Hi,
>
> I'm new to writing services and found it pretty easy using VB.NET.
>
> My service has an internal timer that  fires every 3 seconds.  Each
> time it fires it opens a SQL DB and looks for a specific set of
> records.  Each record it finds it sends to SAP and then changes their
> status so they won't be sent again.
>
> When I run this code out side of a service ( I built a small
> userinterface for testing) it runs flawlessly.  However the service
> will start duplicating (sending records 2-4 times).  In this example I
> put about 500 records in the table and start the service.  It works
> great for the first 30 records.
>
> It seems as if it gets interrupted before it gets a chance to flag a
> record as already processed and then the next time it fires it finds
> the "already processed" data as needing to be processed.
>
> What am I missing with a "windows service" that would interupt my code
> AND not resume at the point of interruption?  It's almost as if the
> program re-starts over an over again.
>
> Any help or ideas are appreciated.

It sounds like you are running into an issue of multiple threads operating
at the same time. If the processing takes more than 3 seconds, you may have
a currently running process that doesn't block out another process from starting.
You may want to put a SynchLock around the process or otherwise check to
see if a process is already running before firing another one off.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Author
17 May 2006 11:47 AM
Phill W.
ggill***@gmail.com wrote:

> When I run this code out side of a service ( I built a small
> userinterface for testing) it runs flawlessly.  However the service
> will start duplicating (sending records 2-4 times). 

> It seems as if it gets interrupted before it gets a chance to flag a
> record as already processed and then the next time it fires it finds
> the "already processed" data as needing to be processed.

I think the problem is that the Timer is firing, starting your
long-running database process abd, before that's finished properly, the
timer is firing /again/.

As a general rule, I disable/stop the Timer when I enter the Elapsed
routine, and restart it on the way out.

Sub Timer1_Elapsed ' or Tick - can't remember which
    Try
       Timer1.Stop
       Call LongRunningProcess()
    Finally
       Timer1.Start
    End Try
End Sub

Or, alternatively, replace the Timer with calls to Sleep() instead, as in

Private m_bKillLoop as Boolean = False

Sub Run()
    Do While Not m_bKillLoop
       Call LongRunningProcess()

       If Not m_bKillLoop Then
          System.Threading.Thread.Sleep(3000)
       End If
    Loop
End Sub

HTH,
    Phill  W.