【问题标题】:vb.net implementing IAsyncResult.AsyncStatevb.net 实现 IAsyncResult.AsyncState
【发布时间】:2010-09-07 17:43:40
【问题描述】:

我可以在 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

【问题讨论】:

    标签: c# vb.net translation implementation iasyncresult


    【解决方案1】:

    成功了...

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-10
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多