执行一个回调方法当异步调用完成 [英] Executing a Callback Method When an Asynchronous Call Completes

查看:84
本文介绍了执行一个回调方法当异步调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更多地了解异步调用,这是MCSD考试的一部分。我按照所有的例子以下页面上成功:的http:// MSDN。 microsoft.com/en-gb/library/2e08f6yc.aspx

I am trying to learn more about Asynchronous calls, which are part of the MCSD exam. I have followed all of the examples on the following page successfully: http://msdn.microsoft.com/en-gb/library/2e08f6yc.aspx.

我对所有的示例中创建控制台应用程序和WinForm的应用程序。但是,回调函数永远不会在最后一个例子叫做(执行一个回调方法,当一个异步调用完成),如果一个WinForm应用程序中使用。请参阅下面的code:

I have created console applications and Winform applications for all of the examples. However, the callback function is never called in the last example (Executing a Callback Method When an Asynchronous Call Completes) if a WinForm application is used. Please see the code below:

Imports System
Imports System.Threading
Imports System.Runtime.InteropServices

Public Class AsyncDemo
    ' The method to be executed asynchronously.
    '
    Public Function TestMethod(ByVal callDuration As Integer, _
            <Out()> ByRef threadId As Integer) As String
        Console.WriteLine("Test method begins.")
        Thread.Sleep(callDuration)
        threadId = AppDomain.GetCurrentThreadId()
        Return "MyCallTime was " + callDuration.ToString()
    End Function
End Class

' The delegate must have the same signature as the method
' you want to call asynchronously.
Public Delegate Function AsyncDelegate(ByVal callDuration As Integer, _
    <Out()> ByRef threadId As Integer) As String

Public Class AsyncMain
    ' The asynchronous method puts the thread id here.
    Private Shared threadId As Integer

    Shared Sub Main()
        ' Create an instance of the test class.
        Dim ad As New AsyncDemo()

        ' Create the delegate.
        Dim dlgt As New AsyncDelegate(AddressOf ad.TestMethod)

        ' Initiate the asynchronous call.
        Dim ar As IAsyncResult = dlgt.BeginInvoke(3000, _
            threadId, _
            AddressOf CallbackMethod, _
            dlgt)

        Console.WriteLine("Press Enter to close application.")
        Console.ReadLine()
    End Sub

    ' Callback method must have the same signature as the
    ' AsyncCallback delegate.
    Shared Sub CallbackMethod(ByVal ar As IAsyncResult)
        ' Retrieve the delegate.
        Dim dlgt As AsyncDelegate = CType(ar.AsyncState, AsyncDelegate)

        ' Call EndInvoke to retrieve the results.
        Dim ret As String = dlgt.EndInvoke(threadId, ar)

        Console.WriteLine("The call executed on thread {0}, with return value ""{1}"".", threadId, ret)
    End Sub
End Class

为什么CallbackMethod从未在一个WinForm应用程序达到了?请注意,我明白了一个控制台应用程序和WinForm应用程序之间的区别。

Why is CallbackMethod never reached in a WinForm application? Please note that I understand the difference between a Console application and a WinForm application.

推荐答案

的问题是到Console.ReadLine()。在WinForms应用程序这个调用不会阻塞。相反,可以使用 Thread.sleep代码(Timeout.Infinite)或任何适合您的需求最好的。

The problem is the Console.ReadLine(). In a WinForms app this call does not block. Instead you can use Thread.Sleep(Timeout.Infinite) or whatever suits your needs best.

这篇关于执行一个回调方法当异步调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆