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, 02:07 PM   #1
Branco Medeiros
Guest
 
Posts: n/a
Default Re: Manipulating controls created by another thread



Jens wrote:
<snip>
> Public Class Worker
>
> Public Sub counter()
> Dim i As Integer
> For i = 0 To 10000
> Form1.TextBox1.BeginInvoke(New Form1.DisplayStatus(AddressOf
> Form1.Status), New Object() {i.ToString})
>
> Next
> End Sub
>
> End Class
>
> When I try to run it I get the error "Invoke oder BeginInvoke kann für ein
> Steuerelement erst aufgerufen werden, wenn das Fensterhandle erstellt wurde."
> Roughly translated to "Invoke or begininvoke can only be run when the
> windows handle has been created".

<snip>

The problem with the code above is that you're apparently trying to use
a default instance of Form1 -- a *bad* practice. Pass a reference to
the actual instance of the form and use that.

Something in the likes of:

<aircode>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
> System.EventArgs) Handles Button1.Click
> Dim MyWorker As New Worker


MyWorker.Form1 = Me

> Dim t As New Threading.Thread(AddressOf MyWorker.counter)
> t.Start()
> End Sub


> Public Class Worker

Public Form As Form1
</aircode>

It also seems to me that you don't need to create 10000 (actually
10001) new instances of the delegate. Just one would suffice:

> Public Sub counter()


Dim Ds As New Form1.DisplayStatus(AddressOf Form1.Status)

> Dim i As Integer
> For i = 0 To 10000


Form1.TextBox1.BeginInvoke(Ds, New Object() {i.ToString})

> Next


HTH.

Regards,

Branco.

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

Jens wrote:
> So, I've tried to integrate your proposals, but still the same error msg:

<snip>
> Public Class Worker
> Public Form As Form1
>
> Public Sub counter()
> Dim i As Integer
> Dim DS As New Form1.DisplayStatus(AddressOf Form1.Status)
>
> For i = 0 To 10000
> Form1.TextBox1.BeginInvoke(DS, New Object() {i.ToString})

<snip>

This last line should reference the Form variable, not Form1 (I noticed
just now that there's an error in the "aircode" of my previous message,
sorry about that).

Also, you may want to follow Robinson's suggestion, and use Invoke()
instead of BeginInvoke(), for the first will work synchronously -- that
is, it will block until the form processes the message. This may or may
not be what you want... That said, the line above would then read:

Form.Invoke(DS, New Object() {i.ToString})

Notice that Form is the variable you declared in your Worker class.

HTH.

Regards,

Branco.

  Reply With Quote
Old 13-12-2006, 03:12 PM   #3
Branco Medeiros
Guest
 
Posts: n/a
Default Re: Manipulating controls created by another thread

Oops!

I just noticed the following:

> Public Sub counter()
> Dim i As Integer
> Dim DS As New Form1.DisplayStatus(AddressOf Form1.Status)


The above line should obviously read:

Dim DS As New Form1.DisplayStatus(AddressOf Form.Status)

At this point I should rename this thread to "The hurdles caused by a
misplaced '1' in aircode and similar helping devices", but the actual
issue is "Always follow your best practices even when aircoding."
(which I didn't) =)

Seriously, take a look at Robinson's example, it does the right thing.

Regards,

Branco.

  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