开发者

No Load Complete event for windows form

开发者 https://www.devze.com 2023-03-30 19:37 出处:网络
In windows form on click on Next I need to display other form and start some processing. Am coding in .Net C#

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.

No Load Complete event for windows form

The actual output must be all controls should be loaded and then processing must start.

No Load Complete event for windows form

I need to call the method to start processing after the 开发者_JAVA技巧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();
}


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消