There is a Task, it is necessary that at the time of its execution the button on the form is locked.
Task task = Task.Factory.StartNew(() => { // Тут идёт коннект к БД }); public async void Start() { button.IsEnabled = false; await Task.Run(() => { //коннект к базе }); button.IsEnabled = true; } For .NET 4.0, this option should work:
public void Start() { button.IsEnabled = false; Task task = ConnectToDb(); task.ContinueWith(task1 => { button.IsEnabled = true; }, TaskScheduler.FromCurrentSynchronizationContext()); } private Task ConnectToDb() { return Task.Factory.StartNew(() => { //подключение к базе }); } task.Wait block the UI. Need task.ContinueWith on UI-scheduler. - VladDSource: https://ru.stackoverflow.com/questions/535078/
All Articles