Add components to a form asynchronously

I am working with WinForms which I am facing the following problem. I have to dynamically create and add on two tabs at a specific time period.

This is the main layout

IMainGeneralReportForm mainGeneralReportLayoutForm =
       ObjectFactory.GetOrCreateView<IMainGeneralReportForm>();

      

I am trying to add my elements like this:

    ObjectFactory.ShowView<IGeneralReportSimpleView>();
    ObjectFactory.ShowView<IGeneralReportAdvancedSearchView>();

      

The methods ShowView

work fine. But when I call the methods one by one, the program performance is a little slow. So I decided to use multithreading like this:

     MainGeneralReportForm generalReportForm = mainGeneralReportLayoutForm as MainGeneralReportForm
generalReportForm.Invoke(new SimpleViewDelegate(() =>
            {
                return ObjectFactory.ShowView<IGeneralReportSimpleView>()
            }));

generalReportForm.Invoke(new AdvancedViewDelegate(() =>
            {
                return ObjectFactory.ShowView<IGeneralReportAdvancedSearchView>()
            }));

    private delegate IGeneralReportSimpleView SimpleViewDelegate();
    private delegate IGeneralReportAdvancedSearchView AdvancedViewDelegate();

      

These approaches work the same way. Could you please give me some advice on how to solve this problem when multithreading?

+3


source to share


1 answer


You cannot use multithreading because they both invoke the UI thread. You will need to create your generic report form on a separate thread and then add the report to the object on the main UI thread.



+1


source







All Articles