PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Re: Manipulating controls created by another thread

Reply

Re: Manipulating controls created by another thread

 
Thread Tools Rate Thread
Old 13-12-2006, 01:34 PM   #1
Robinson
Guest
 
Posts: n/a
Default Re: Manipulating controls created by another thread


Pass in the for instance when you create the worker, i.e. :

Dim MyWorker As New Worker ( Me )

then invoke on the instance.

Also make sure Button1 is not returning a "dialogresult". If it is, it
might destroy the form when it is clicked which will result in the handle
being unavailable.


  Reply With Quote
Old 13-12-2006, 02:20 PM   #2
Robinson
Guest
 
Posts: n/a
Default Re: Manipulating controls created by another thread


Here, also as branco says Please note that if you close the form before
the thread has completed, you will get the handle error. I have introduced
a delay in the thread so you can see it in operation - and am using "Invoke"
rather than "BeginInvoke".

Public Class Form1

Delegate Sub DisplayStatus(ByVal msg As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim MyWorker As New Worker(Me)
Dim t As New Threading.Thread(AddressOf MyWorker.counter)
t.Start()
End Sub

Public Sub Status(ByVal msg As String)
TextBox1.Text = msg
End Sub

End Class

Public Class Worker

Private m_Form As Form1

Public Sub New(ByVal theForm As Form1)
m_Form = theForm
End Sub

Public Sub counter()

Dim i As Integer

Dim theDelegate As New Form1.DisplayStatus(AddressOf m_Form.Status)

For i = 0 To 10000
m_Form.Invoke(theDelegate, New Object() {i.ToString})
Threading.Thread.Sleep(100)
Next

End Sub

End Class

"Jens" <Jens@discussions.microsoft.com> wrote in message
news:2768AEED-A9A1-4E17-A46A-1B50E215E92F@microsoft.com...
> This seems to be the same problem, but they haven't found a solution, yet:
> http://groups.google.de/group/micro...8bd026d36a33fa8



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off