Santhosh

Santhosh

  • NA
  • 300
  • 136.5k

Threads Dispatchers Static Code

Sep 27 2019 3:43 AM
  1. public partial class TestWindow : Window  
  2.     {  
  3.         public ICommand ExecuteAction;  
  4.         public MainWindow()  
  5.         {  
  6.             InitializeComponent();  
  7.         }  
  8.   
  9.         Thread th;  
  10.         Thread thMain;  
  11.         private void BtnTest_Click(object sender, RoutedEventArgs e)  
  12.         {  
  13.             th = new Thread(UpdateValue);  
  14.             th.Start();  
  15.         }  
  16.   
  17.         private void BtnSearch_Click(object sender, RoutedEventArgs e)  
  18.         {  
  19.             string sourceDir = string.Empty;  
  20.             string searchText = string.Empty;  
  21.             wpMain.Children.Clear();  
  22.             sourceDir = txtInputDir.Text.Trim();  
  23.             searchText = txtInput.Text.Trim();  
  24.             thMain = new Thread(() => PerformAction(sourceDir, searchText));  
  25.             thMain.SetApartmentState(ApartmentState.MTA);  
  26.             thMain.Start();  
  27.         }  
  28.         private void PerformAction(string sourceDir, string searchText)  
  29.         {  
  30.             try  
  31.             {  
  32.                 //sourceDir = @"C:\Users\santhosh.polu\source\repos\TFS_JIRA_SUPPORT_Solution";  
  33.                 if (Directory.Exists(sourceDir))  
  34.                 {  
  35.                     List<string> lstDirsFiles = Directory.GetDirectories(sourceDir, "*", SearchOption.AllDirectories).ToList();  
  36.                     List<string> lstDirsFiles1 = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories).ToList();  
  37.                     lstDirsFiles.AddRange(lstDirsFiles1);  
  38.   
  39.                     int totalCount = lstDirsFiles.Count;  
  40.                     if (totalCount < 1)  
  41.                         return;    

  42.                     if (lstDirsFiles.Count > 0)  
  43.                     {  
  44.                         int i = 0;  
  45.                         while (i < lstDirsFiles.Count)  
  46.                         {    
  47.                             string currentItem = lstDirsFiles[i];  
  48.                             Dispatcher.Invoke(new Action(() =>  
  49.                             {  
  50.                                 lblStatus.Content = currentItem;  
  51.                             }));  
  52.                             if (Directory.Exists(currentItem))  
  53.                             {  
  54.                                 DirectoryInfo dir = new DirectoryInfo(currentItem);  
  55.                                 String dirName = dir.Name.ToString();  
  56.                                 if (dirName.ToLower().Contains(searchText.ToLower()))  
  57.                                 {  
  58.                                     Dispatcher.Invoke(new Action(() =>  
  59.                                     {  
  60.                                         Button btn = new Button();  
  61.                                         btn.Content = dirName;  
  62.                                         btn.Background = Brushes.AliceBlue;  
  63.                                         btn.ToolTip = "Directory : " + dir.FullName;  
  64.                                         btn.Tag = dir.FullName;  
  65.                                         btn.Click += Btn_Click;  
  66.   
  67.                                         wpMain.Children.Add(btn);     
  68.                                     }));  
  69.                                 }  
  70.                             }  
  71.                             else if (File.Exists(currentItem))  
  72.                             {  
  73.                                 FileInfo fi = new FileInfo(currentItem);  
  74.                                 String fileName = fi.Name.ToString();  
  75.                                 if (fileName.ToLower().Contains(searchText.ToLower()))  
  76.                                 {  
  77.                                     Dispatcher.Invoke(new Action(() =>  
  78.                                     {  
  79.                                         Button btn = new Button();  
  80.                                         btn.Content = fileName;  
  81.                                         btn.ToolTip = "File : " + System.IO.Path.GetDirectoryName(fileName);  
  82.                                         btn.Background = Brushes.IndianRed;  
  83.                                         btn.Tag = System.IO.Path.GetDirectoryName(fileName);  
  84.                                         btn.Click += Btn_Click1;  
  85.   
  86.                                         wpMain.Children.Add(btn);    
  87.                                     }));  
  88.                                 }  
  89.                             }  
  90.                             i++;  
  91.                         }  
  92.                     }  
  93.                     else  
  94.                     {  
  95.                         MessageBox.Show("No Directories exist.");  
  96.                     }  
  97.                 }  
  98.                 else  
  99.                 {  
  100.                     MessageBox.Show("Directory Does not exist.");  
  101.                 }  
  102.   
  103.             }  
  104.             catch (Exception ex)  
  105.             {  
  106.                 MessageBox.Show(ex.Message.ToString() + System.Environment.NewLine + ex.StackTrace.ToString());  
  107.             }  
  108.         }  
  109.   
  110.         private void Btn_Click1(object sender, RoutedEventArgs e)  
  111.         {  
  112.             Button btn = sender as Button;  
  113.             Process.Start("explorer.exe", btn.Tag.ToString());  
  114.         }  
  115.   
  116.         private void Btn_Click(object sender, RoutedEventArgs e)  
  117.         {  
  118.             Button btn = sender as Button;  
  119.   
  120.             Process.Start("explorer.exe", btn.Tag.ToString());  
  121.   
  122.         }  
  123.   
  124.         public void UpdateValue()  
  125.         {  
  126.             while (true)  
  127.             {  
  128.                 Application.Current.Dispatcher.Invoke(  
  129.                 DispatcherPriority.Background,  
  130.                 new Action(() =>  
  131.                 {  
  132.                     txtCurrent.Text = DateTime.Now.ToString("DD MM yyyy hh mm ss");  
  133.                 }));  
  134.             }  
  135.         }  
  136.   
  137.         private void BtnStop_Click(object sender, RoutedEventArgs e)  
  138.         {  
  139.             if (thMain!= null)  
  140.             {  
  141.                 thMain.Abort();  
  142.             }  
  143.         }  
  144.   
  145.     }  
Hi,
 
I am trying to write code for a small application, which reads all the directories and performs some operations on the .xml file types on each directory.
 
So 
 
When i Read Directory without using any Threads UI is freezing untill the execution of the method is completed.
 
Lets say, without Thread and Dispatcher, it has taken 1.5minutes of time.
 
after using thread and dispatchers, UI is not freezing and it is taking lot of time to complete the action. 
 
Code snippet is given. 
 
Kindly tell me, how to reduce the time for Execution (Without UI Freeze).
 
Thanks. 
 
 
 

Answers (2)