I
I
Ivan Vyrov2016-01-24 06:55:13
C++ / C#
Ivan Vyrov, 2016-01-24 06:55:13

How to implement a loading screen when working with an application?

Good afternoon!
There is a program that transmits information from the client to the server with confirmation from the server of the correctness of the input (operation time from 5 to 10 seconds), at this time it is necessary to organize a download window (something like "Please wait, the entered data is being sent and verified"). As soon as the server responds to the client about the correctness of the input, it is necessary to hide the download window and continue working with the program.
The request to push in what direction to dig and if it is possible that to throw the link to examples of such operations.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-01-24
@ProKiLL

1. Create an information window.
2. Show it through ShowDialog
3. In this window on Shown, create and run a BackgroundWorker in which your request is executed.
4. On RunWorkerCompleted, close the modal window.

A
Andrew, 2016-01-24
@byte916

You can use async/await to solve this problem. To do this, you need to make the called function return type Task (or generic - Task<the desired type>), and add the async keyword (in this example, the Run() method). At the call site, you need to add await. You also need to add async to the method from which the call is made (in this example, it is button1_Click, but it is not necessary to add Task to it - this method is called synchronously).

private async void button1_Click(object sender, EventArgs e)
        {
            await Run();
        }

        public async Task Run()
        {
            var form = new Form2();
            form.Show();
//Тут делаем нужную работу
            form.Close();
        }

In this example, the Run method will be called asynchronously, without blocking the main thread. It will create a window and on completion the window will close and the thread will be destroyed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question