Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Richtextbox - ScrollBarVisibility is giving exception while opening the WPF window second time

$
0
0

Hi,

I am getting the exception "Cannot use a DependencyObject that belongs to a different thread than its parent Freezable." while opening (second time on words) WPF window having RichTextBox with  VerticalScrollBarVisibility property.

If I remove the property it works, but i have to show the vertical scroll bar.

Using VS 2010 professional with .Net 4.0 framework and Operating System is Window7 .

Here is the Window "Window1.xaml" having RichTextBox which loads "abc.rtf" file

<RichTextBox Name="rtbEULA" IsReadOnly="True" VerticalScrollBarVisibility="Auto"  ></RichTextBox>

Opening the Window as followed

 Thread newSettingsWindowThread;

 ParameterizedThreadStart p = new ParameterizedThreadStart(ShowWindowThreadStart);
            newSettingsWindowThread = null;
            try
            {
                newSettingsWindowThread = new Thread(p);
                newSettingsWindowThread.SetApartmentState(ApartmentState.STA);
                newSettingsWindowThread.IsBackground = true;
                newSettingsWindowThread.Start(params);
            }
            catch (Exception )
            {   }

  private void ShowWindowThreadStart(object inParams)
        {
              Thread.CurrentThread.Name = "New WindowThread";

              var WinObj = new Window1();
              WinObj .WindowStartupLocation = WindowStartupLocation.CenterScreen;
              WinObj .Topmost = true;
              WinObj .Closed += (sender, e) => WinObj .Dispatcher.InvokeShutdown();
              WinObj .Show();       

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

     }

Very first time, the window can be seen and can be closed. Second time onwards the issue is there.

If I  remove the property "VerticalScrollBarVisibility" it works fine.

Please help me to figure it out the issue, is it really related to that property or any thing internally causing the issue.

Thanks in advance.




Viewing all articles
Browse latest Browse all 18858


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>