Re: Me.Invoke - Incorrect behavior by Stoitcho
Stoitcho
Thu Jan 05 15:05:19 CST 2006
Hi,
I apparently haven't seen this problem and sounds very strange giving the
fact how Control.Invoke works. Is it possible that you have post the code
for this methods that mess up with Control.Invoke or if you have isolated
the portion of the code that most likely causes the problem just this code
snippet?
--
Stoitcho Goutsev (100)
"MadisonDecker" <MadisonDecker@community.nospam> wrote in message
news:C504B6A5-E8DE-4C8D-B8B9-5B0FB2156834@microsoft.com...
> Thanks for the response....
> I have placed logging in the code that logs the Thread ID. Each time the
> thread ID is the same and it is not the UI thread. It never gets to the
> "Else" portion of the code like it should because Me.InvokeRequired is
> always
> returning "true". The code works fine except when we do one particular
> function first. It appears that this prior function is causing a threading
> issue but I don't know what it is. I am hoping that someone has
> encountered
> this before or knows why the Me.Invoke isn't creating the delegate on the
> UI
> thread. It is almost as if the Me.Invoke is being forced to use the same
> thread because the UI thread is busy or in an error state.
>
> --
> Madison Decker - MCSD
>
>
> "Stoitcho Goutsev (100) [C# MVP]" wrote:
>
>> Madison,
>>
>> It doesn't matter where you create the delegate. Only the reference to
>> the
>> control, on which the Invoke is called matters.
>>
>> Looking in the posted code I don't see any reason why you get this
>> recursion.
>>
>> My best guess is that the recursion happens from the code in the *Else*
>> clause of the IF statement. For example setting the
>> Mybutton.Enabled to Value may cause SetButton method to be called again
>> which will go in the Elese and execute Mybutton.Enabled = Value again
>> that
>> will call SetButton and so on and so forth....
>>
>> Try to set break points in both branches of the IF statement. See how the
>> execution of the code goes. Inspect the calling thread when the break
>> point
>> is hit.
>>
>> IMHO the problem is not in the Invoke.
>>
>>
>> --
>>
>> Stoitcho Goutsev (100)
>>
>> "MadisonDecker" <MadisonDecker@community.nospam> wrote in message
>> news:ADCA8435-E2BD-4322-9C81-9CB2B79F4922@microsoft.com...
>> >
>> > I have encountered an odd behavior when using Me.Invoke and any help
>> > would
>> > be great. I am using a standard Me.InvokeRequired followed be a
>> > Me.Invoke
>> > to
>> > make sure a method runs on the UI thread. This logic has worked for me
>> > countless times but for some reason I have a problem with it in one
>> > circumstance. The Me.Invoke is called but it does not create the
>> > delegate
>> > on
>> > the UI thread. Instead it creates the delegate on the same thread which
>> > then
>> > gets caught by the Me.InvokeRequired which call Me.Invoke and this
>> > continues
>> > until I get a stack overflow. So my question is what would cause
>> > Me.Invoke
>> > to
>> > not use the UI thread but instead the thread that it is currently on?
>> > Below
>> > is a code snippet....
>> >
>> > Private Delegate Sub SetButtonDelegate(ByVal Value As Boolean)
>> > Private Sub SetButton(ByVal Value As Boolean)
>> > If Me.InvokeRequired Then
>> > Dim newDelegate As New SetButtonDelegate(AddressOf SetButton)
>> > Me.Invoke(newDelegate, New Object() {Value})
>> > Else
>> > Mylabel.Enabled = Value
>> > Mybutton.Enabled = Value
>> > End If
>> >
>> > End Sub
>> >
>> > Thanks
>> > Madison
>> >
>> >
>> > --
>> > Madison Decker - MCSD
>>
>>
>>