cpu utilization at 100%

neverborn

New member
Joined
Aug 30, 2010
Messages
2
Programming Experience
5-10
I created a windows application service that runs every 20 seconds to collect data from avaya cms and then dump it into a database. I did this using a timer. Since I had to have access to the clipboard to get the data and parse it, I had to be in STA. Unfortunately, just putting the STA before the Main() (or any other sub) did not keep the service in single thread mode. So I had to create a new thread and join it like so:

VB.NET:
Dim cbThread As New Thread(New ThreadStart(AddressOf CopyToClipboard)) cbThread.SetApartmentState(System.Threading.ApartmentState.STA) 
cbThread.Start() 
If Not cbThread Is Nothing Then 
  If Not cbThread.Join(100) Then 
    cbThread.Abort() 
  End If 
End If

At this point my program can access the clipboard and the script works fine for a while. Unfortunately, after a few hours (ranges from 1.5 to 17 hours) cpu utilization on the service hits 100% and the server must be rebooted. I'm not sure what is causing this but I wondered if perhaps the threads I'm creating are somehow building up and taking over the cpu? No error shows up in the event viewer (also, I've not managed to get try/catch to work in my windows service, but it usually writes to the event viewer on errors anyway.)

I can post the entire script if necessary. Would appreciate any help!
 
Last edited:
Here is the full script, I changed the threading part a bit. Same result. It ran for 7 hours then locked up the entire server with no error.

VB.NET:
Public Class CMS
    Protected Overrides Sub OnStart(ByVal args() As String)
        Timer1.Enabled = True
        Timer1.Start()
    End Sub
    Protected Overrides Sub OnStop()
 
    End Sub
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        If Not EventLog.SourceExists("MySource") Then
            EventLog.CreateEventSource("MySource", "MyNewLog")
        End If
        EventLog1.Source = "MySource"
        EventLog1.Log = "MyNewLog"
    End Sub
    Protected Overrides Sub OnContinue()
        'EventLog1.WriteEntry("In OnContinue.")
    End Sub
    Public Sub SaveToDatabase(ByVal strsql)
        Dim dcndb As dbcon
        Dim rs As OleDb.OleDbCommand
        dcndb = New dbcon
        rs = dcndb.DoQuery(strsql)
        rs.ExecuteNonQuery()
        dcndb.CloseCon()
    End Sub
    Sub CopyToClipboard()
        Try
            Dim cvsApp As ACSUP.cvsApplication
            Dim cvsConn As ACSCN.cvsConnection
            Dim cvsSrv As ACSUPSRV.cvsServer
            Dim strsql As String
            cvsApp = New ACSUP.cvsApplication
            cvsConn = New ACSCN.cvsConnection
            cvsSrv = New ACSUPSRV.cvsServer
            If cvsApp.CreateServer("***", "***", "", "***", False, "ENU", cvsSrv, cvsConn) Then
                If cvsConn.Login("***", "***", "***", "ENU", "", False) Then
                End If
            End If

            Dim cvsRepInfo As Object
            Dim cvsRepProp As Object
            Dim cvsLog As Object
            Dim b As Boolean
            Dim bConnected As Boolean

            cvsSrv.Reports.ACD = 4
            cvsRepInfo = cvsSrv.Reports.Reports("Real-Time\Designer\Wallboards")

            If cvsRepInfo Is Nothing Then
                cvsLog = Nothing
            Else

                b = cvsSrv.Reports.CreateReport(cvsRepInfo, cvsRepProp)
                If b Then
                    cvsRepProp.SetProperty("ACD", "4")
                    cvsRepProp.SetProperty("Skill", "37")
                    cvsRepProp.SetProperty("2ACD", "1")
                    cvsRepProp.SetProperty("2Skill", "38")
                    cvsRepProp.SetProperty("3ACD", "3")
                    cvsRepProp.SetProperty("3Skill", "38")

                    b = cvsRepProp.ExportData("", 9, 0, True, True, True)

                    'Closes report
                    If bConnected = True Then
                        cvsRepProp.Quit()
                    Else
                        cvsRepProp = Nothing
                    End If

                    strsql = "delete from RealtimeCMS"
                    Call SaveToDatabase(strsql)
                    Dim thidate As DateTime = Now()

                    Dim ss As Array
                    ss = Split(Clipboard.GetText, vbCrLf)
                    Dim st As Array
                    st = Split(ss(0), vbTab)
                    Dim su As Array
                    su = Split(ss(1), vbTab)
                    Dim sv As Array
                    sv = Split(ss(2), vbTab)
                    Dim sw As Array
                    sw = Split(ss(3), vbTab)
                    Dim sx As Array
                    sx = Split(ss(4), vbTab)
                    Dim sy As Array
                    sy = Split(ss(5), vbTab)

                    strsql = "insert into RealtimeCMS values('"
                    If st(1) = "ACD4_IN" Then
                        strsql = strsql & "India"
                    Else
                        strsql = strsql & st(1)
                    End If
                    strsql = strsql & "'," & su(1) & ",'" & sv(1) & "'," & sw(1) & "," & sx(1) & "," & sy(1) & ",'" & thidate & "')"

                    Call SaveToDatabase(strsql)

                    strsql = "insert into RealtimeCMS values('"
                    If st(2) = "ACD4_IN" Then
                        strsql = strsql & "India"
                    Else
                        strsql = strsql & st(2)
                    End If
                    strsql = strsql & "'," & su(2) & ",'" & sv(2) & "'," & sw(2) & "," & sx(2) & "," & sy(2) & ",'" & thidate & "')"

                    Call SaveToDatabase(strsql)

                    strsql = "insert into RealtimeCMS values('"
                    If st(3) = "ACD4_IN" Then
                        strsql = strsql & "India"
                    Else
                        strsql = strsql & st(3)
                    End If
                    strsql = strsql & "'," & su(3) & ",'" & sv(3) & "'," & sw(3) & "," & sx(3) & "," & sy(3) & ",'" & thidate & "')"

                    Call SaveToDatabase(strsql)
                End If
            End If

            cvsRepInfo = Nothing
            If Not cvsSrv.Interactive Then cvsApp.Servers.Remove(cvsSrv.ServerKey)

            ''If bConnected = False Then
            cvsConn.Logout()
            cvsConn.Disconnect()
            cvsSrv.Connected = False
            '' End If

            cvsConn = Nothing
            cvsSrv = Nothing
            cvsApp = Nothing
        Catch ex As Exception
            EventLog1.WriteEntry("Error: " & vbCrLf & ex.Message & vbCrLf & "Details: " & ex.StackTrace)
        End Try
    End Sub
    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        Dim cbThread As New Thread(New ThreadStart(AddressOf CopyToClipboard))
        cbThread.SetApartmentState(System.Threading.ApartmentState.STA)
        cbThread.Start()
        cbThread.Join()
    End Sub

End Class
 
Back
Top