Resolved Refresh Winform Crystal Report Simultaneously

dv2020

Active member
Joined
Dec 18, 2020
Messages
30
Programming Experience
1-3
Hi All,

I'm trying to have all three of my crystal reports refresh at the same time. I am using the code below but receiving an error.

C#:
       //Allowing three task to execute at a time
                ParallelOptions parallelOptions = new ParallelOptions
                {
                    MaxDegreeOfParallelism = 3
                };

                     Parallel.Invoke(
                 parallelOptions,
                     () => this.crystalReportViewerProfileSum.RefreshReport(),
                     () => this.crystalReportViewer1.RefreshReport(),
                     () => this.crystalReportViewerBRL.RefreshReport()
                 );


"Controls created on one thread cannot be parented to a control on a different thread."

I'm not to sure why, as i though running with Parallel would generate separate controls.

Any ideas?

Thanks

David
 

Attachments

  • error.PNG
    error.PNG
    3.9 KB · Views: 13
Solution
Each of your this.crystalReportViewer* was created on the main thread. Your parallel invoke to refresh the report is trying to create the controls on other threads, but they are trying to attach to that parent object that was in the main thread.

Also consider what happens if one of those threads you have has to hit a wait state before it completes all its work, or simply gets paused and scheduled out because it was taking too long. The thread that is currently running on is put back into the thread pool. Later when the wait state ends, or the scheduler tries to continue running the paused thread, there is a high probability that a different thread from the thread pool will be assigned to continue running your code. Now you hit...
Each of your this.crystalReportViewer* was created on the main thread. Your parallel invoke to refresh the report is trying to create the controls on other threads, but they are trying to attach to that parent object that was in the main thread.

Also consider what happens if one of those threads you have has to hit a wait state before it completes all its work, or simply gets paused and scheduled out because it was taking too long. The thread that is currently running on is put back into the thread pool. Later when the wait state ends, or the scheduler tries to continue running the paused thread, there is a high probability that a different thread from the thread pool will be assigned to continue running your code. Now you hit another case where the controls may not all belong to the same thread.

Parallel code is meant to do CPU or I/O intensive background work. If you need to do UI work across multiple threads, you'll need to create threads yourself, and mark them as UI threads.
 
Solution
Each of your this.crystalReportViewer* was created on the main thread. Your parallel invoke to refresh the report is trying to create the controls on other threads, but they are trying to attach to that parent object that was in the main thread.

Also consider what happens if one of those threads you have has to hit a wait state before it completes all its work, or simply gets paused and scheduled out because it was taking too long. The thread that is currently running on is put back into the thread pool. Later when the wait state ends, or the scheduler tries to continue running the paused thread, there is a high probability that a different thread from the thread pool will be assigned to continue running your code. Now you hit another case where the controls may not all belong to the same thread.

Parallel code is meant to do CPU or I/O intensive background work. If you need to do UI work across multiple threads, you'll need to create threads yourself, and mark them as UI threads.
Hi

Thanks for replying. Its not designed for intensive work, basically have a dashboard on a timer and want the 3 x dashboards to refresh at the same time as they all hit different underlying queries in the database.

Understanding from what you have explained, I need to create each crystalreportviewer on its own UI thread?
 
Yes, and that is assuming that WinForms will let you do it. The underlying Win32 API will let you, but WinForms tried to simplify things.
 
Back
Top Bottom