繁体   English   中英

vb.net实现IAsyncResult.AsyncState

[英]vb.net implementing IAsyncResult.AsyncState

我可以轻松地在C#中执行此操作...但是我需要VB.Net中的等效功能。 我需要能够在VB.Net中实现各种IAsyncResult属性。

在C#中

像冠军一样工作...

public object AsyncState { get; set; }

在VB.NET中-此失败

这将失败,因为您不能在VB.Net中重载属性

Public ReadOnly Property AsyncState() As Object Implements IAsyncResult.AsyncState
    Get
        '... the GET's code goes here ...
    End Get
End Property
Public WriteOnly Property AsyncState() As Object
    Set(ByVal value As Object)
        '... the SET's code goes here ...
    End Set
End Property

在VB.NET中-两者均失败

这无法满足“必须实现IAsyncResult”的要求

Public AsyncState As Object
Public AsyncState As Object Implements IAsyncResult.AsyncState

这成功了...

    Public Class AsyncResult
        Implements IAsyncResult

#Region "CONSTRUCTORS"

        Public Sub New(ByVal callback As AsyncCallback, ByVal context As HttpContext)
            _asyncCallback = callback
            _httpContext = context
            _createdTime = DateTime.Now
        End Sub

#End Region

#Region "PROPERTIES"

        Public Const TimeoutSeconds As Integer = 3

        Private _asyncCallback As AsyncCallback
        Private _httpContext As HttpContext
        Private _createdTime As DateTime

        Public ReadOnly Property TimedOut() As Boolean
            Get
                Return ((DateTime.Now - _createdTime).TotalSeconds >= TimeoutSeconds)
            End Get
        End Property
        Public Property Response() As Response
            Get
                Return m_Response
            End Get
            Set(ByVal value As Response)
                m_Response = value
            End Set
        End Property
        Private m_Response As Response

#Region "IAsyncResult Members"

        Public ReadOnly Property HttpContext() As HttpContext
            Get
                Return _httpContext
            End Get
        End Property
        Public ReadOnly Property AsyncState() As Object Implements IAsyncResult.AsyncState
            Get
                Return m_AsyncState
            End Get
            'Set(ByVal value As Object)
            '    m_AsyncState = value
            'End Set
        End Property
        Private m_AsyncState As Object

        Private ReadOnly Property IAsyncResult_AsyncWaitHandle() As System.Threading.WaitHandle Implements IAsyncResult.AsyncWaitHandle
            Get
                Throw New NotImplementedException()
            End Get
        End Property

        Private ReadOnly Property IAsyncResult_CompletedSynchronously() As Boolean Implements IAsyncResult.CompletedSynchronously
            Get
                Return False
            End Get
        End Property

        Public ReadOnly Property IsCompleted() As Boolean Implements IAsyncResult.IsCompleted
            Get
                Return m_isCompleted
            End Get
            'Set(ByVal value As Boolean)
            '    If Not value Then
            '        Return
            '    End If

            '    Me.m_isCompleted = True
            '    _asyncCallback(Me)
            'End Set
        End Property
        Private m_isCompleted As Boolean = False

#End Region

#End Region

#Region "METHODS"

        Public Function ProcessRequest() As Boolean

            ' Any "Execution" code goes here...

            Return Me.IsCompleted
        End Function
#End Region

    End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM