如何停止和处置线程 [英] How to Stop and Dispose Thread

查看:87
本文介绍了如何停止和处置线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中,加载和处理会花费很长时间,因此请尝试使用
线程,但它会关闭窗体中的错误.我的句柄线程如下:

昏暗的ThreadBom作为新的System.Threading.Thread(AddressOf HandleData)
ThreadBom.Start()
ThreadBom.Join(1000)

但是它将在关闭窗体时运行错误.如果您知道为什么,请告诉我,
非常感谢.

In the program, the loading and handling cost long time, so try using
Thread, but it will bug in the form closing. my handle Thread as following :

Dim ThreadBom As New System.Threading.Thread(AddressOf HandleData)
ThreadBom.Start()
ThreadBom.Join(1000)

But it will run bug in Form closed. If you know why, please tell me,
Many thanks.

推荐答案

好吧,我认为您对不同的东西花费多少感到困惑.您如何结束您的申请?您需要停止所有正在运行的线程才能在此实例中停止您的应用程序.您的线程在做什么?


Well, I think you''re a little confused about how much different things cost. How do you end your application ? You need to stop all the running threads to stop your app in this instance. What is your thread doing ?


写道:​​

但是它将在关闭的窗体中运行错误

But it will run bug in Form closed



如果您告诉我们该错误是什么,您遇到什么错误,等等,将对您有所帮助.



It would help if you told us what the bug was, what errors you get, etc.


感谢Graus的回复.

应用程序如下:

Thankyou for Graus''s reply.

the application as following :

<br />
Imports System.Data.SqlClient<br />
Imports System.Threading<br />
Imports System.Windows.Forms<br />
<br />
Public Class Form1<br />
<br />
    Dim m_dtTemp As New DataTable<br />
<br />
    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed<br />
        If m_dtTemp IsNot Nothing Then<br />
            m_dtTemp.Dispose()<br />
            m_dtTemp = Nothing<br />
        End If<br />
    End Sub<br />
<br />
<br />
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
        Dim sSql As String<br />
<br />
        Try<br />
            sSql = "SELECT SYA_NAME, BMM_LINE_NO, BMM_DATA " & _<br />
                                   "FROM BM_M_INFO INNER JOIN SY_M_UDC_DTA ON SYA_TYPE=''BIT'' AND SYA_DTA_NO=BMM_TYPE " & _<br />
                                   "WHERE BMM_BOM_NO=''18.8892''"<br />
            Using myCommand As New SqlCommand(sSql, g_SqlConn), myReader As SqlDataReader = myCommand.ExecuteReader<br />
                m_dtTemp.Load(myReader)<br />
            End Using<br />
<br />
            Dim Threadcbo As New System.Threading.Thread(AddressOf HandleData)<br />
            Threadcbo.Start()<br />
            Threadcbo.Join(1000)<br />
        Catch ex As Exception<br />
            HandleException(ex)<br />
        End Try<br />
    End Sub<br />
<br />
    Private Sub HandleData()<br />
        For Each myRow As DataRow In m_dtTemp.Rows<br />
            If myRow.Item(0).ToString.EndsWith("a") Then myRow.Item(0) = "Test"<br />
        Next<br />
    End Sub<br />
<br />
<br />
End Class<br />



如果我要处理子控件"HandleData"中的控件值,
如何解决它?



If i want the handle the control''s value in sub ''HandleData'',
How to fix it?


在我看来,这似乎是对线程的疯狂使用.我猜想线程运行时UI仍会保持响应,但是它在做什么,为什么要花这么长时间?要管理UI元素,您需要在主线程上调用它们.总的来说,我认为您正在做错事情.我不认为在这里线程是个好主意,除非您希望线程在UI保持响应的同时持续运行.我将使用BackgroundWorker,因为它是针对调用UI线程之类的东西精心设计的.您需要告诉我们您的错误是什么,但是我怀疑您需要在应用关闭时杀死线程.
This looks to me like an insane use of threading. I guess the UI remains responsive while the thread runs, but what is it doing, and why does it take so long ? To manage UI elements, you need to invoke them on the main thread. Overall, I think you''re going about things wrong. I''m not convinced a thread is a good idea here, unless you want this to run constantly, while the UI stays responsive. I would use a BackgroundWorker, because it is well designed for things like making calls to the UI thread. You need to tell us what your error is, but I suspect you need to kill the thread when your app is closing.


这篇关于如何停止和处置线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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