远程处理:服务器没有回答,但仍然可以正常工作

时间:2013-12-25 00:15:39

标签: vb.net remoting

我有一个VB.Net应用程序,我通过Remoting创建了一些对象。

有时(并非总是)创建远程处理对象块并且应用程序冻结而不会抛出任何异常,即使服务器正常。我试图让它运行,但15分钟后,仍然没有发生任何事情。但是,如果我停止应用程序并立即重新运行它,它可以完美地运行。

反正知道这有什么问题吗?或者在超时后重新运行远程对象的创建方法?

以下是我用来创建远程对象的代码:

URL = "tcp://" & ServerIPAddress.ToString & ":" & ServerRemotingPort.ToString & "/TechnicalDatabase"
RemoteObject = CType(Activator.GetObject(GetType(TechnicalDatabase.TechnicalDatabaseObject), URL), TechnicalDatabase.TechnicalDatabaseObject)

这个问题大约每10次发生一次。

1 个答案:

答案 0 :(得分:2)

以下代码将被视为示例/简单修复。它没有解释“为什么”。它在BackgroundWorker中运行代码并使用Timer来超时/中止工作者。

Imports System.Runtime.Remoting
Imports System.ComponentModel
Imports System.Threading.Thread

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
        Me.bgWorker = New BackgroundWorker()
        Me.bgWorker.WorkerSupportsCancellation = True
        Me.bgTimer = New System.Windows.Forms.Timer
        Me.bgTimer.Interval = 60000 '<-- Timeout after 1 minute.
    End Sub

    Private Sub Connect(ipAddress As String, portNumber As Integer, db As String)
        If (Me.bgWorker.IsBusy) Then
            Throw New Exception("Worker is busy.")
        End If
        Dim [error] As Exception = Nothing
        Try
            AddHandler Me.bgTimer.Tick, New EventHandler(AddressOf Me.OnTimeout)
            AddHandler Me.bgWorker.DoWork, New DoWorkEventHandler(AddressOf Me.OnDoWork)
            AddHandler Me.bgWorker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf Me.OnWorkCompleted)
            Me.bgTimer.Enabled = True
            Me.bgWorker.RunWorkerAsync(String.Format("tcp://{0}:{1}/{2}", ipAddress, portNumber, db))
        Catch ex As Exception
            [error] = ex
            Me.bgTimer.Enabled = False
            RemoveHandler Me.bgTimer.Tick, New EventHandler(AddressOf Me.OnTimeout)
            RemoveHandler Me.bgWorker.DoWork, New DoWorkEventHandler(AddressOf Me.OnDoWork)
            RemoveHandler Me.bgWorker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf Me.OnWorkCompleted)
        End Try
        If (Not [error] Is Nothing) Then
            Throw [error]
        End If
    End Sub

    Private Sub OnDoWork(sender As Object, e As DoWorkEventArgs)
        Me.bgThread = Thread.CurrentThread
        Try
            e.Result = CType(Activator.GetObject(GetType(TechnicalDatabase.TechnicalDatabaseObject), DirectCast(e.Argument, String)), TechnicalDatabase.TechnicalDatabaseObject)
        Catch ex As ThreadAbortException
            e.Cancel = True
            Thread.ResetAbort()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub OnWorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
        Me.bgTimer.Enabled = False
        Me.bgThread = Nothing
        RemoveHandler Me.bgTimer.Tick, New EventHandler(AddressOf Me.OnTimeout)
        RemoveHandler Me.bgWorker.DoWork, New DoWorkEventHandler(AddressOf Me.OnDoWork)
        RemoveHandler Me.bgWorker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf Me.OnWorkCompleted)
        If (Not e.Error Is Nothing) Then
            MessageBox.Show(e.Error.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        ElseIf (e.Cancelled) Then
            MessageBox.Show("Worker cancelled.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Dim remoteObject As Object = CType(e.Result, TechnicalDatabase.TechnicalDatabaseObject)
            MessageBox.Show("Success!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub OnTimeout(sender As Object, e As EventArgs)
        Me.bgTimer.Enabled = False
        If (Not Me.bgThread Is Nothing) Then
            Me.bgThread.Abort()
        End If
    End Sub

    Private bgWorker As BackgroundWorker
    Private bgTimer As System.Windows.Forms.Timer
    Private bgThread As Thread

End Class

连接:

Me.Connect(ServerIPAddress.ToString(), ServerRemotingPort, "TechnicalDatabase")

灵感来源: