This is a migrated thread and some comments may be shown as answers.

cross-thread exception in page view

4 Answers 138 Views
PageView
This is a migrated thread and some comments may be shown as answers.
Sadrul
Top achievements
Rank 2
Sadrul asked on 04 Oct 2010, 11:04 AM
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?

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 12:01 PM
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
0
Alexander
Telerik team
answered on 07 Oct 2010, 11:42 AM
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
0
Kamal Shammugalingam
Top achievements
Rank 1
answered on 06 Dec 2012, 02:04 PM

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.

0
Boryana
Telerik team
answered on 11 Dec 2012, 10:48 AM
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.
Tags
PageView
Asked by
Sadrul
Top achievements
Rank 2
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Kamal Shammugalingam
Top achievements
Rank 1
Boryana
Telerik team
Share this question
or