Wait for Winform UserControl to load

2019-06-13 16:46发布

问题:

I have a custom Winform Infopath UserControl which loads a form from a sharepoint library. I have a series of function calls, after opening the form, to pull some data from the form, even a take screenshot function. But the form load takes a lot of time, and the other functions finish up too quickly, much before the form load, which gives me wrong results.

Is there any way I can have a wait function, that waits for the infopath form to finish loading before the other functions are called?(in c#)

--Update

Example Code:

Inside the UserControl, I have a form initialize function, that basically loads the form

public void InitializeInfoPathForm(string myurl)
        {
            if (this.IsInitialized) return;

            CreateForm(new Uri(myurl),null);
        }

public void CreateForm(
            Uri formUrlName,
            Stream dataStream)
        {
            TestInitialization();

            try
            {
                this.formControl.Close();

                // Open / create a form
                if (dataStream != null)
                    formControl.Open(
                        formUrlName.ToString()
                        );
                else
                {
                    formControl.Open(
                        formUrlName.ToString());
                }
                RefreshView(UIStatesForm.DocumentReadMode);
                }
            catch (Exception)
            {
                RefreshView(UIStatesForm.NoDocumentAvailable);

                throw;
            }
        }

The Winform looks like this:

public partial class Form1 : Form    
{    
     public Form1(string sharepoint_url)    
     {    
        InitializeComponent();

         this.infoPathUserControl1.InitializeInfoPathForm(sharepoint_url);
         takescreenshot();         
     }
}

I tried putting the takescreenshot() in a Form1_Load and Form1_Shown eventhandlers, but still the screenshots occur much earlier than the form load because FormControl.Open() takes a lot of time. I could put the screenshot function in a button_click event, but i want to automate this procedure. I even tried putting it ina button_click procedure and called button.PerformClick from the Form_Load eventhandler.

Pls Advice.

回答1:

You use the event UserControl.Activated for the purpose

UserControl1.Activated += new EventHandler(SeriesOfFunctions);

Then write your code in this SeriesOfFunctions

You have other choices like UserControl.Load,UserControl.Activating



回答2:

You can define a bool and set it to false, after your infopath has been loaded, set it to true and only in that case start the intialize_component in your form constructor. or something like that!



回答3:

If I am understanding your question correctly, it seems as though you are running into a race condition where you have tasks executing on separate threads.

If you can... I'd use the On_Load function of the Form; however, if you need to have separate threads for speed so that the background tasks don't have to wait for the form prior to starting, take a look at the ManualResetEvent. You could have the form.on_load event call set on the event once it is correctly loaded.