c# Label is “empty”/is a hole in the form

2019-07-23 03:51发布

问题:

I have 2 windows. The main form and the Loading form. In the main form you send a request which will be executed. Because this takes some time, I made the Loading-form with a progress bar so that the user knows the program is working.

What I want to: The Loading-form should open itself when the process ist started and close itself when it's finished. At the moment I have code that looks something like this:

    Loading.Show();
    Loading.MakeAStep();    //used for progressing the progress bar

    //program is working

    //finishes
    Loading.Visible = false;
    Loading.ResetProgress();    //Sets the value of the progress bar to 0

My problem is: The window with the progress bar opens, but there is also a label which shows "please wait". When the form opens, the progress bar works perfectly, but the label is just a hole (it really is you can look through it). When I use instead of visible = false form.Close, it works just fine with the label but I get an error when I try to start a progress in the same session. What I want/need: Either a solution to the hole-problem, or an effective way to open and close a form several times during one session.

回答1:

(Posted on behalf of the OP).

The answer is in the comments: The UI blocks and I needed to Update the form with Loading.Update(); I put that between Show and MakeAStep.



回答2:

As already mentioned by others, the problem is that you run your long running process in the UI thread. To avoid this, you should improve how the loading form receives the task and works on it:

The loading form should get the thing to run as a Task (maybe by a method Run(Task task). After getting this task the loading form can attach another action to it, what shall happen when the task is finished by using .ContinueWith() and simply closes itself when it reaches that point. After that it will Start() the task and call ShowDialog() on itself.



标签: c# forms