Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add components on form asynchronously

I'm working with WinForms I faced with following problem. I should dynamically create and add on form two tabs in concrete period of time.

This is main layout

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

I try add my elements in next way:

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

Methods ShowView works perfectly. But when I call methods one by one the performance of program is a bit slow. So I decided to use multithreading in next way:

     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();

Those approaches work identically. Could you please give me an advice how to fix this problem with multithreading?

like image 727
Ivan Ivanov Avatar asked Jan 21 '26 06:01

Ivan Ivanov


1 Answers

You can't multithread this way because they are both invoking the UI thread. You would need to create your general report form on a separate thread, then add your report to an object on your main UI thread.

like image 69
DoomVroom Avatar answered Jan 23 '26 20:01

DoomVroom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!