MCSD 시험의 일부인 비동기 호출에 대해 자세히 배우려고합니다. 다음 페이지의 모든 예제를 성공적으로 따라갔습니다 : http://msdn.microsoft.com/en-gb/library/2e08f6yc.aspx.비동기 호출이 완료 될 때 콜백 메서드 실행
모든 예제의 콘솔 응용 프로그램과 Winform 응용 프로그램을 만들었습니다. 그러나 콜백 함수는 WinForm 응용 프로그램이 사용되는 경우 마지막 예제 (비동기 호출이 완료되면 콜백 메서드 실행)에서 호출되지 않습니다. 아래 코드를 참조하십시오 :
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
왜 WinForm 응용 프로그램에서 CallbackMethod에 도달하지 않았습니까? 콘솔 응용 프로그램과 WinForm 응용 프로그램의 차이점을 이해합니다.
WinForms 프로젝트가'Main()'메소드에서 시작하도록 설정 했습니까? 비동기 함수 자체가 호출 되었습니까? –
@Nico Schertler, 네. 나는 '응용 프로그램 프레임 워크 사용'을 해제하고 기본 방법에 도달했습니다. – w0051977