cross-thread exception in page view

5 posts, 0 answers
  1. Saif
    Saif avatar
    5 posts
    Member since:
    Feb 2005

    Posted 04 Oct 2010 Link to this post

    my application uses BackgroundWorker to initialize user controls and load data models for the control to keep the user interface responsive. i have a custom control which uses page view to show different custom user controls. i load data for the control and instantiate the control in a background thread. after initializations the data model and the control is added to a global list of controls. the main ui then shows the control in a tab document. now when i try to add it to the tab the page view page object throws an exception mentioning cross thread access. i have checked for cross thread access before adding the control to the tab strip. it only happens if i use a page view control inside the my custom user control. instead if i use a tab strip than page view in my custom control it works fine.

    how can i avoid this exception?
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 04 Oct 2010 Link to this post

    Hello Danilo,

    I would suggest using a SynchronizationContext(http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext%28v=VS.90%29.aspx) in order to send data from a background thread to the main thread in a safe way,

    it's really easy to use, understand and maintain, you can create a private member like:
    private SynchronizationContext synchronizationContext:
     then, in the form constructor just say:
    synchronizationContext = SynchronizationContext.Current;
    and when you get a callback from a background thread:
    public void SomeMethodCallFromBackgroundThread(object data)
    {
            // prepare data
            // send data on the main thread
            synchronizationContext.Post(ReceivedData, someObject);
    }
     
    private void ReceivedData(object data)
    {
            // this is the main thread, handle data
    }

    Usually, even though you are checking with InvokeRequired, in some cases InvokeRequired is false even though it needs to be accessed from the main thread, that's why i prefer using this type of event to be sure that I'm avoiding CrossThreadCalls Exceptions.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 07 Oct 2010 Link to this post

    Hello Danilo,

    Thank you for your question.

    I tried following your scenario and created a UserControl with a RadPageView in it. I added one more RadPageView and a BackGroundWorker in a windows form. I used the following code to initialize the user control and added it to the RadPageViewPage in the form:
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        DoWork();
    }
     
    private void DoWork()
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new MethodInvoker(DoWork));
        }
        else
        {
            UserControl1 userControl1 = new UserControl1();
            this.radPageViewPage1.Controls.Add(userControl1);
        }
    }

    This scenario does not throw exception.

    If this and Emanuel's answers do not help you find the reason for the issue in your case, please create a support ticket and attach your project to it. It will help us to investigate it further.

    Best regards,
    Alexander
    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
  5. Kamal Shammugalingam
    Kamal Shammugalingam avatar
    4 posts
    Member since:
    Sep 2009

    Posted 06 Dec 2012 Link to this post

    I had a similar experience. I was doing the design of a user configuration form in a worker thread and after the data was gathered I would call show method on the form. When it was shown I was synchronizing the thread using Invoke Required. All the other controls worked fine except for PageViewPage.

     

     I was using the Page View in a custom user control and adding PageViewPages into the PageView in the worker thread. It sometimes took forever to add a simple test PageViewPage. Sometimes it added but nothing showed up in the PageView, but when you close or hit the arrow on PageView I would get the cross-thread exception on the dispose methods. It was baffling.

     

    I got it solved by not adding the PageViewPage on the constructor but on the Form.Load event handler.

  6. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 11 Dec 2012 Link to this post

    Hello Kamal,

    Thank you for writing.

    I am glad you found a way to avoid the exception. Nevertheless, I kindly ask you to open a support ticket and attach a sample project that reproduces the error. This will allow us to debug the issue locally and improve the RadPageView control.

    Let me know if you encounter further issues. I will do my best to assist you.

    Regards,
    Boryana
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top
UI for WinForms is Visual Studio 2017 Ready