Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Why my Searcher App crash and how to handle it ?

$
0
0

I wrote a File-Searcher Windows Application (with .NET 4.5) and use Async technique to search faster without locking the GUI.

It tracks it's directory to find files with the given name and extension.

I used Linq Query instead of iterating on files using foreach. (Is it better than foreach ? )

It works great! but it just has a problem. It crashes rapidly!

As I write more than 2 character in the text box, it STOPS WORKING . . .

Do you have any idea that what cause it and how shall I handle it ?

This is my app :

Dictionary<string, string> DS = new Dictionary<string, string>(); // public Form1() { InitializeComponent(); } // private void Form1_Load(object sender, EventArgs e) { } private void Txt_Text_TextChanged(object sender, EventArgs e) { try { if (!String.IsNullOrWhiteSpace(this.Txt_Text.Text)) { Action a = new Action(Search_Name); a.BeginInvoke(AsyncCore, a); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Txt_Ext_TextChanged(object sender, EventArgs e) { try { if (!String.IsNullOrWhiteSpace(this.Txt_Ext.Text)) { Action a = new Action(Search_Ext); a.BeginInvoke(AsyncCore, a); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

/// <summary>
        /// To open the file that listed. (On user's click)
        /// </summary>

private void ListBox_SelectedIndexChanged(object sender, EventArgs e) { try { string FPath = (from x in DS where x.Key == this.ListBox.SelectedItem.ToString() select x.Value).Single(); Process.Start(FPath); } catch (Exception ex) { MessageBox.Show(ex.Message); } } // void Search_Name() { try { DirectoryInfo DI = new DirectoryInfo(Environment.CurrentDirectory); var Q = from FileInfo x in DI.GetFiles() where x.Name.Contains(this.Txt_Ext.Text) select new { FName = x.Name , FPath = x.FullName }; foreach (var item in Q) { DS.Add(item.FName, item.FPath); } } catch (Exception ex) { throw ex; } } void Search_Ext() { try { DirectoryInfo DI = new DirectoryInfo(Environment.CurrentDirectory); var Q = from FileInfo x in DI.GetFiles() where x.Extension == "." + this.Txt_Ext.Text select new { FName = x.Name, FPath = x.FullName }; foreach (var item in Q) { DS.Add(item.FName, item.FPath); } } catch (Exception ex) { throw ex; } } void FillListBox() { try { this.ListBox.Items.Clear(); foreach (KeyValuePair<string, string> item in DS) { this.ListBox.Items.Add(item.Key); } } catch (Exception ex) { throw ex; } } void AsyncCore(IAsyncResult ar) { try { Action a = (Action)ar.AsyncState; a.EndInvoke(ar); this.Invoke((MethodInvoker)delegate { FillListBox(); }); } catch (Exception ex) { throw ex; } }



Hamed Shams The consultant and executer of IT projects. Windows and Web programmer based on Microsoft technology.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>