0

I am working on a user interface in C#.

When the program is running (this process takes several minutes...), before run, I want a message is displayed and after run, the message will disappear automatically.

My aim is to give an information message like 'Running, please wait' to the user.

I tried the code shown below: (formMsgWait has only a label 'Running, please wait')

private void btnExit_Click(object sender, EventArgs e)
{
    using (formMsgWait fMsgWait = new formMsgExit())
    {
        fMsgWait.Show();
        System.Windows.Forms.Application.DoEvents();

        ...statement 1
        ...statement 2
        ...
    }
}

When run to System.Windows.Forms.Application.DoEvents(); the program doesn't run continue, so all of the statements below doesn't do (...statement 1, ...statement 2, ...), formMsgWait doesn't close.

Is there anyway to do that?

Any tips on these will be great help.

Kay Lee
  • 922
  • 1
  • 12
  • 40
Oosutsuke
  • 79
  • 3
  • 12
  • 1
    do you want display a splash screen? or you just want to display a wait message to user until your app gets loaded. – Brijesh Feb 24 '17 at 08:28
  • Hi Brijesh, i just want to display a wait message to user until your app gets loaded. ( I click buttonA -> while processing, display form message wait -> when end process -> form message close, and form of functionA display ) – Oosutsuke Feb 24 '17 at 08:31
  • use this link... This should solve your problem: http://stackoverflow.com/questions/15836027/c-sharp-winform-loading-screen – Brijesh Feb 24 '17 at 08:37
  • I think, you should go with splash screen, you can show your message on splash screen and which is meant to show until the app loads. You can refer to http://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application and http://www.c-sharpcorner.com/UploadFile/1e050f/splash-screen-for-windows-form-application-C-Sharp/ – Hitesh Feb 24 '17 at 08:39
  • Thank Brijesh, Hitesh Mistry. – Oosutsuke Feb 24 '17 at 09:04

2 Answers2

1

You're blocking the current thread. According to http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx

Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see Asynchronous Programming Overview.

So that's what you should actually be doing here: start all of your actual work on a separate thread, preferably using async. For example:

public async Task<bool> DoTheWorkAsync()
{
    formMsgWait f = new formMsgWait();
    f.Show();
    bool finished = await Task.Run(() => DoTheWork());
    f.Close();
    return finished;
}

private bool DoTheWork()
{
    ... work
    return true;
}
Drakestar
  • 698
  • 10
  • 27
  • Hi Drakestar,Then, I call method DoTheWorkAsync(), inside of event `private void btnExit_Click(object sender, EventArgs e) { DoTheWorkAsync(); }` ? – Oosutsuke Feb 24 '17 at 08:57
  • Async methods are called just like regular methods. The return type is the T type inside the Task. You can call this as bool foo = DoTheWorkAsync(); or without assigning its return type (as you wrote above). – Drakestar Feb 24 '17 at 09:01
0

Oh. I think you can use Dialog Window. Visit this link to refer: https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx

Tomato32
  • 2,145
  • 1
  • 10
  • 10