MultiThreding is not supported with DragAndDropManager

8 posts, 0 answers
  1. Sanket Singhvi
    Sanket Singhvi avatar
    20 posts
    Member since:
    May 2010

    Posted 08 Jun 2011 Link to this post

    Hi,

    Looks like RadDragAndDropManager is not ThreadStatic which causes application crash as We have UI in two different threads.

    I have RadDragAndDropManager.EnableNativeDrag = true set in my Base control Constructor. 

    Now that base control is used at multiple places in my application. 
    In our main UI thread we have this control and from this thread we started another thread which has UI and in that window we make use of our control at many places in various window forms. And so whenever we open a window which contain that base control lookslike RadDragAndManager call the reset function on  DesktopDragDropProvider object. 

    But when I go back to first UI Thread and click on the control that is using RadDragAndDropManager UE occurs.

    ************** Exception Text **************
    System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
       at System.Windows.Threading.Dispatcher.VerifyAccess()
       at System.Windows.Threading.DispatcherObject.VerifyAccess()
       at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
       at System.Windows.Controls.ContentControl.get_Content()
       at Telerik.Windows.Controls.DragDrop.UndetectableWindow.set_Child(FrameworkElement value)
       at Telerik.Windows.Controls.DragDrop.DesktopDragDropProvider.Reset()
       at Telerik.Windows.Controls.DragDrop.DesktopDragDropProvider.OnLeftMouseButtonUp(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Xm8.App.X.Program.ShowControlCenter(ControlCenterParameters ccParams)
       at Xm8.App.X.Program.ShowCC(Object parameter)
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart(Object obj)


    In my understanding RadDragAndDropManager should be ThreadStatic rather than just the static. that might solve the problem.

    Regards
    Sanket
  2. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 09 Jun 2011 Link to this post

    Hi Sanket Singhvi,

    Would it be possible to share with us the version of our controls used, so we can check the exact cause of this issue and resolve it.

    All the best,
    Tsvyatko
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Sanket Singhvi
    Sanket Singhvi avatar
    20 posts
    Member since:
    May 2010

    Posted 09 Jun 2011 Link to this post

    We have downloaded the last release on April 19th 2011 and using those dlls. 
    Also please find the attached Smaple projects.

    You should be able to reproduce the defect with following steps:
    1) Run Application
    2) Click on Show Form Button
    3) It will show the GridWindow on another thread 
    4) click on RadGridView in both forms couple of times
    5) Close GridWindow.
    6) Click on First Form. Cllick on RadGridView (May be on header or row or try sorting ) . You should be able to get UE.


    Attached file is a zip file. so rename the extension to .zip and then extract.

    GridControl.xaml.cs
    -----------------------------
    using System.Data;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.DragDrop;


    namespace ThreadingDefect
    {
        /// <summary>
        /// Interaction logic for GridControl.xaml
        /// </summary>
        public partial class GridControl
        {
            private DataTable _dataTable;
            private GridViewDataColumn _colA;
            private GridViewDataColumn _colB;
            private GridViewDataColumn _colC;


            public GridControl()
            {
                InitializeComponent();
                RadDragAndDropManager.EnableNativeDrag = true;
            }


            public void Initialize()
            {
                Init();
            }


            private void Init()
            {
                CreateGrid();
                GetDataTable();
                FillGrid();
            }


            private void FillGrid()
            {
                DataRow row = _dataTable.NewRow();
                row["_colA"] = "1";
                row["_colB"] = "This is a Dummy Record";
                row["_colC"] = "This is Third Column";
                _dataTable.Rows.Add(row);


                _mainGrid.ItemsSource = _dataTable;
            }


            private void CreateGrid()
            {
                _mainGrid.AutoGenerateColumns = false;
                if (_colA == null)
                {
                    _colA = new GridViewDataColumn();
                }


                //
                //  _ColA
                //
                _colA.UniqueName = "_colA";
                _colA.Header = "Column A";
                _colA.IsGroupable = false;
                _colA.IsFilterable = false;
                _colA.IsReadOnly = true;
                _colA.IsVisible = true;
                _colA.Width = 100;


                if (_colB == null)
                {
                    _colB = new GridViewDataColumn();
                }


                //
                //  _colB
                //
                _colB.UniqueName = "_colB";
                _colB.Header = "Column B";
                _colB.IsGroupable = false;
                _colB.IsFilterable = false;
                _colB.IsReadOnly = true;
                _colB.IsVisible = true;


                //
                //  _colC
                //
                if (_colC == null)
                {
                    _colC = new GridViewDataColumn();
                }
                _colC.UniqueName = "_colC";
                _colC.Header = "Column C";
                _colC.IsGroupable = false;
                _colC.IsFilterable = false;
                _colC.IsResizable = false;
                _colC.Width = 150;


                _mainGrid.Columns.Clear();
                _mainGrid.Columns.Add(_colA);
                _mainGrid.Columns.Add(_colB);
                _mainGrid.Columns.Add(_colC);


                //_mainGrid.Width = 480;
                _mainGrid.ShowGroupPanel = false;
            }


            private void GetDataTable()
            {
                if (_dataTable == null)
                {
                    _dataTable = new DataTable();
                }
                _dataTable.Columns.Add("_colA");
                _dataTable.Columns.Add("_colB");
                _dataTable.Columns.Add("_colC");
            }
        }
    }

    GridControl.xaml
    -----------------------

    <UserControl x:Class="ThreadingDefect.GridControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <Controls:RadGridView x:Name="_mainGrid" />
        </Grid>
    </UserControl>

    GridWindow.xaml
    --------------------------
    <Window x:Class="ThreadingDefect.GridWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:ThreadingDefect="clr-namespace:ThreadingDefect"
            xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" Title="GridWindow" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ThreadingDefect:GridControl x:Name="_gridControl" Height="200"/>
            <Controls:RadButton Grid.Row="1"  Margin="10" x:Name="_closeButton" Content="Close" Click="_closeButton_Click" Width="80" Height="25"/>
        </Grid>
    </Window>

    GridWindow.xaml.cs
    ---------------------------
    namespace ThreadingDefect
    {
        /// <summary>
        /// Interaction logic for GridWindow.xaml
        /// </summary>
        public partial class GridWindow : Window
        {
            public GridWindow()
            {
                InitializeComponent();
                if (_gridControl != null)
                {
                    _gridControl.Initialize();
                }
            }


            private void _closeButton_Click(object sender, RoutedEventArgs e)
            {
                Close();
            }
        }
    }

    MainWindow.xaml:
    -------------------------
    <Window x:Class="ThreadingDefect.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:ThreadingDefect="clr-namespace:ThreadingDefect"
            xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" Title="MainWindow" SizeToContent="WidthAndHeight">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ThreadingDefect:GridControl x:Name="_mainGrid" Height="200"/>
            <Controls:RadButton Grid.Row="1"  x:Name="_showButton" Height="25"  Width="80" Click="_showButton_Click" Content="Show Form" Margin="10"/>
        </Grid>
    </Window>

    MainWindow.xaml.cs
    ---------------------------
    using System.Threading;
    using System.Windows;


    namespace ThreadingDefect
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                if (_mainGrid != null)
                {
                    _mainGrid.Initialize();
                }
            }


            private void _showButton_Click(object sender, RoutedEventArgs e)
            {
                var th = new Thread(ShowForm);
                th.TrySetApartmentState(ApartmentState.STA);
                th.Start();
            }


            private void ShowForm()
            {
                var win = new GridWindow();
                win.ShowDialog();
            }
        }
    }


    I was not able to attach the project as it says I can only attach image files. So I just pasted the code above for all 6 Files. You should be able to create a Testprojects. Let me know if you need anything else.

    Regards
    Sanket
  5. Yordanka
    Admin
    Yordanka avatar
    634 posts

    Posted 10 Jun 2011 Link to this post

    Hi Sanket Singhvi,

    Thank you for the details.
    We were able to reproduce the problem and we are currently investigating it. We will contact you as soon as we have more information.
     
    Kind regards,
    Yordanka
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Sanket Singhvi
    Sanket Singhvi avatar
    20 posts
    Member since:
    May 2010

    Posted 15 Jun 2011 Link to this post

    Hi,

    I just wanted to know the status of this defect. It was really good that you were able to produce it.

    Please let me know as it is very critical for us.

    Regards
    Sanket
  7. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 17 Jun 2011 Link to this post

    Hello Sanket Singhvi,

     We had similar issue in such scenario in version you are currently using. You can check our latest internal build where this issue is addressed.  Let us know if you have any further issues regarding this scenario.

    Best wishes,
    Tsvyatko
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Sanket Singhvi
    Sanket Singhvi avatar
    20 posts
    Member since:
    May 2010

    Posted 17 Jun 2011 Link to this post

    Hi,

    I downloaded the latest Internal release today but defect is still present. Not solved.

    Regards
    Sanket
  9. Yordanka
    Admin
    Yordanka avatar
    634 posts

    Posted 20 Jun 2011 Link to this post

    Hello Sanket Singhvi,

    Unfortunately, this fix didn't make it to our last internal build, so it is included in the one issued today. Please excuse us for the misunderstanding.
     
    Regards,
    Yordanka
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready