1

I'm converting a UI from windows forms to WPF. I'm getting the following exception "The Calling thread cannot access this object because a different thread owns it" whenever I try to call anything on this new WPF window I created.

I referred stack overflow and many websites to find out that I should use Dispatcher.CheckAccess() or somethings similar to dispatcher and check access. I tried many such things

This is one of the things that I have used

Private Delegate Sub ShowSkinInvoked()

If (Dispatcher.CheckAccess()) Then
    Me.Show()
Else
    Dim d As ShowSkinInvoked = New ShowSkinInvoked(AddressOf ShowSkin)
    Dispatcher.Invoke(d)
End If

This has removed the exception and while debugging the error is gone but it freezes the application and I cannot do anything other than terminate it. It doesn't even show the window after "Me.Show".

Also, if I compile the program and then make the calling module use this compiled exe by specifying path to exe then for some reason it works perfect.

If this sounds confusing then what I mean is, I have multiple forms. If I call the code in module A to load and display module B then it gives me the exception but if I call the code in module A to run the compiled exe of module B then it runs perfectly.

Any suggestions?

Jay
  • 89
  • 1
  • 12

1 Answers1

2

When WPF creates a user interface it created a thread that is responsible for handling all the user interaction events and scheduling the rendering. This is called the dispatcher thread. Many of the objects that it creates are sub classes of DispatcherObject.

You can't call methods on a DispatcherObject from threads other then the Dispatcher thread that created them. The reasons why are complicated but relate to COM interop.

When you are in a Buttons click event you are running on dispatcher thread.

If you are coming from another thread you must get your work to be performed on the dispatcher thread. It can typically be found by accessing the static current dispatcher Dispatcher.CurrentDispatcher, unless your creating multiple dispatcher threads.

However I would suggest explaining your problem in terms of what work your trying to do with regards to having one form show ui on another. There are multiple ways like an EventAggregator to communicate between ui that might be more appropriate.

Clemens
  • 123,504
  • 12
  • 155
  • 268
dave
  • 185
  • 1
  • 4
  • There was a application which was made in windows form and worked perfectly. I have recreated the same thing in WPF and it also uses the exact same code (Except few syntax renaming to adjust to WPF). I get the exception in this WPF window and it never came in the windows form. Also, I tried using Dispatcher.CurrentDispatcher. I still get the same exception. Interesting thing to me is that if I compile the application to a exe and then run this new exe it works perfectly. It gives these errors only when its goes through the code. – Jay Sep 09 '15 at 20:32
  • Basically there is one main application which calls multiple other applications/class libraries. The application I converted to WPF is something like scanner for all fingerprints and palm prints. This is called when the user clicks on Fingerprint scan", the entire UI for the scanning application should load and should display which finger should be scanned. The sequence of fingers and related things are controlled by the class libraries and not the scanning application. This is what is causing the problem I assume, but this never happened to the old windows form version of the application. – Jay Sep 09 '15 at 20:49
  • Jay porting applications from one GUI framework to another is a difficult task. What is the reason for the multiple executable? You can have multiple applications communicate with wpf but I suspect there might be an easier way to achieve the business goal. http://stackoverflow.com/questions/11133947/how-to-open-second-window-from-first-window-in-wpf – dave Sep 10 '15 at 17:59