How do you turn the button on the form during the Task'a?



  • There is a Task Force, it is necessary that the form button be rolled out at the time of its implementation.

    Task task = Task.Factory.StartNew(() =>
    {
         // Тут идёт коннект к БД
    });
    


  •         public async void Start()
            {
                button.IsEnabled = false;
    
                await Task.Run(() =>
                {
                    //коннект к базе
                });
    
                button.IsEnabled = true;
            }
    

    For .NET 4.0, the option should be:

        public void Start()
    
    {
         button.IsEnabled = false;
         Task task = ConnectToDb();
    
         task.ContinueWith(task1 =>
         {
             button.IsEnabled = true;
    
         }, TaskScheduler.FromCurrentSynchronizationContext());
    
    }
    
    private Task ConnectToDb()
    {
        return Task.Factory.StartNew(() =>
        {
            //подключение к базе
        });
    }
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2