Sunil Agarwal
Sunil Agarwal

Reputation: 4277

No Load Complete event for windows form

In windows form on click on Next I need to display other form and start some processing. Am coding in .Net C#

The problem is before the form is complete visible, the method gets triggered and the processing starts and the UI looks like it crashed. Processing started even before controls are loaded. and once processing is done all controls are visible. enter image description here The actual output must be all controls should be loaded and then processing must start. enter image description here

I need to call the method to start processing after the form (user control) is visible and is loaded completely.

I tried this and this, but no luck.

Added code:

private void FeatureRemovalControl_Load(object sender, EventArgs e)
{
    pictureBox2.Image = Properties.Resources.line;
    prgbar.Value = 0;
    //code to load images and some other stuff
    StratProcess();
}

Upvotes: 1

Views: 3036

Answers (3)

Iain Ward
Iain Ward

Reputation: 9936

Try calling that method at the end of the FormLoad event, the control should have finished loading by then. If it hasn't that you may need perform some checks and possibly create a custom event that fires when you're happy that it is ready.

Another solution is to have a button that the user must press to trigger the processing, which they will only be able to click once everything has loaded

EDIT: The reason it look's like it's happening is because you're starting the process in one of the control's load method, which I assume is not the last control to load, so it's starts processing before the other controls are given a chance to load. Make StratProcess method public and call it in the FormLoad method of the parent form instead, like so:

private void ParentForm_Load(object sender, EventArgs e)
{
     FeatureRemovalControl.StratProcess(); // Should it be called StartProcess instead?
}

Beware though this is still doing the processing on the UI thread, so the screen may appear to 'hang' whilst this is happening so I advise you move it to a background thread as others have suggested.

Upvotes: 0

slawekwin
slawekwin

Reputation: 6310

Best way, if you ask me, would be to start processing asynchronously, so that you maintain full control of the UI and process at the same time.

http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.71).aspx

Upvotes: 2

Roger Lipscombe
Roger Lipscombe

Reputation: 91835

You're calling StartProcess (which seems to block until it's finished) from your UI thread. Because WinForms repaints occur on that thread, nothing is painted, and it appears that your process has hung. You should look at using a BackgroundWorker, or other way to call StartProcess asynchronously.

Upvotes: 3

Related Questions