Search code examples
c#.netwpflynclync-client-sdk

Lync SDK . Issue with Multiple UI threads


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:[email protected]"/>
            <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();

        }

Solution

  • 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!