Let say I've a login window and a main window for my wpf application, if login successful, the user's settings and options will be retrieved from the configuration server and will be bound to the main window. In order to let those settings and options have enough time to be retrieved and bound, is it ok to set some time buffer by using thread.sleep() to hold the transition from login window to main window? Or is there any other better suggestion?
-
3Thread.Sleep will freeze your application, just as a long-running request would. – Panagiotis Kanavos Jun 25 '15 at 08:36
-
Where is the code? How are you making the calls? If they were blocking calls, you wouldn't have any synchronisation issues. Are you running the checks in a separate thread? It's possible to make asynchronous database calls and web service calls and `await` them without blocking the UI thread – Panagiotis Kanavos Jun 25 '15 at 08:43
-
@PanagiotisKanavos I believe ZWah did not make async calls. I might be mistaken, but I think (s)he fell for the common misconception that separate forms run in separate threads (or at least has separate message pumps). – mg30rg Jun 25 '15 at 09:02
-
Hi @PanagiotisKanavos and mg30rg, thanks for the comments. The structure is like this: user enters info in login window -> if info correct, then login successful -> Before proceed to main window, user's options & settings will be retrieved from the config server and passed to the main window -> After that, main window will be shown with those user settings. My concern is how to let those task finished first before proceed to main window. – YWah Jun 26 '15 at 00:21
-
How about Wait function? – YWah Jun 26 '15 at 01:31
4 Answers
Using Thread.Sleep()
in your main/GUI thread will block the execution of code in both of your windows, not only your login window, so the main window will only start to gather data after the sleep period is over.
What you can do is gather the data required by your main form asynchronously and notify your login form when the async task is finished by the means of synch objects.

- 1,311
- 13
- 24
Why don't you make use of async call to database and retrieve data from the database and show loading dialog/image, that is better solution than making thread.sleep.
Check : Asynchronous Programming with Async and Await
Example :
private void Button_Click(object sender, RoutedEventArgs e)
{
CallLongRunningMethod();
Label3.Content = "Working...";
}
private async void CallLongRunningMethod()
{
string result = await LongRunningMethodAsync("World");
Label3.Content = result;
}
private string LongRunningMethodAsync(string message)
{
Thread.Sleep(2000);
return "Hello " + message;
}
You can also check BackGroundWoker for this but Thread.Sleep is a strong no-go..

- 1,311
- 13
- 24

- 175,020
- 35
- 237
- 263
-
1Multiple reasons. 1) removing Thread.Sleep simply to reintroduce it doesn't help. You still waste a thread. 2) `Task.Delay` already provides delays and does *not* block anything - it uses a timer. 3) this code won't do anything - nothing awaits for `RunIt` so it will run in the background but won't delay anything. 4) `async void` is mainly asynchronous event handlers or event-like methods. Even if you tried to `await` `RunIt` you couldn't. 5) Console applications can't use `await` in the main thread. The application will terminate before `RunIt` has a chance to run. – Panagiotis Kanavos Jun 25 '15 at 08:40
-
In total, this code contains a lot of asynchronous programming pitfalls. In fact, it's almost the opposite of what is described in the article link – Panagiotis Kanavos Jun 25 '15 at 08:42
-
@PanagiotisKanavos - i m not saying go for solution blindly..just providing one way to achieve better way...that is reason to go though link...i worte it as Example not solution to follow... – Pranay Rana Jun 25 '15 at 08:45
-
What does this code achieve though? You can delay simply with `await Task.Delay(3000)`. Oh, 6) creating a cold task - if you are going to simply call Start right away, why don't you write Task.Run ? – Panagiotis Kanavos Jun 25 '15 at 08:47
Use thread.sleep() is definitely not the right solution. Thread.sleep() does not guaranty that the data will be retrieved after the delay you set.
Why not use a loading indicator while you are retrieving some data with an asynchronous task ?

- 2,568
- 4
- 23
- 39
I think this might be what you are looking for :
Should be something like this :
async Task Foo() {
showSplashScreen();
await Task.Delay(1000);
closeSplashScreen();
}

- 1
- 1

- 123
- 2
- 7
-
Please note that - if I understood correctly - the OP wants to perform bacground operations and user interaction (like asking for username/password) at the same time. So it is not a simple splash screen task. – mg30rg Jun 25 '15 at 08:57