当我使用后台工作程序时出现问题 [英] Problem appear when I am using background worker
问题描述
当我使用后台工作程序时,我得到了不清楚的消息'
跨线程操作无效:控制'Panel4'从线程以外的线程访问是创建的。
但是如果按常规到正常按钮它工作正常但我在完成过程后挂起这个过程一切都可以使用。
使用后台工作人员的正确方法是什么?
< b>我尝试了什么:
私人 Sub BtnOk_Click(发件人作为 对象,e As EventArgs)句柄 BtnOk.Click
' 尝试
如果 BW.IsBusy = True 然后
如果 MsgBox( 有一个进程正在运行,你想杀死进程吗?,MsgBoxStyle.YesNo + MsgBoxStyle.Information,< span class =code-string> Process)= MsgBoxResult.Yes 然后
BW.CancelAsync()
结束 如果
Else
BW.RunWorkerAsync()
结束 如果
结束 Sub
< pre> Private Sub BW_DoWork(发送者作为 对象,e As System.ComponentModel.DoWorkEventArgs)句柄 BW.DoWork
如果 不 conn.State = ConnectionState.Open 然后 conn.Open()
SaveInto.Connection = conn
SaveInto.CommandText = DELETE FROM TrialBalance
SaveInto.ExecuteNonQuery()
选择 案例 Period_Choice
案例 0 ' Uptodate
选择 案例 AllBr_Status
案例 True
选择 < span class =code-keyword>案例 RepType
案例 < span class =code-string> 0
选择 案例 Level_Choice
案例 1
' سلكتللمستوىالاولكاجمالياتبدونتحديدايفرع
Dim Account_Code,Account_Name As Stri ng
Dim Selected_row 作为 Boolean
PB.Maximum = Me .AccountsList.Rows.Count
PB.Visible = True
对于 每个 r1 作为 DataGridViewRow 在 我 .AccountsList.Rows
Selected_row = r1.Cells( 0 )。值
Account_Code = Trim(r1.Cells( 1 )。值)
Account_Name =修剪(r1.Cells( 2 )。值)
如果 Selected_row = True 然后
SaveInto.CommandText = INSERT INTO TrialBalance(帐户,名称,OpenBalance)
(SELECT Main_Gr_Code,Main_Gr_Name,Sum(ISNULL(OP_BAL _&修剪(CurYear)& ,0.00))
FROM Acc_OBAL
GROUP BY Main_Gr_Code,Main_Gr_Name
拥有Main_Gr_Code ='&修剪(Account_Code)& ')
如果 不 conn.State = ConnectionState.Open 然后 conn.Open()
SaveInto.ExecuteNonQuery ()
SaveInto.CommandText = 更新TrialBalance SET TransactionBalance =
ISNULL((SELECT SUM( Dr_Acc_Value) - sum(Cr_Acc_Value)As Tot
来自JV_QRY
Group By Main_Gr_Code,JV_Year
HAVING Main_Gr_Code ='& Trim(Account_Code)& '
并且JV_Year ='& CurYear& '),0.00)
其中Account ='&修剪(Account_Code)& '
如果 不 conn.State = ConnectionState.Open 然后 conn.Open()
SaveInto.ExecuteNonQuery()
结束 如果
PB.Value + = 1
下一步
PB.Visible = 错误
PB.Value = 0
< blockquote>您只能从创建它们的线程访问UI控件 - U.我线程。如果你试图在后台工作者中做任何事情 - 这是一个不同的线程 - 你将得到一个交叉线程例外。
而是使用< a href =https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.reportprogress%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396> BackgroundWorker。 ReportProgress方法 [ ^ ]将信息传递给UI线程并让它处理UI更新。
似乎你的问题被定义为:如何使用多线程?
检查:
如何:对Windows窗体控件进行线程安全调用 [ ^ ]
如何:使用后台线程搜索文件 [ ^ ]
这也很有用:
c# - 跨线程操作无效:从其创建的线程以外的线程访问控件 - Stack Overflow [ ^ ]
Hi,
When i am using background worker i got unclear message '
Cross-thread operation not valid: Control 'Panel4' accessed from a thread other than the thread it was created on.
but if take the routine to the normal button it is working perfectly but i got hang while the process then after finishing the process everything can be usable.
what is the correct way to use Background worker?
What I have tried:
Private Sub BtnOk_Click(sender As Object, e As EventArgs) Handles BtnOk.Click
'Try
If BW.IsBusy = True Then
If MsgBox("There is a process is running, Do you like to kill the process?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Process") = MsgBoxResult.Yes Then
BW.CancelAsync()
End If
Else
BW.RunWorkerAsync()
End If
End Sub
<pre>Private Sub BW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BW.DoWork
If Not conn.State = ConnectionState.Open Then conn.Open()
SaveInto.Connection = conn
SaveInto.CommandText = "DELETE FROM TrialBalance"
SaveInto.ExecuteNonQuery()
Select Case Period_Choice
Case "0" 'Uptodate
Select Case AllBr_Status
Case True
Select Case RepType
Case "0"
Select Case Level_Choice
Case "1"
'سلكت للمستوى الاول كاجماليات بدون تحديد اي فرع
Dim Account_Code, Account_Name As String
Dim Selected_row As Boolean
PB.Maximum = Me.AccountsList.Rows.Count
PB.Visible = True
For Each r1 As DataGridViewRow In Me.AccountsList.Rows
Selected_row = r1.Cells(0).Value
Account_Code = Trim(r1.Cells(1).Value)
Account_Name = Trim(r1.Cells(2).Value)
If Selected_row = True Then
SaveInto.CommandText = "INSERT INTO TrialBalance(Account,Name,OpenBalance)
(SELECT Main_Gr_Code,Main_Gr_Name,Sum(ISNULL(OP_BAL_" & Trim(CurYear) & ",0.00))
FROM Acc_OBAL
GROUP BY Main_Gr_Code,Main_Gr_Name
HAVING Main_Gr_Code = '" & Trim(Account_Code) & "')"
If Not conn.State = ConnectionState.Open Then conn.Open()
SaveInto.ExecuteNonQuery()
SaveInto.CommandText = "Update TrialBalance SET TransactionBalance =
ISNULL((SELECT SUM(Dr_Acc_Value) - sum(Cr_Acc_Value) As Tot
From JV_QRY
Group By Main_Gr_Code, JV_Year
HAVING Main_Gr_Code = '" & Trim(Account_Code) & "'
And JV_Year = '" & CurYear & "'),0.00)
where Account = '" & Trim(Account_Code) & "'"
If Not conn.State = ConnectionState.Open Then conn.Open()
SaveInto.ExecuteNonQuery()
End If
PB.Value += 1
Next
PB.Visible = False
PB.Value = 0
You can only access UI controls from the thread on which they were created - the UI thread. If you try to do anything at all with them from within a background worker - which is a different thread - you will get a cross threading exception.
Instead use the BackgroundWorker.ReportProgress method[^] to pass the information to the UI thread and let it deal with UI updates.
Seems your problem is defined as: How to use multithreading?
Check this:
How to: Make Thread-Safe Calls to Windows Forms Controls[^]
How to: Use a Background Thread to Search for Files[^]
This can be useful too:
c# - Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on - Stack Overflow[^]
这篇关于当我使用后台工作程序时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!