-2

I want to add a loading form in a function

private async void Test()
{
    // 1, show loading form

    await DoTest();

    //2, close loading form
}

the loading form need to block the main form, at first, I try to run the form ShowDialog(), but it will stop the following function

What's the best idea to do this?

My final solution:

private async void Test()
{
    // 1, show loading form
    var loadingScreen = new LoadingForm();
    Task.Factory.StartNew(delegate
    {
        Invoke((Action)delegate
        {
            loadingScreen.ShowDialog(App.Views.Forms.RobotActiveView);
        });
    });

    await DoTest();

    //2, close loading form
    Invoke((Action)delegate
    {
        loadingScreen.Close();
    });
}
CAM
  • 15
  • 2
  • 8
  • is the whole point of the loading form to prevent user interaction? – Alex W Jun 24 '16 at 06:47
  • @AlexW yes, as the download function, loading data and so on... – CAM Jun 24 '16 at 07:04
  • http://stackoverflow.com/a/393870/366904 – Cody Gray - on strike Jun 24 '16 at 07:05
  • @CodyGray not show the splash screen at startup application. the loading screen used to waiting the "BIG" function – CAM Jun 24 '16 at 07:09
  • @CodyGray this is not a duplicate case...... – CAM Jun 24 '16 at 07:15
  • Looks like the same problem to me. How is it any different that you want to display a splash screen when displaying the main form at startup, versus displaying a splash screen while displaying another form? – Cody Gray - on strike Jun 24 '16 at 07:16
  • @CodyGray Sorry, maybe my question is not clear. it is NOT for startup form. I just want to show the "Waiting Screen" when user click the button to submit the request. – CAM Jun 24 '16 at 07:22
  • You have not yet explained how that is a fundamentally different problem, or why it would require a different solution. – Cody Gray - on strike Jun 24 '16 at 07:23
  • @CodyGray I don't know why they are the same problem... – CAM Jun 24 '16 at 07:31
  • Because I would literally post exactly the same answer as KeithS, except instead of telling you to create the splash screen in the Main method, I'd say to create it in whatever method you run your code. You ask "What's the best way to do this?", and that question is already answered in the duplicate. I do not understand what your problem is. – Cody Gray - on strike Jun 24 '16 at 07:34
  • @CodyGray Okay, maybe they are the "SAME", I added my final solution. and thx for your information. – CAM Jun 24 '16 at 09:04

1 Answers1

1

Show loading form and disable current form.

this.Enabled = false;

var loadingForm = new LoadingForm();
loadingForm.Show();

...

loadingForm.Close();

this.Enabled = true;
Tommy
  • 3,044
  • 1
  • 14
  • 13
  • Good idea, gives me an inspiration, but my program is MVP, so I will try to disable the form at Presenter. – CAM Jun 24 '16 at 06:48