Solved

Running Async with 1ms delay

Posted on 2016-08-08
3
61 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 1

Accepted Solution

by:
MCSI Developers earned 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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