Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16063

What's teh use of canceling an async token if the function has already run?

$
0
0
Hi.
I'm trying to cancel an async function before it starts.Well actually it's a loop.
The code is very long so I'm writing the major parts here.

So I have a cancellation token and I set it to true - to cancel.
The loop is like:
Code:

'url is a list of addresses
    Dim downloadTasksQuery As IEnumerable(Of Task(Of String)) =
            From url In urlList Select GetIPv4AddressAsync(url, cts.Token)

' goes to function GetIPv4AddressAsync

Async Function (..blah.blah, ct As CancellationToken)



        Try
if ct.IsCancellationRequested = true then
exit function
else
            iphe = Await System.Net.Dns.GetHostEntryAsync(strHostName)
end if
        Catch ex As Exception
if ct.IsCancellationRequested = true then
exit function
else....

end function

Now The try will never work as it seems that the GetHostEntryAsync has already been initiate for every single call in the url list
I can only use it in the exception that , as i see, runs after all the GetHostEntryAsync calls have been initiated.

So long story short, how can I have a cancellation caught before the GetHostEntryAsync ? Is it possible?
Imagine if I have 1000 addresses that I want to iterate and it will only cancel the threads after every one has been tests with GetHostEntryAsync. that would be useless.
I can put a timer but that beats the whole cancellation purpose.

Thanks.

Viewing all articles
Browse latest Browse all 16063

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>