0

I am calling COM dll in WPF and want that UI doesn't Hang. So far i have used Task.Run approaches but no result.

Here is the code

   public MainWindow()
        {
            InitializeComponent();

            Task.Run(() => DoWork());

        }


 private  void DoWork()
    {

         Task.Run(() =>
        {
            Allocate();

        });

    }



 void Allocate()
        {

            int idwErrorCode = 0;


            try
            {  
                bIsConnected = axCZKEM1.Connect_Net("192.168,2,2", 3703);


            }

the COM method axCZKEM1.Connect_Net("192.168,2,2", 3703) execution takes time and UI hangs up.

Need a simple solution.

Singleton
  • 3,701
  • 3
  • 24
  • 37
  • Did you try to use threads ? https://msdn.microsoft.com/en-us/library/ms173178.aspx That would explicitely use another thread to run your COM method – Irwene Mar 04 '16 at 08:11
  • And.... you're running a Task..... Inside another Task ? – Irwene Mar 04 '16 at 08:13
  • Threading is not a minor detail, the COM runtime knows whether or not the object is thread-safe and which thread owns the object. So when you call a method on a worker thread then COM takes care of automatically marshaling the call to the thread that owns the object. Very similar to the way Dispatcher.Invoke() works but completely automatic. So your UI thread still hangs. Workaround is to *create* the object on a worker thread so that this thread owns it and executes its methods. It must be STA and pump a message loop, [like this](http://stackoverflow.com/a/21684059/17034). – Hans Passant Mar 04 '16 at 09:09
  • Fwiw, the "ax" prefix spells doom, suggests that it is actually an ActiveX control with a user interface and that you used the AxHost wrapper and ElementHost to put on a WPF window. Can't run that one on another thread. Do this early, use a splash screen to hide the delay. – Hans Passant Mar 04 '16 at 09:14

0 Answers0