2020/02/19(Wed) 19:13:18 編集(投稿者)
'----- vb.netのClass(COMVisibleEvents)-----
Imports System.EnterpriseServices
Imports System.IO
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.Threading.Tasks
Namespace COMVisibleEvents
<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
<Guid("B64928E8-1D07-4FE8-AD5D-E479CD75C6C4")>
Public Interface IEvents
<DispId(1)> Sub OnDownloadCompleted()
End Interface
<ComVisible(True)>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
<Guid("47A87A45-2B51-482F-ADF5-AF818A31F96D")>
Public Interface IDemo
<DispId(2)> Function DownloadFileAsync(address As String, filename As String) As Task
End Interface
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.None)>
<ComDefaultInterface(GetType(IDemo))>
<ComSourceInterfaces(GetType(IEvents))>
<Guid("C58721B1-15B3-4eeb-9E1E-BCDA33D38EE6")>
Public Class DemoEvents : Inherits ServicedComponent : Implements IDemo
Public Delegate Sub OnDownloadCompletedDelegate()
Public Event OnDownloadCompleted As OnDownloadCompletedDelegate
Public Function DownloadFileAsync(address As String, filename As String) As Task Implements IDemo.DownloadFileAsync
Try
Using webClient As WebClient = New System.Net.WebClient()
webClient.Credentials = New NetworkCredential("user", "psw", "domain")
'イベントハンドラの作成
AddHandler webClient.DownloadFileCompleted, AddressOf downloadClient_DownloadFileCompleted
webClient.DownloadFileAsync(New Uri(address), filename)
End Using
Catch ex As Exception
' Log exception here ...
End Try
End Function
Private Sub downloadClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Cancelled Then
MsgBox("キャンセルされました。")
ElseIf Not (e.Error Is Nothing) Then
MsgBox("エラー:" & e.Error.Message)
Else
MsgBox("ダウンロードが完了しました。")
End If
Try
RaiseEvent OnDownloadCompleted()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
End Namespace