sportsoft

Hi All

Have had a really good look around and cannot find any posts that have dealt with this issue.

I send messages to a message queue on the local machine with my application that listens for any new messages and reads/responds according.

I wanted to be able to use certain messages to open a new WPF window and other messages (in different queues) to control models on that new widow. I have been as far as the following code:

message receive event handler that opens new widow:

Code Snippet

Dim newWindowThread As Thread = New Thread(AddressOf Me.Thread_start_new_timed_window)

newWindowThread.SetApartmentState(ApartmentState.STA)

newWindowThread.IsBackground = True

newWindowThread.Start()

Thread Event handler for above:

Code Snippet

Private Sub Thread_start_new_timed_window()

frm_timed_scoreboard = New timed_sports_scoreboard_V2

frm_timed_scoreboard.Show()

System.Windows.Threading.Dispatcher.Run()

End Sub

Ongoing message receive event handler that will control model on new window:

Code Snippet

frm_timed_scoreboard.get_time_data(court_data.countdown_time)

Dim newWindowEditThread As Thread = New Thread(AddressOf Me.Thread_update_timer)

newWindowEditThread.SetApartmentState(ApartmentState.STA)

newWindowEditThread.IsBackground = True

newWindowEditThread.Start()

Thread event Handler for above:

Code Snippet

Private Sub Thread_update_timer()

frm_timed_scoreboard.run_timer_changes()

System.Windows.Threading.Dispatcher.Run()

End Sub

Of course when the second thread is fired it creates a "The calling thread cannot access this object because a different thread owns it." Exception. Being new to WPF and the dispatcher I am not sure how to get past this

Any help or ideas would be great

Regards

Nick



Re: Windows Presentation Foundation (WPF) Different The calling thread cannot access this object because a different thread owns it

WPCoder

Nick -- that's not enough code to pinpoint the problem. However, it's not clear why you've created multiple threads Does each Window really need to be on it's on thread Although not impossible, it's unlikely in most situations that you'd need to do that. At some point, your code must be manipulating a usercontrol or a Freezable object across thread/dispatcher boundaries. When it does that, you'll get that error. If you need access to the UI objects or other DependencyObjects created on the UI thread, use the Dispatcher(.Invoke) to get access to the UI thread safely.