Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a class for making database connection in a windows application. It has some functions for making the connection. I want them to run in a separate thread. How can I do that as normally any function address can't be sent to thread..

Here is my code..

Public Sub conn()
        'str = "server=;uid=;pwd=;database=;"
        con = New MySqlConnection(str)
    End Sub

    Public Sub access(ByVal que As String)
        'Try
        con.Open()
        adap = New MySqlDataAdapter(que, con)
        rec.Clear()
        rec.Reset()
        adap.Fill(rec)
        con.Close()
        'Catch e As MySqlException
        'MsgBox(e.ErrorCode.ToString + Chr(13) + "Error in connection to the database." + Chr(13) + "Kindly check your internet connection." + Chr(13) + "Stop all the downloads(if any), then restart the application and try again.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Connection Problem")
        'Catch e As Exception
        'MsgBox("Error in connection to the server." + Chr(13) + "Please try again later.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Connection Problem")
        'End Try
    End Sub

    Public Sub update(ByVal que As String)
        Try
            con.Open()
            cmd = New MySqlCommand(que, con)
            cmd.ExecuteNonQuery()
            con.Close()
        Catch e As MySqlException
            MsgBox(e.ErrorCode.ToString + Chr(13) + "Error in connection to the database." + Chr(13) + "Kindly check your internet connection." + Chr(13) + "Stop all the downloads(if any), then restart the application and try again.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Connection Problem")
        Catch e As Exception
            MsgBox("Error in connection to the server." + Chr(13) + "Please try again later.", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Connection Problem")
        End Try
    End Sub
Posted

This should get you started:

VB
Dim thread as New Thread(new ThreadStart(ThreadProc))
thread.Start()


Private Sub TreadProc()
    Dim conn As SqlConnection = Nothing
    Dim cmd as SqlCommand = Nothing
    Try
        conn = new SqlConnection(blah blah)
        conn.Open()
        cmd = new SqlCommand("query text", conn)
        cmd.ExecuteNonQuery()
    Catch ex as Exception
    Finally
        If conn IsNot Nothing Then
            conn.Close()
        End If
    End Try
End Sub
 
Share this answer
 
Comments
DEB4u 13-Sep-11 15:09pm    
thanx for yr reply... Can I make my access() and con() procedures in side thread like this..

Dim thread as New Thread(new ThreadStart(access))
thread.Start()


Thanx
Dave Kreskowiak 13-Sep-11 15:28pm    
There's no point to it. Since anything you do with cmd depends entirely on conn, there' no point in having con setup and executed on a seperate thread.
Sergey Alexandrovich Kryukov 13-Sep-11 16:42pm    
Exactly.
--SA
#realJSOP 13-Sep-11 15:30pm    
No, making the separate steps into separate threads is beyond pointless. If you want to re-use the parts of a db query, make them global to the class they're used in. You *typically* create a connection, get the data, and close the connection - all in a single method.
Sergey Alexandrovich Kryukov 13-Sep-11 15:20pm    
All correct, my 5. OP is confused about "addresses", threads and delegates; I tried to clarify, but here the regular education in those topics would rather be useful.
--SA
No, .NET does not work with "function address". It does not work with pointers at all. It works with references which are very different from pointers; they are manages, could be safety relocated by the CLR, etc. A .NET method is not just address. In this is an instance method, it is basically two references: to the code entry point and to the instance of the object, which is passed during the call as "this" parameter. This is used by delegate instances, which are pretty complex objects; they hold an invocation lists of other delegate instance, each such list element delegate containing exactly one method in its invocation list.

Any thread can run any delegate; it's the code of the delegate instance which can potentially screw up the execution in a thread, not the thread itself.

—SA
 
Share this answer
 
Comments
DEB4u 13-Sep-11 15:23pm    
ok, so what'd I do have a separate thread for my functions inside a class?
Sergey Alexandrovich Kryukov 13-Sep-11 16:44pm    
You do need a separate thread, but just one more, following the pattern suggested by John and taking into account the advice by Dave.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900