0

Hey guys I am having trouble understanting await. What I really need to do is have a for loop which searches between 10,000 values in an array show me which values contain certain letters.

Right now without Tasks this process works but lags the app around 4 seconds per request which is why I believe I need to do this using Tasks and Await.

The task doesnt need to return anything. All I really need it to tell it to wait until the Task has ended without causing me to lag. It lags when using the searchbar and typing in letters, every letter causes the app to lag 5 seconds. What I want is for the input for the searchbar to not lag at all and just wait for the Task to finish.

This is the function I am trying to work with.

         async Task searchLoop()
        {
            string word = GetDataValue(items[x], "Name:");
            searchWord = searchWord.ToLower();


            if (word.Contains(searchWord))
            {
                myButton[counter].Text = GetDataValue(items[x], "Name:");
                myButton[counter].StyleId = x.ToString();
                counter++;
            }
        }

I tried a similar thing using Webclient and DownloadStringTaskAsync and it works exactly like I want it to, but I want to do it locally and not need a Webclient to feed me the information I need.

Carlos Castillo
  • 151
  • 2
  • 2
  • 8
  • The `searchLoop` method, if it has no `await`, the method is a sync/blocking method. What happens if you add `await Task.Yield()` at the beginning of the method? Or use `Task.Run` wrap its content? – weichch Mar 16 '20 at 03:41
  • If you want your `searchLoop()` method to operate asynchronously, it has to have some asynchronous behavior inside it. Adding `async` doesn't change the method; it just changes the context, to allow the use of `await` and to change the semantics of the `return` statement in the method. See marked duplicate for details about how to move code to a background task via `Task.Run()`, allowing a method to work asynchronously. – Peter Duniho Mar 16 '20 at 03:50
  • Why are you using `word.Contains(searchWord)` and not `word == searchWord`? – ProgrammingLlama Mar 16 '20 at 04:24
  • What should happen is case the user types several letters? Each new letter should initiate a new asynchronous operation, without cancelling the previous ones? – Theodor Zoulias Mar 16 '20 at 07:31
  • The thing is everytime I add a letter it should be popping up the names that show up with that letter. But some tablets are so slow that this process doesnt finish before pressing the next letter so I am hoping on finding a way to make this Task work without the tablet crashing or freezing until it finds all the names. @John The reason its looking for contains is becasue I am searching letter by letter say Pressin A to find Allison. – Carlos Castillo Apr 06 '20 at 02:22
  • @Carlos I'd suggest naming your variable `searchTerm` in that case since `searchWord` (to me) gives the impression that it contains a complete word to match against. – ProgrammingLlama Apr 06 '20 at 02:26

0 Answers0