From 6f6b7cfcd2885852ca531e28c042810d1e3119be Mon Sep 17 00:00:00 2001 From: AsvalGTA Date: Wed, 24 Apr 2019 23:01:13 +0200 Subject: [PATCH] fixed huge lag while searching for an asset by delaying the textchanged event, you have 600 milliseconds before it checks your text and search for the asset --- FModel/Custom/TypeAssistant.cs | 29 +++++++++++++++++++++++++++++ FModel/FModel.csproj | 1 + FModel/Forms/SearchFiles.cs | 18 ++++++++++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 FModel/Custom/TypeAssistant.cs diff --git a/FModel/Custom/TypeAssistant.cs b/FModel/Custom/TypeAssistant.cs new file mode 100644 index 00000000..7c597465 --- /dev/null +++ b/FModel/Custom/TypeAssistant.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace FModel.Custom +{ + public class TypeAssistant + { + public event EventHandler Idled = delegate { }; + public int WaitingMilliSeconds { get; set; } + System.Threading.Timer waitingTimer; + + public TypeAssistant(int waitingMilliSeconds = 600) + { + WaitingMilliSeconds = waitingMilliSeconds; + waitingTimer = new Timer(p => + { + Idled(this, EventArgs.Empty); + }); + } + public void TextChanged() + { + waitingTimer.Change(WaitingMilliSeconds, System.Threading.Timeout.Infinite); + } + } +} diff --git a/FModel/FModel.csproj b/FModel/FModel.csproj index b2a12015..407ca3ec 100644 --- a/FModel/FModel.csproj +++ b/FModel/FModel.csproj @@ -63,6 +63,7 @@ + Form diff --git a/FModel/Forms/SearchFiles.cs b/FModel/Forms/SearchFiles.cs index 6a977b97..3518b1ef 100644 --- a/FModel/Forms/SearchFiles.cs +++ b/FModel/Forms/SearchFiles.cs @@ -1,4 +1,5 @@ -using System; +using FModel.Custom; +using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -14,6 +15,7 @@ namespace FModel.Forms { public partial class SearchFiles : Form { + TypeAssistant assistant; List myInfos = new List(); List myFilteredInfos; private static string fileName; @@ -25,6 +27,9 @@ namespace FModel.Forms public SearchFiles() { InitializeComponent(); + + assistant = new TypeAssistant(); + assistant.Idled += assistant_Idled; } private async void SearchFiles_Load(object sender, EventArgs e) @@ -255,12 +260,17 @@ namespace FModel.Forms listView1.EndUpdate(); } - private async void textBox1_TextChanged(object sender, EventArgs e) + void assistant_Idled(object sender, EventArgs e) { - await Task.Run(() => + this.Invoke( + new MethodInvoker(() => { filterListView(); - }); + })); + } + private void textBox1_TextChanged(object sender, EventArgs e) + { + assistant.TextChanged(); } private void button1_Click(object sender, EventArgs e)