PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Re: Manipulating controls created by another thread
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Re: Manipulating controls created by another thread
![]() |
Re: Manipulating controls created by another thread |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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. |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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. |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

