By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,820 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,820 IT Pros & Developers. It's quick & easy.

Singleton-please verify usage

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
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" <ec******@nospam.com> wrote in message news:eU**************@TK2MSFTNGP10.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.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
Nov 21 '05 #2

P: n/a
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" <so*****@nospam.com> wrote in message news:eL**************@TK2MSFTNGP12.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" <ec******@nospam.com> wrote in message news:eU**************@TK2MSFTNGP10.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.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
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.