使用vb.net函数进行线程化 [英] Threading with a vb.net function

查看:190
本文介绍了使用vb.net函数进行线程化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个用于在Windows应用程序中建立数据库连接的类.它具有一些用于建立连接的功能.我希望它们在单独的线程中运行.我该怎么办?通常情况下,任何函数地址都无法发送到线程..

这是我的代码.

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

推荐答案

这应该使您入门:

This should get you started:

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


不,.NET可以不适用于功能地址".它根本不适用于指针.它适用于与指针有很大不同的引用.它们是可管理的,可以由CLR安全地重新定位,等等..NET方法不仅是地址.在此实例方法中,基本上有两个引用:指向代码入口点和对象实例,它们在调用期间作为"this"参数传递.委托实例使用它,它们是非常复杂的对象.它们包含其他委托实例的调用列表,每个这样的列表元素委托在其调用列表中仅包含一个方法.

任何线程都可以运行任何委托;它是委托实例的代码,它有可能破坏线程中的执行,而不是线程本身.

—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


这篇关于使用vb.net函数进行线程化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆