vb.net 实现 IAsyncResult.AsyncState

发布于 2024-09-17 21:29:29 字数 797 浏览 11 评论 0原文

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

I can easily do this in C#...but I need the equivalent in VB.Net. I need to be able to implement various IAsyncResult properties in VB.Net.

IN C#

Works like a champ...

public object AsyncState { get; set; }

IN VB.NET - THIS FAILS

This fails because you cannot overload a property in 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

IN VB.NET - BOTH THESE FAIL

This fails the "must implement IAsyncResult" requirement

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-09-24 21:29:29

这成功了......

    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

This did the trick...

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文