PDA

View Full Version : مشکل در BackGroundWorker



hosein320
یک شنبه 24 شهریور 1392, 11:26 صبح
سلام
این خطا برای چیه که پیش میاد

The calling thread cannot access this object because a different thread owns it.
من کد رو وقتی داخل بک گراند ورکر میزارم این خطا میاد
ولی وقتی معمولی استفاده میکنم مشکلی نیست و راحت اجرا میشه

hosein320
یک شنبه 24 شهریور 1392, 21:14 عصر
دوستان کمک:گریه:

aliagamon
یک شنبه 24 شهریور 1392, 21:37 عصر
از invoke استفاده کنید یا اینو بزارین تو فرم لود:

CheckForIllegalCrossThreadCalls = False

hosein320
یک شنبه 24 شهریور 1392, 21:52 عصر
از invoke استفاده کنید یا اینو بزارین تو فرم لود:

CheckForIllegalCrossThreadCalls = False

ممنون از توجهتون
ارور میده
میشه دلیلشو بگید

aliagamon
یک شنبه 24 شهریور 1392, 22:32 عصر
از invoke استفاده کنید ...

hosein320
یک شنبه 24 شهریور 1392, 23:13 عصر
نمونه سرسی دارین
آشنا نیستم

hosein320
دوشنبه 25 شهریور 1392, 16:45 عصر
پست اسپم حهت دریافت کمک:لبخند:

tooraj_azizi_1035
دوشنبه 25 شهریور 1392, 17:08 عصر
کدتون رو بذارید.

hosein320
دوشنبه 25 شهریور 1392, 22:24 عصر
کل کد رو نمیتونم بزارم
این اون قستمی هست که خطا میده
Sub BwDellVirtual_RunWork()
Static DellVirtualCht As New Cht
UserDell = DellVirtualCht.DeleteVirtualUser(ComboVirtualUserD ell.SelectedItem, MyProperty.MyUserName, MyProperty.MyPwd)
End Sub

Sub BwDellVirtual_WorkCompelete()
BtnREmoveVirtual.IsEnabled = True
If UserDell = False Then
MsgBox("خطا")
End If
End Sub
http://uploadkon.ir/uploads/Capture_315.JPG

tooraj_azizi_1035
سه شنبه 26 شهریور 1392, 21:58 عصر
از متد Invoke استفاده کن:


' The following example demonstrates the 'Invoke(Delegate)' method of 'Control class.
' A 'ListBox' and a 'Button' control are added to a form, containing a delegate
' which encapsulates a method that adds items to the listbox.This function is executed
' on the thread that owns the underlying handle of the form. When user clicks on button
' the above delegate is executed using 'Invoke' method.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MyFormControl
Inherits Form

Delegate Sub AddListItem()
Public myDelegate As AddListItem
Private myButton As Button
Private myThread As Thread
Private myListBox As ListBox

Public Sub New()
myButton = New Button()
myListBox = New ListBox()
myButton.Location = New Point(72, 160)
myButton.Size = New Size(152, 32)
myButton.TabIndex = 1
myButton.Text = "Add items in list box"
AddHandler myButton.Click, AddressOf Button_Click
myListBox.Location = New Point(48, 32)
myListBox.Name = "myListBox"
myListBox.Size = New Size(200, 95)
myListBox.TabIndex = 2
ClientSize = New Size(292, 273)
Controls.AddRange(New Control() {myListBox, myButton})
Text = " 'Control_Invoke' example"
myDelegate = New AddListItem(AddressOf AddListItemMethod)
End Sub 'New

Shared Sub Main()
Dim myForm As New MyFormControl()
myForm.ShowDialog()
End Sub 'Main

Public Sub AddListItemMethod()
Dim myItem As String
Dim i As Integer
For i = 1 To 5
myItem = "MyListItem" + i.ToString()
myListBox.Items.Add(myItem)
myListBox.Update()
Thread.Sleep(300)
Next i
End Sub 'AddListItemMethod

Private Sub Button_Click(sender As Object, e As EventArgs)
myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
myThread.Start()
End Sub 'Button_Click

Private Sub ThreadFunction()
Dim myThreadClassObject As New MyThreadClass(Me)
myThreadClassObject.Run()
End Sub 'ThreadFunction
End Class 'MyFormControl


' The following code assumes a 'ListBox' and a 'Button' control are added to a form,
' containing a delegate which encapsulates a method that adds items to the listbox.
Public Class MyThreadClass
Private myFormControl1 As MyFormControl

Public Sub New(myForm As MyFormControl)
myFormControl1 = myForm
End Sub 'New

Public Sub Run()
' Execute the specified delegate on the thread that owns
' 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate)
End Sub 'Run

End Class 'MyThreadClass