.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Dispatcher.Invoke Working?
 

Dispatcher.Invoke Working?

When I call Dispatcher.Invoke() it seems to work, in that no errors are thrown, yet the work it is supose to do is not done. Any ideas?


Bill Gates look out!
Bill Gates II
Are you intending to do an assignment or a comparison here?

Function() TextBox2.Text = TextBox2.Text & vbCrLf & e.Data

That is a comparison that will return true if the two expressions are equal. If you meant to assign a value to TextBox2.Text, then I think you want something more like this:

        Private Sub p_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles p.OutputDataReceived
            Try
                If String.IsNullOrEmpty(e.Data) = False Then
                    TextBox2.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, _
                                                   New Action(AddressOf MyAction))
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Private Sub MyAction(ByVal e As System.Diagnostics.DataReceivedEventArgs)
            TextBox2.Text = TextBox2.Text & vbCrLf & e.Data
        End Function

Basically, you were trying to define an inline Sub which is not supported until VB 10.

By the way, Dispatcher.Invoke does not have to be called from the same thread. That would beat the point of using it. TextBox2.Dispatcher.Invoke allows you to execute an action on the TextBox's thread (ie: the UI thread). Since you are trying to edit the contents of the TextBox you need to do it in the UI thread. Therefore, your usage of Dispatcher.Invoke is justified if you are currently in another thread.
  • Marked As Answer byBill Gates II Tuesday, September 22, 2009 9:18 PM
  • Edited byGraeme.Hill Tuesday, September 22, 2009 9:20 PMtypo in code
  •  
Graeme.Hill
How do you know that it does not fire? Have you put a breakpoint in the method that Invoke calls?

Without seeing the problem code it's impossible for us to troubleshoot the error.

P.S. Have you tried using BeginInvoke()?
DeviantSeev
I think you'll need to post more details. Maybe give the snippet of code that calls Dispatcher.Invoke()
Graeme.Hill

Private Sub p_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles p.OutputDataReceived
    Try
        If String.IsNullOrEmpty(e.Data) = False Then
            TextBox2.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, _
                                           New Action(Function() TextBox2.Text = TextBox2.Text & vbCrLf & e.Data))
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
'p' is a System.Diagnostic.Process

I debuged it and it calls TextBox2.Dispatcher.Invoke(...) yet TextBox2.Text does not change.
Bill Gates look out!
Bill Gates II
Are you calling the Invoke process on the UI thread? Can you show us the part of the code where you're calling this function?

If I recall correctly, the Dispatcher.Invoke has to be called on the same thread where it was created. In this case you're calling the TextBox.Dispatcher.Invoke so it is created on the UI thread.
DeviantSeev
It's called by the event p.OutputDataReceived so I'm not sure what you mean by where I'm calling it from.

I don't know if I'm calling it from the UI thread. My Multi-Threading experience is nothing really beyond using BackgroundWorker.
Bill Gates look out!
Bill Gates II
I am sorry my VB is not great so I did not realize that your code is actually an event handler.

I think it would help if you would tell me what you're trying to achieve and I can try to give you the code for it.
DeviantSeev
Are you intending to do an assignment or a comparison here?

Function() TextBox2.Text = TextBox2.Text & vbCrLf & e.Data

That is a comparison that will return true if the two expressions are equal. If you meant to assign a value to TextBox2.Text, then I think you want something more like this:

        Private Sub p_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles p.OutputDataReceived
            Try
                If String.IsNullOrEmpty(e.Data) = False Then
                    TextBox2.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, _
                                                   New Action(AddressOf MyAction))
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Private Sub MyAction(ByVal e As System.Diagnostics.DataReceivedEventArgs)
            TextBox2.Text = TextBox2.Text & vbCrLf & e.Data
        End Function

Basically, you were trying to define an inline Sub which is not supported until VB 10.

By the way, Dispatcher.Invoke does not have to be called from the same thread. That would beat the point of using it. TextBox2.Dispatcher.Invoke allows you to execute an action on the TextBox's thread (ie: the UI thread). Since you are trying to edit the contents of the TextBox you need to do it in the UI thread. Therefore, your usage of Dispatcher.Invoke is justified if you are currently in another thread.
  • Marked As Answer byBill Gates II Tuesday, September 22, 2009 9:18 PM
  • Edited byGraeme.Hill Tuesday, September 22, 2009 9:20 PMtypo in code
  •  
Graeme.Hill
Yea that solves my problem. Can't wait for VB 10 now!

Thats what I thought, and the reason I was using it.

Thank you!
Bill Gates look out!
Bill Gates II

You can use google to search for other answers

Custom Search

More Threads

• Performance: Zoomable User Interface
• Mousewheel Event in xaml
• Create a splash screen in WPF
• Client architecture
• Customizing Listbox
• having touble with listbox and keyboard focus
• Parallel (synchronized) mediaelements
• drag and drop between list boxes
• Disabling OnTextChanged
• Listview to Treeview drag drop find drop treeviewitem