Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to implement threading for the first time and I don't know if I am applying it correctly or if it is appropriate. Let me explain. I have a Win service that has a timer and every minute it checks an Oracle table to see if there is something in there for it to do (a bit flag). So when there is nothing to do this polling win service is idle. When a record gets dropped in the table and the timer pops the service goes to work and puts the polling mode on hold.

 

What I would like would be for the 'work' to be done on a separate thread and for the original calling code to wait until this thread is done working and then kill it when it comes back.

 

My reason for wanting to do this is because currently the process tends to run up the memory that the service consumes. This memory is not relinquished when the polling mode comes back and the request is done. I'm thinking if I do the work in the thread and then kill it each time I might get around this. Does that make any sense?

 

A bit of my code is below. One issue I had was that the calling code already has an instance of my Request class but I couldn't figure out how to pass that to the Translating function that is on the thread so I had to instantiate a new Request object in the Translating function. That seemed like a hack to me but I couldn't figure out another way.

 

Thanks for any suggestions.

 

 Protected Overrides Sub OnStart(ByVal args() As String)
       Timer1.Enabled = True 'Fire up the timer
       polling = True 'start the service in polling mode
   End Sub
   Protected Overrides Sub OnStop()
       Timer1.Enabled = False 'Clean up
   End Sub
   Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

       Try
           If polling Then
    
               'Look for outstanding requests
               Dim aRequest As New Request
               aRequest.FindRequests()

               If aRequest.rqRequestCount = 0 Then 'No requests, remain in polling mode
                   polling = True
                   EvLog.WriteEntry(strServiceName & " Log", "There are NO outstanding requests " & CStr(TimeOfDay), EventLogEntryType.Information)
                   Exit Sub
               End If

               'requests out there, enter working mode
               polling = False
           
               Dim start As ThreadStart = New ThreadStart(AddressOf Translating)
               m_Worker = New Thread(start)
               EvLog.WriteEntry(strServiceName & " Log", "Spawning worker thread", EventLogEntryType.Information)
               m_Worker.Start()

               'This request is finished, go back to polling mode
               ' Kill the worker thread
               If Not (m_Worker Is Nothing) And m_Worker.IsAlive Then
                   m_Worker.Abort()
                   EvLog.WriteEntry(strServiceName & " Log", "Killing worker thread", EventLogEntryType.Information)
               End If

               polling = True
           End If
       Catch ex As Exception
       End Try
   End Sub

   Private Sub Translating()
       Dim aRequest As New Request
       aRequest.FindRequests()

       'Start translating drawings
       aRequest.DoSomething(aRequest)
   End Sub

Wanna-Be C# Superstar
  • Administrators
Posted
If the memory is not being freed up when the process completes there is no guarentee that moving it to a seperate thread will change things. Is this causing problems due to the memory utilisation or is there still ample memory free after the process completes? If the later then it is probably just the GC being its lazy (by design) self. If the former then it may be more to do with how you are allocating memory within the aRequest.DoSomething(aRequest) call.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Yeah, guess I kinda figured that. The memory grab is not that great but I do notice performance slow down on other applications. This service is running on a development box right now and not a very high powered one either. It will be deployed to a server at some point.

I just thought that maybe running it on a thread might be likely to flush the used memory away faster.

But couldn't another benefit of the threading be if this were to receive multiple requests? I mean couldn't it simulating having a bunch of win services instead of one?

Wanna-Be C# Superstar
  • Administrators
Posted
The requirement to support multiple clients concurrently is a much more convincing argument for considering threading as a solution. However taking single threaded code and making it execute on multiple threads isn't always a simple experience, often the complexities of syncronisation and the pain of debugging can be a major obstacle to overcome.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...