Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to get a timer to work,but it won't.. here is the code

 

Option Explicit On 
Imports System.Timers.Timer
Imports System.Data
Imports System.Data.SqlClient

Module Module1
   Dim WithEvents loopit As New Timers.Timer
   Dim counter As Integer = 0

   Sub Main()

       'subSystemUserUsageInsert("S011038mp")
       Dim intTimer As Integer = 3000
       loopit.Enabled = True
       loopit.Interval = intTimer
   End Sub

#Region "System User Usage"
   Private Sub subSystemUserUsageInsert(ByVal vcServerName As String)
       'more or less a dynamic server connection.. server name is being passed in above
       Dim cnRead As SqlConnection = New SqlConnection("server=" & vcServerName & "; database=master; Trusted_connection=true")
       Dim cnCounter As SqlConnection = New SqlConnection("server=ntdts1; database=networking; Trusted_connection=true")
       Dim cnLog As SqlConnection = New SqlConnection("server=Ntdts1; database=networking; trusted_connection=true")

       Dim sqlRead As New SqlCommand("select net_address, dbid, cpu, physical_io, memusage, spid, getdate() as dtmDateTime from dbo.sysprocesses", cnRead)

       If counter = 0 Then
           Dim cmd1Statement As String = "delete tblSystemUserUsageOldRun where vcServer = '" & vcServerName & "'"
           Dim cmd2Statement As String = "delete tblSystemUserUsageNewRun where vcServer = '" & vcServerName & "'"
           Dim cmd1 As New SqlCommand(cmd1Statement, cnCounter)
           Dim cmd2 As New SqlCommand(cmd2Statement, cnCounter)

           Try
               cnCounter.Open()
               cmd1.ExecuteNonQuery()
               cmd2.ExecuteNonQuery()
               cnCounter.Close()
               cmd1 = Nothing
               cmd2 = Nothing
           Catch ex As Exception
               Throw ex
           End Try
       End If


       Dim cmInsert1 As New SqlCommand
       With cmInsert1
           .Connection = cnCounter
           .CommandType = CommandType.StoredProcedure
           .CommandText = "spSystemUserUsageInsert"
           .Parameters.Add(New SqlParameter("@Server", SqlDbType.VarChar, 20))
           .Parameters("@Server").Value = vcServerName
           Try
               cnCounter.Open()
               .ExecuteNonQuery()
               cnCounter.Close()
           Catch ex As Exception
               Throw ex
           End Try
       End With

       Dim cmLog As New SqlCommand
       With cmLog
           .CommandText = "spSystemUserUsageInsertPart2"
           .CommandType = CommandType.StoredProcedure
           .Connection = cnLog
           .Parameters.Add(New SqlParameter("@vcNetAddress", SqlDbType.VarChar, 20))
           .Parameters.Add(New SqlParameter("@intDbid", SqlDbType.Int))
           .Parameters.Add(New SqlParameter("@intCpu", SqlDbType.Int))
           .Parameters.Add(New SqlParameter("@intPhysicalIo", SqlDbType.Int))
           .Parameters.Add(New SqlParameter("@intMemusage", SqlDbType.Int))
           .Parameters.Add(New SqlParameter("@intSpid", SqlDbType.Int))
           .Parameters.Add(New SqlParameter("@vcServer", SqlDbType.VarChar, 20))
           .Parameters.Add(New SqlParameter("@dtmDateTime", SqlDbType.DateTime))
       End With



       cnRead.Open()
       cnLog.Open()
       Dim dtReader As SqlDataReader = sqlRead.ExecuteReader
       Dim vcNetAddress As String
       Dim intDbId As Integer
       Dim intCpu As Integer
       Dim intPhysicalIO As Integer
       Dim intMemusage As Integer
       Dim intSpid As Integer
       Dim vcServer As String
       Dim dtmDateTime As DateTime
       Dim debugCounter As Integer = 0

       While dtReader.Read
           vcNetAddress = CType(dtReader("net_address"), String)
           intDbId = CType(dtReader("dbid"), Integer)
           intCpu = CType(dtReader("cpu"), Integer)
           intPhysicalIO = CType(dtReader("physical_io"), Integer)
           intMemusage = CType(dtReader("memusage"), Integer)
           intSpid = CType(dtReader("spid"), Integer)
           vcServer = vcServerName
           dtmDateTime = CType(dtReader("dtmDateTime"), DateTime)

           With cmLog
               .Parameters("@vcNetAddress").Value = vcNetAddress
               .Parameters("@intDbid").Value = intDbId
               .Parameters("@intCpu").Value = intCpu
               .Parameters("@intPhysicalIo").Value = intPhysicalIO
               .Parameters("@intMemusage").Value = intMemusage
               .Parameters("@intSpid").Value = intSpid
               .Parameters("@vcServer").Value = vcServer
               .Parameters("@dtmDateTime").Value = dtmDateTime
               .ExecuteNonQuery()
               counter = 1
               debugCounter = debugCounter + 1
               Debug.WriteLine(debugCounter)


           End With
       End While

       Try

           cnLog.Close()
           dtReader.Close()
           cnRead.Close()
       Catch ex As Exception
           Throw ex
       End Try

       counter = 1
   End Sub
#End Region

   Private Sub loopit_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles loopit.Elapsed
       subSystemUserUsageInsert("Test")
           End Sub
End Module

 

when stepping through it.. it hits the

loopit.enabled=true

loopit.internaval=intTimer

 

then it goes down to the event handler for the timer..

hits the subSystemUserUsageInsert("Test") and then back to module1 end sub.. it doesn't go into the subSystemUserUsageInsert sub at all..

 

what am I doing wrong.. things got me bummed

JvCoach23

VB.Net newbie

MS Sql Vet

  • Administrators
Posted
When you are running your app it will start at the top of Sub Main, procede throough the code and when the Exit Sub line is reached the method will exit and so will the application. Rather than use a timer you may just want to consider just using a simple loop and going to sleep for 1 second at the end of the loop.

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...