none
Tasks and async programming? RRS feed

  • General discussion

  • Anyone done any task and async programming?

    I tried to get a thread answered in the UWP forums but thought someone did something similar in win forms since you guys do class libraries a lot more in vb.net over here. Its just as simple as getting the task to run on a background thread (not background worker) and complete and when run on UI side it runs fine. I decided to separate my logic into a DLL or class library but this is crazy. Maybe my code is sane and the visual studio  installation got corrupted because I cannot read values when I hit a breakpoint now versus before. Here's the forum post:

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/ead9c1e7-8fcf-448a-9c59-2c6b529dea3c/uwpvbnet-how-to-run-a-method-from-an-inprocess-background-thread?forum=wpdevelop#ead9c1e7-8fcf-448a-9c59-2c6b529dea3c


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    • Edited by The Thinker Saturday, March 17, 2018 9:49 PM
    Saturday, March 17, 2018 9:48 PM

All replies

  • Hi

    Here is an extract from a project that may help.

    ' part of a Button Click event
     ListBox1.DataSource = Await GetFiles
    
      ' -----------------------------------
      Async Function GetFiles(searchstring As String) As Task(Of List(Of String))
    	' findinfiles wildcards filter
    	dic.Clear()
    	Dim lst As New List(Of String)
    	Dim files As ObjectModel.ReadOnlyCollection(Of String) = Nothing
    	Label5.Text = "Working .........."
    	Label5.BackColor = Color.LightGreen
    	Dim so As SearchOption = SearchOption.SearchTopLevelOnly
    	If RadioButton1.Checked Then
    	  so = SearchOption.SearchAllSubDirectories
    	End If
    
    	Dim wildcards() As String = GetWildCards()
    
    	Await Task.Run(
    	Sub()
    	  files = FileSystem.FindInFiles(My.Settings.SearchFolder, searchstring, True, so, wildcards)
    	End Sub)
    
    	Await Task.Run(
    	Sub()
    	  getFolderFileCount(My.Settings.SearchFolder, so)
    	End Sub)
    
    	Dim len As Integer = My.Settings.SearchFolder.Length
    
    	For Each s As String In files
    	  Dim pass As Boolean = True
    	  For Each sss As String In My.Settings.Exclud
    		If s.ToLower.Contains(sss.ToLower) Then pass = False
    	  Next
    	  If pass Then
    		Dim ss As String = s
    		If s.StartsWith(My.Settings.SearchFolder) Then
    		  ss = "...." & s.Substring(len, ss.Length - len)
    		  dic.Add(ss, s)
    		End If
    		lst.Add(ss)
    	  End If
    	Next
    	If lst.Count < 1 Then
    	  lst.Add("Nothing found")
    	  dic.Add("Nothing found", "Nothing found")
    	End If
    	Return lst
      End Function


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, March 17, 2018 10:01 PM
    Saturday, March 17, 2018 10:00 PM
  • You should look at implementing Task.ConfigureAwait.

    I don't have an example to match up with your task nor in VB.NET but if you want to see usage in C# I can provide this accessing SQL-Server database which is meant (if you wanted to see it) to be a conceptual example.

    Also have used this with WebAPI/ASP.NET solutions too.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 18, 2018 12:51 AM
    Moderator
  • You should look at implementing Task.ConfigureAwait.

    I don't have an example to match up with your task nor in VB.NET but if you want to see usage in C# I can provide this accessing SQL-Server database which is meant (if you wanted to see it) to be a conceptual example.

    Also have used this with WebAPI/ASP.NET solutions too.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    if you've used webclient to download files all I am doing is using that and doing some mathematical calculations in GrabFromOnlineSource and its the only function not completing. It never returns to the main function either. The biggest problem is telling the task to wait for completion. But even though it's UWP its not much difference in implementation that's why I asked here too. I am reinstalling Visual studio 2017 to make sure nothing got messed up because this seems too weird not to work at all.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us



    Sunday, March 18, 2018 1:10 AM
  • Well WebClient can perform asynchronously so unless the mathematical calculations are extremely time consuming then perhaps just use WebClient asynchronously.

    La vida loca

    Sunday, March 18, 2018 8:58 AM
  • Well WebClient can perform asynchronously so unless the mathematical calculations are extremely time consuming then perhaps just use WebClient asynchronously.

    La vida loca


    I do but my function still does not output anything to a log file when GrabFromOnlineSource is executed. I can give it another try but I might be doing something wrong. That's why I asked everyone here because it's really simple operations and it baffles me that it does not even execute anything in the function. 

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Sunday, March 18, 2018 12:33 PM
  • Well WebClient can perform asynchronously so unless the mathematical calculations are extremely time consuming then perhaps just use WebClient asynchronously.


    La vida loca


    I do but my function still does not output anything to a log file when GrabFromOnlineSource is executed. I can give it another try but I might be doing something wrong. That's why I asked everyone here because it's really simple operations and it baffles me that it does not even execute anything in the function. 

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    The problem may be UWP specific due to the page events being used, but I did notice in the code posted to your other thread that you do not Await the Task.Run() that you want to wait for.  Add an Await to that line of code.  I'm also not sure why you have so many Task.Delay() calls in there... if they are being used for timing purposes, that is an anti-pattern and should be refactored.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, March 19, 2018 12:09 PM
    Moderator
  • Well WebClient can perform asynchronously so unless the mathematical calculations are extremely time consuming then perhaps just use WebClient asynchronously.


    La vida loca


    I do but my function still does not output anything to a log file when GrabFromOnlineSource is executed. I can give it another try but I might be doing something wrong. That's why I asked everyone here because it's really simple operations and it baffles me that it does not even execute anything in the function. 

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    The problem may be UWP specific due to the page events being used, but I did notice in the code posted to your other thread that you do not Await the Task.Run() that you want to wait for.  Add an Await to that line of code.  I'm also not sure why you have so many Task.Delay() calls in there... if they are being used for timing purposes, that is an anti-pattern and should be refactored.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thanks for your help but the waits need to stay in because I need the user to see progress otherwise my program will finish too fast. In addition, the user may not see the operations progress if i do so (sometimes it  run on a fast internet connection). If the operation is long the progress will slowly move. I ran it from another UI component where I know the code worked as this was ran from a XAML Control and not direct UI. It turns out UWP broke some backward compatible API's and I had to recode anyways. Ouch! X{.

    On Another note: I timed it at creating files dynamically and using webapis at about 30-40 seconds for the whole operation and this may actually be 15-20 seconds depending on my internet connection(about 5-6 pages of items retrieved from ebay too). Edit: Actually that's with my breakpoints so its actually a few seconds without them.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us








    Saturday, March 31, 2018 11:33 PM
  • I just wanted to say thank you to everyone involved. I need to figure out what's going on and why it works in another place just fine but when I use it for loading my custom UI component (XAML based) it crashes and does not finish.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    Monday, April 2, 2018 2:27 PM