Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Running Async with 1ms delay

Posted on 2016-08-08
3
Medium Priority
?
98 Views
Last Modified: 2016-08-09
Hi,
I would like to know why the current wait for 1 millisecond is not working. to me it looks more like a 50 millisecond wait.
what can I do to this this glith?

Public Class Form1
    Dim iCount As Long = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim aaa = DoSomethingAndWaitAsync()
    End Sub

    Private Sub trmLabel_Tick(sender As Object, e As EventArgs) Handles trmLabel.Tick
        Dim iDate As DateTime = Date.UtcNow
        Label2.Text = iDate.ToString

        If iDate.Second = 59 Then
            iCount = 0
        Else
            If iCount > 0 Then 
                Label1.Text = (iCount / iDate.Second).ToString & " | " & iCount.ToString
            Else
                Label1.Text = iCount.ToString
            End If
        End If
    End Sub

    Public Async Function DoSomethingAndWaitAsync() As Task
        Dim i = True

        While (i = True)
            Await Task.Run(Function() DoSomething())
            Await Task.Delay(1)
        End While

    End Function

    Function DoSomething() As Long
        iCount += 1
        Return iCount
    End Function
End Class

Open in new window

0
Comment
Question by:XK8ER
3 Comments
 
LVL 2

Expert Comment

by:JesNoFear
ID: 41748130
why are you pulling seconds?
If iDate.Second = 59 Then
            iCount = 0
        Else
            If iCount <> iDate.Second Then
                Label1.Text = (iCount / iDate.Second).ToString & " | " & iCount.ToString
            Else
                Label1.Text = iCount.ToString
            End If
        End If
0
 
LVL 1

Author Comment

by:XK8ER
ID: 41748134
JesNoFear, the issue is not with the timer so ignore that part of the code
0
 
LVL 2

Accepted Solution

by:
Mlungisi Ndlela earned 2000 total points
ID: 41748325
Dim i = True

        While (i = True)
            Await Task.Run(Function() DoSomething())
            Await Task.Delay(1)
        End While

Open in new window


Well I've noticed that your I will never ever be false as you can see above. Also I don't think it will wait because you start by running it then call delay at last. try swooping your code and be like this
 Await Task.Delay(1)
Await Task.Run(Function() DoSomehing())

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question