Share via

How to temporarily pause processing

RogerSchlueter-7899 1,731 Reputation points
2025-03-16T18:03:34.8266667+00:00

This question is about a mapping application I am writing, specifically how to coordinate processing and mouse actions.

To add a new point to a map, this routine is called:

Private Sub AddPoint() 
	PostAllPoints()   'Show all existing points
	Points.Clear()   'Clear saved points
	<<STOP HERE>>
	Dim win As New GeoElementParameters
	....
End Sub

After existing points are cleared, I want processing to stop so the user can create a new point using the following method:

Private Sub CreatePoint(sender As Object, e As MouseButtonEventArgs) Handles map.MouseLeftButtonUp
	If Keyboard.Modifiers = ModifierKeys.Control Then
		Dim pt As Point = map.ScreenToGeographic(e.GetPosition(map))
		Points.Add(pt)
		PostMark(pt, vl, Brushes.Red)
	End If
End Sub

After the user creates a point I want the processing to continue after the <<STOP HERE>> line in the AddPoint routine.

I've read up on Async methods but can't put theory into practice. What is the best way to accomplish this?

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2025-03-17T02:58:38.1733333+00:00

Hi @RogerSchlueter-7899 ,

You need to pause execution at <<STOP HERE>> in AddPoint() until the user interacts with the map and creates a point using a TaskCompletionSource.

Imports System.Threading.Tasks

Private _pointCompletionSource As TaskCompletionSource(Of Point)

Private Async Sub AddPoint()
    PostAllPoints() ' Show all existing points
    Points.Clear()  ' Clear saved points

    ' Create a new TaskCompletionSource
    _pointCompletionSource = New TaskCompletionSource(Of Point)()

    ' Wait for user to create a point
    Dim newPoint As Point = Await _pointCompletionSource.Task

    ' Continue processing after the user adds a point
    Dim win As New GeoElementParameters()
End Sub

Private Sub CreatePoint(sender As Object, e As MouseButtonEventArgs) Handles map.MouseLeftButtonUp
    If Keyboard.Modifiers = ModifierKeys.Control AndAlso _pointCompletionSource IsNot Nothing Then
        Dim pt As Point = map.ScreenToGeographic(e.GetPosition(map))
        Points.Add(pt)
        PostMark(pt, vl, Brushes.Red)

        ' Complete the TaskCompletionSource with the selected point
        _pointCompletionSource.TrySetResult(pt)
    End If
End Sub

Best Regards.

Jiachen Li


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.