tiben20 New User
Posts : 5 Join date : 2008-10-18 Age : 28
| Subject: Re: MultiThreading, Using controls accross threads. Sat Oct 18, 2008 6:27 am | |
| you can use the System.ComponentModel.BackgroundWorker i just did a test project for you - Code:
-
class frmmain Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click cmdTest.Enabled = False If bworker.IsBusy = False Then bworker.RunWorkerAsync() End If End Sub
Private Sub bworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bworker.DoWork Dim xxxx As String For x = 0 To 9999999999 'only for wasting time!! xxxx = "yo" xxxx = "yeah" Next End Sub
Private Sub bworker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bworker.RunWorkerCompleted cmdTest.Enabled = True
End Sub end class enjoy it | |
|
wodi666 Junior Member
Posts : 17 Join date : 2008-10-15
| Subject: Re: MultiThreading, Using controls accross threads. Sat Oct 18, 2008 3:18 am | |
| Ok, i am a c# developer, but i hope that doesn't matter You have to check before you use a control, if there is Invoke required. - Code:
-
Dim instance As Control Dim value As Boolean
value = instance.InvokeRequired
If Invoke is required... - Code:
-
Dim instance As Control Dim method As [Delegate] Dim returnValue As Object
returnValue = instance.Invoke(method)
There could be a problem, that the control isn't build, and you don't have a handle on the control. InvokeRequired return false. And the thread was started in the consturtor of the control for example. You have to check if the handle exist. If there is no handle you couldn't use Invoke, delegate, etc. - Code:
-
Dim instance As Control Dim value As Boolean
value = instance.IsHandleCreated
Here is an example (msdn) - Code:
-
' 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
wodi666 | |
|
billyad2000 Admin
Posts : 1326 Join date : 2008-09-20
| Subject: MultiThreading, Using controls accross threads. Fri Oct 17, 2008 3:29 pm | |
| In order to take this program to the next step it will be necessary to change this into a multi-threaded application. As an example I would like to allow the program to scan for, and add new movies, while keeping the rest of the program useable.
The first problem I have come across with this is that controls can only be accessed from the same thread that created them. I have read that the way around this is to use the Invoke method, I am having difficulty working out exactly how this works.
Could someone give me a simple example of a small program that using 2 threads, and modifys a control on the first thread from within the second thread. | |
|
Sponsored content
| Subject: Re: MultiThreading, Using controls accross threads. | |
| |
|