0

I'm seeing an issue where having multiple UI threads with Lync 2010 SDK controls are causing the program to crash.

The error I'm running into is:

{"Must create DependencySource on same Thread as the DependencyObject."}

If someone can give me an explanation as to why this happens ,or how to get around this. It will be great!

<Window x:Class="Lync.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:lync="clr-namespace:Microsoft.Lync.Controls;assembly=Microsoft.Lync.Controls"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <lync:PresenceIndicator Source="sip:gawicks@home.com"/>
            <Button Height="20" Width="20"  Click="Button_Click"> </Button>        
        </StackPanel>
    </Window>



  private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Simply creating another UI thread on button click https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/
            Thread thread = new Thread(() =>
            {

                MainWindow w = new MainWindow();
                //Crash!!
                w.Show();

                w.Closed += (sender2, e2) =>
                 w.Dispatcher.InvokeShutdown();

                System.Windows.Threading.Dispatcher.Run();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

        }
gawicks
  • 1,894
  • 15
  • 22
  • The issue has nothing to do with LYNC, rather it's an issue with attempting to spin up a new GUI on some thread other than the thread the current window is running. Get rid of your Thread logic and you'll be ok. – JWP Dec 28 '15 at 05:24
  • The requirement IS to run on a new thread. Which should work https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/. All other controls work fine. I can confirm that the lync control is causing the problem. – gawicks Dec 28 '15 at 05:28
  • Try setting thread.IsBackground = true after setting the ApartmentState – William Dec 28 '15 at 05:29
  • Ok. One other thing you can try is setting the SynchronizationContext (in the thread start, before the main window initialization) like so: `SynchronizationContext.SetSynchronizationContext( new DispatcherSynchronizationContext( Dispatcher.CurrentDispatcher));` – William Dec 28 '15 at 05:34
  • The Lync client SDK is kind of garbage. It has some serious issues with threading. It'd be nice if Microsoft would ever get around to an update - but as it is, it is easier to build your own UI elements that interact with a decently multi-threaded UCMA or UCWA backend if that is at all possible. – EricRRichards Aug 31 '16 at 14:01

3 Answers3

1

Seems like this is indeed a limitation of the Lync controls. Internally these controls use the Lync client. And the lync client being a COM wrapper likes to live on a single UI thread.

So instead I came up with my own control. Wrote a facade over the lync client which lives on a single UI thread. And Dispatched all calls to it. And Voilà its working!

gawicks
  • 1,894
  • 15
  • 22
0
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //Simply creating another UI thread on button click https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/
        Thread thread = new Thread(() =>
        {
            SynchronizationContext.SetSynchronizationContext(
            new DispatcherSynchronizationContext(
            Dispatcher.CurrentDispatcher));

            MainWindow w = new MainWindow();
            //Crash!!
            w.Show();

            w.Closed += (sender2, e2) =>
            w.Dispatcher.InvokeShutdown();

            System.Windows.Threading.Dispatcher.Run();
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

    }

Try setting the SynchronizationContext of the newly created thread as shown above.

William
  • 1,837
  • 2
  • 22
  • 36
  • What is happening in the constructor for `MainWindow`? If you strip it down to `InitializeComponent` does it still throw the error? – William Dec 28 '15 at 05:40
  • Yes ive got zero logic in there except InitializeComponent(). – gawicks Dec 28 '15 at 05:42
  • And just out of curiosity if you remove this line from the xaml `` does it still throw the error?. If it doesn't then it does indeed look like it's to do with LYNC – William Dec 28 '15 at 05:45
  • edit : sorry I meant that the error goes away if is removed. So I'm fairly confident this is lync specific. – gawicks Dec 28 '15 at 05:45
0

None of the solutions given here will work if Lync itself dissallows more than a singleton instance of Lync. If I recall, this is the case as the designers wanted it that way. Lync simply will not start twice.

JWP
  • 6,672
  • 3
  • 50
  • 74
  • yes Im aware. There are several equivalent approaches to creating a WPF UI thread . The question is rather , why the lync sdk behaves erratically in this situation. – gawicks Dec 28 '15 at 05:47
  • Ok, but the code above doesn't show the starting of the dispatcher on the new thread, per Reed's article. Can you try that? – JWP Dec 28 '15 at 05:56
  • Maybe we were misunderstanding the issue a bit, does the exception happen in the button click logic or when the mainwindow is shown prior to the button click? – JWP Dec 28 '15 at 05:57
  • The button click logic fires off a new UI thread. It's here that the exception happens. – gawicks Dec 28 '15 at 05:59