Singleton-please verify usage

E

ECathell

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.Threading.Thread
t = New Threading.Thread(AddressOf runSnapshot)
Catch ex As Exception
Dim m As New StringBuilder
m.Append("Error Running OracleSnapshot. Processed Failed")
m.Append(Environment.NewLine)
m.Append(ex.ToString)

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.AppSettingsReader
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.DatabaseConnection(sqlServer, sqlDatabase)

Try
con.ConnectionString = mycon.ConnectionString

With cmd
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandText = "SendBeginingInventoryToOracle"
.CommandTimeout = 60

End With
con.Open()

cmd.ExecuteNonQuery()

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

End Try
End Sub
End Class
 
M

Marina

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?


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.Threading.Thread
t = New Threading.Thread(AddressOf runSnapshot)
Catch ex As Exception
Dim m As New StringBuilder
m.Append("Error Running OracleSnapshot. Processed Failed")
m.Append(Environment.NewLine)
m.Append(ex.ToString)

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.AppSettingsReader
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.DatabaseConnection(sqlServer, sqlDatabase)

Try
con.ConnectionString = mycon.ConnectionString

With cmd
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandText = "SendBeginingInventoryToOracle"
.CommandTimeout = 60

End With
con.Open()

cmd.ExecuteNonQuery()

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

End Try
End Sub
End Class
 
E

ECathell

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


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.Threading.Thread
t = New Threading.Thread(AddressOf runSnapshot)
Catch ex As Exception
Dim m As New StringBuilder
m.Append("Error Running OracleSnapshot. Processed Failed")
m.Append(Environment.NewLine)
m.Append(ex.ToString)

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.AppSettingsReader
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.DatabaseConnection(sqlServer, sqlDatabase)

Try
con.ConnectionString = mycon.ConnectionString

With cmd
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandText = "SendBeginingInventoryToOracle"
.CommandTimeout = 60

End With
con.Open()

cmd.ExecuteNonQuery()

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

End Try
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top