Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to admit I don't really understand this async calling
Below code seems to "fail for me or better is not behaving as I would expect
I'm calling
WAIT_MOVING()
from my main thread and would like to wait in a loop running in a new task to wait for
IsMoving
to change state
The main idea is for the main thread to stay responsive but wait for the task to finish ...
This does not seem to happen
- The task starts and does it's job but the main thread keeps on going - as is not waiting for the task to finish.
Again the main task is to keep the main thread responsive while we wait for the
IsMoving
flag to be set.
Any ideas why this is not working as expected ?




Friend Async Sub WAIT_MOVING()
       Dim AbortWait As Boolean = Await Task.Run(Function()
                                                     Return WaitFor_Moving()
                                                 End Function)
       If AbortWait Then
           doSomething
       End If
   End Sub

   Friend Function WaitFor_Moving() As Boolean

       While Not IsMoving
           If MC_Alive = False then
               Return True
          endif
       End While

       Return False
   End Function


What I have tried:

Multiple variations of Async calls non seemed to have produced the "expected "result for me
Posted
Comments
Richard Deeming 13-Feb-18 9:36am    
What does "the main thread keeps on going" mean?

The code that calls WAIT_MOVING has no way of waiting for the method to finish. It's the VB.NET equivalent of an async void method, which should be avoided wherever possible[^].

Also, how is the IsMoving property modified? There might be a more efficient way to wait for it to change than testing it in a tight loop.
Georg Kohler 13-Feb-18 13:09pm    
Ismoving is modified by another thread that's handling the moving of the axis.
After commanding the axis there is a short delay before they actually start usually a few ms but if we never start the move the main thread is the only thing that can stop the process (by pushing the stop button on the GUI and canceling out of the wait loop)

We do like to pick up the change of the isMoving flag as fast as possible because the move might be super short (10 ms or so)

Async call seemed the right thing to do (if it worked)

I know it's not working because the next method is called before the wait task finished - even after reading trough hours of online pages I really don't understand how this actually works ..

Any ideas on how to accomplish this without blocking the main thread 
I'm open to any ideas
Richard Deeming 13-Feb-18 13:15pm    
Assuming you have control over the other thread, you could use a TaskCompletionSource<bool>[^] to create a task that completes when the property becomes True.

When you say "the next method", do you mean the doSomething from your question, or the method following WAIT_MOVING?
Georg Kohler 13-Feb-18 14:05pm    
Another Method following the Wait_move

Problem is timing as the wait might be very short (5ms is what i see now)
I could use a simple loop with a delay in the main thread but that makes it unresponsive with short delays (using sleep)
I was hoping for a cleaner solution :-)

"doSomething" is just for me to cancel the move if something goes wrong
Richard Deeming 13-Feb-18 14:09pm    
As I said, WAIT_MOVING doesn't return anything, so there's no way for the calling code to know when it's finished.

If you wanted to wait for it to finish, you'd need to change it to be a Function that returned a Task:
Friend Async Function WAIT_MOVING() As Task
    Dim AbortWait As Boolean = Await Task.Run(Function()
        Return WaitFor_Moving()
    End Function)
    
    If AbortWait Then
       doSomething
    End If
End Function
The calling code would then use Await to wait for the call to complete before continuing:
Await WAIT_MOVING()
doSomethingElse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900