using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Animation; namespace WpfApp15 { public partial class MainWindow : Window { public class CustomerEntity { public int ID { get; set; } public string CustomerName { get; set; } public string CustomerTel { get; set; } public List GetCustomer() { // Simulate long running processing. Thread.Sleep(5000); var model = new List { new CustomerEntity {ID = 1,CustomerName = "AAAA", CustomerTel = "09125484848"}, new CustomerEntity {ID = 2,CustomerName = "BBBBB", CustomerTel = "09125486708"}, }; return model; } } public MainWindow() { InitializeComponent(); } private Storyboard GetStoryboard() { return TryFindResource("RotationBtn") as Storyboard; } private void StartAnimation(Storyboard storyboard) { Storyboard.SetTarget(storyboard, Command4); storyboard.Begin(); } private void StopAnimation(Storyboard storyboard) { storyboard.Stop(); } private async void Command4_Click(object sender, RoutedEventArgs e) { // Get text from the TextBox here in the main thread. var myText = MyTextBox.Text; // Check text here, because there is no sense to start animation and "DoMyCodes" when it's not valid. // It also makes more sense to show MessageBox here. if (string.IsNullOrEmpty(myText)) { MessageBox.Show("please enter valid parameter"); } else { // Get storyboard and start animation. var storyboard = GetStoryboard(); StartAnimation(storyboard); // Get customers. var customers = await DoMyCodesAsync(myText); // TODO: Process result here. // Stop animation. StopAnimation(storyboard); } } private async Task> DoMyCodesAsync(string myText) { // 1) More correct approach: // // "GetCustomer()" method should be async, and it should also use async methods to get customers from the DB, network, or other source. // CustomerEntity C = new CustomerEntity(); // var AllData = await C.GetCustomerAsync(); // var result = AllData.Select(e => e.CustomerName.Contains("xxxx")).Select(s => s).First(); // var CompantNames = AllData.Where(s => s.ID > 1) // .Select(s => s) // .Where(st => st.CustomerName.Contains("xxxx")); // return CompantNames; // // 2) Another approach: // // If from whatever reason you can't make "GetCustomers" async, then you can create a new task here and invoke it as-is. return await Task.Run(() => { CustomerEntity C = new CustomerEntity(); var AllData = C.GetCustomer(); var CompantNames = AllData.Where(s => s.ID > 1) .Select(s => s) .Where(st => st.CustomerName.Contains(myText)); return CompantNames; }); } } }