Singleton-please verify usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ECathell

    #1

    Singleton-please verify usage

    In Theory is this the correct use of a singleton object? I only want them to be able to run this process once(of course by closeing and restarting the application stops that functionality).
    Is this setup properly?


    Public Class OracleSnapshot

    Private Shared m_instance As OracleSnapshot

    Private Sub New()
    Try
    Dim t As System.Threadin g.Thread
    t = New Threading.Threa d(AddressOf runSnapshot)
    Catch ex As Exception
    Dim m As New StringBuilder
    m.Append("Error Running OracleSnapshot. Processed Failed")
    m.Append(Enviro nment.NewLine)
    m.Append(ex.ToS tring)

    MessageBox.Show (m.ToString, "Error Creating Snapshot")
    End Try
    End Sub

    Public Shared Function Activate() As OracleSnapshot
    If m_instance Is Nothing Then
    m_instance = New OracleSnapshot
    End If
    Return m_instance

    End Function

    Private Shared Sub runSnapshot()
    Dim config As New Configuration.A ppSettingsReade r
    Dim sqlDatabase, sqlServer As String

    sqlDatabase = config.GetValue ("databasename" , GetType(String) )
    sqlServer = config.GetValue ("servername ", GetType(String) )
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim mycon As New EricDLL.Databas eConnection(sql Server, sqlDatabase)

    Try
    con.ConnectionS tring = mycon.Connectio nString

    With cmd
    .Connection = con
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "SendBeginingIn ventoryToOracle "
    .CommandTimeout = 60

    End With
    con.Open()

    cmd.ExecuteNonQ uery()

    Catch ex As Exception
    Debug.WriteLine (ex.ToString)
    Finally
    con.Close()
    con.Dispose()
    cmd.Dispose()

    End Try
    End Sub
    End Class
  • Marina

    #2
    Re: Singleton-please verify usage

    Thing is, what would anyone do with an instance of an OracleSnapshot object? The only methods in this class are Shared.

    Also, you are creating a new thread for runSnapshot, but you are never starting it?


    "ECathell" <ecathell@nospa m.com> wrote in message news:eUUkwHrlFH A.3584@TK2MSFTN GP10.phx.gbl...
    In Theory is this the correct use of a singleton object? I only want them to be able to run this process once(of course by closeing and restarting the application stops that functionality).
    Is this setup properly?


    Public Class OracleSnapshot

    Private Shared m_instance As OracleSnapshot

    Private Sub New()
    Try
    Dim t As System.Threadin g.Thread
    t = New Threading.Threa d(AddressOf runSnapshot)
    Catch ex As Exception
    Dim m As New StringBuilder
    m.Append("Error Running OracleSnapshot. Processed Failed")
    m.Append(Enviro nment.NewLine)
    m.Append(ex.ToS tring)

    MessageBox.Show (m.ToString, "Error Creating Snapshot")
    End Try
    End Sub

    Public Shared Function Activate() As OracleSnapshot
    If m_instance Is Nothing Then
    m_instance = New OracleSnapshot
    End If
    Return m_instance

    End Function

    Private Shared Sub runSnapshot()
    Dim config As New Configuration.A ppSettingsReade r
    Dim sqlDatabase, sqlServer As String

    sqlDatabase = config.GetValue ("databasename" , GetType(String) )
    sqlServer = config.GetValue ("servername ", GetType(String) )
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim mycon As New EricDLL.Databas eConnection(sql Server, sqlDatabase)

    Try
    con.ConnectionS tring = mycon.Connectio nString

    With cmd
    .Connection = con
    .CommandType = CommandType.Sto redProcedure
    .CommandText = "SendBeginingIn ventoryToOracle "
    .CommandTimeout = 60

    End With
    con.Open()

    cmd.ExecuteNonQ uery()

    Catch ex As Exception
    Debug.WriteLine (ex.ToString)
    Finally
    con.Close()
    con.Dispose()
    cmd.Dispose()

    End Try
    End Sub
    End Class

    Comment

    • ECathell

      #3
      Re: Singleton-please verify usage

      Yep I found that and fixed it..thanks

      The sole reason for this class is to run the stored procedure. Only allowing it to be run once and running it on its own separate thread so that it doesn't lockup the interface.

      --
      --Eric Cathell, MCSA
      "Marina" <someone@nospam .com> wrote in message news:eLQ7MhrlFH A.3144@TK2MSFTN GP12.phx.gbl...
      Thing is, what would anyone do with an instance of an OracleSnapshot object? The only methods in this class are Shared.

      Also, you are creating a new thread for runSnapshot, but you are never starting it?


      "ECathell" <ecathell@nospa m.com> wrote in message news:eUUkwHrlFH A.3584@TK2MSFTN GP10.phx.gbl...
      In Theory is this the correct use of a singleton object? I only want them to be able to run this process once(of course by closeing and restarting the application stops that functionality).
      Is this setup properly?


      Public Class OracleSnapshot

      Private Shared m_instance As OracleSnapshot

      Private Sub New()
      Try
      Dim t As System.Threadin g.Thread
      t = New Threading.Threa d(AddressOf runSnapshot)
      Catch ex As Exception
      Dim m As New StringBuilder
      m.Append("Error Running OracleSnapshot. Processed Failed")
      m.Append(Enviro nment.NewLine)
      m.Append(ex.ToS tring)

      MessageBox.Show (m.ToString, "Error Creating Snapshot")
      End Try
      End Sub

      Public Shared Function Activate() As OracleSnapshot
      If m_instance Is Nothing Then
      m_instance = New OracleSnapshot
      End If
      Return m_instance

      End Function

      Private Shared Sub runSnapshot()
      Dim config As New Configuration.A ppSettingsReade r
      Dim sqlDatabase, sqlServer As String

      sqlDatabase = config.GetValue ("databasename" , GetType(String) )
      sqlServer = config.GetValue ("servername ", GetType(String) )
      Dim con As New SqlConnection
      Dim cmd As New SqlCommand
      Dim mycon As New EricDLL.Databas eConnection(sql Server, sqlDatabase)

      Try
      con.ConnectionS tring = mycon.Connectio nString

      With cmd
      .Connection = con
      .CommandType = CommandType.Sto redProcedure
      .CommandText = "SendBeginingIn ventoryToOracle "
      .CommandTimeout = 60

      End With
      con.Open()

      cmd.ExecuteNonQ uery()

      Catch ex As Exception
      Debug.WriteLine (ex.ToString)
      Finally
      con.Close()
      con.Dispose()
      cmd.Dispose()

      End Try
      End Sub
      End Class

      Comment

      Working...