2011-11-18 48 views
0

我使用的是vb.net 2010,我创建了一个使用套接字在我们的Windows服务器和unix服务器之间传输数据的程序。该代码最初来自微软示例项目,因此我对它的理解很少。VB.Net套接口调用

一切都很好,直到我有了将程序改为服务的想法。 Invoke命令不能从服务访问。我想我明白为什么,但更重要的是我该如何解决或修复它?

' need to call Invoke before can update UI elements 
    Dim args As Object() = {command, data} 
    Invoke(_processInStream, args) 

有人请帮助我不顾一切完成这个程序,所以我可以在:)

下面移动是班上的其他同学,有一个服务器套接字类太多,但我没有想使事情变得复杂?

Public Class srvMain 

' start the InStream code to receive data control.Invoke callback, used to process the socket notification event on the GUI's thread 
Delegate Sub ProcessSocketCommandHandler(ByVal command As NotifyCommandIn, ByVal data As Object) 
Dim _processInStream As ProcessSocketCommandHandler 

' network communication 
Dim WithEvents _serverPRC As New ServerSocket 
Dim _encryptDataIn() As Byte 
Dim myConn As SqlConnection 
Dim _strsql As String = String.Empty 

Protected Overrides Sub OnStart(ByVal args() As String) 
    ' watch for filesystem changes in 'FTP Files' folder 
    Watch() 

    ' hookup Invoke callback 
    _processInStream = New ProcessSocketCommandHandler(AddressOf ProcessSocketCommandIn) 
    ' listen for Ultimate sending signatures 
    _serverPRC.Start(My.Settings.listen_port_prc) 
    myConn = New SqlConnection(My.Settings.Mill_SQL_Connect) 
End Sub 

Protected Overrides Sub OnStop() 
    ' Add code here to perform any tear-down necessary to stop your service. 
End Sub 

' this is where we will break the data down into arrays 
Private Sub processDataIn(ByVal data As Object) 
    Try 
     If data Is Nothing Then 
      Throw New Exception("Stream empty!") 
     End If 

     Dim encdata As String 

     ' decode to string and perform split(multi chars not supported) 
     encdata = Encoding.Default.GetString(data) 

     _strsql = encdata 
     myConn.Open() 
     Dim commPrice As New SqlCommand(_strsql, myConn) 
     Dim resPrice As SqlDataReader = commPrice.ExecuteReader 

     '********************************THIS MUST BE DYNAMIC FOR MORE THAN ONE NATIONAL 
     If resPrice.Read = True And resPrice("ats" & "_price") IsNot DBNull.Value Then 
      'If resPrice("ats" & "_price") Is DBNull.Value Then 
      ' cannot find price so error 
      'natPrice = "" 
      'natAllow = 2 
      'End If 
      natPrice = resPrice("ats" & "_price") 
      natAllow = resPrice("ats" & "_allow") 
     Else 
      ' cannot find price so error 
      natPrice = "" 
      natAllow = 2 
     End If 


     myConn.Close() 
     ' substring not found therefore must be a pricing query 
     'MsgBox("string: " & encdata.ToString) 
     'natPrice = "9.99" 

    Catch ex As Exception 
     ErrHandle("4", "Process Error: " + ex.Message + ex.Data.ToString) 
    Finally 
     myConn.Close() ' dont forget to close! 
    End Try 
End Sub 

'======================== 
'= ServerSocket methods = 
'======================== 
' received a socket notification for receiving from Ultimate 
Private Sub ProcessSocketCommandIn(ByVal command As NotifyCommandIn, ByVal data As Object) 
    ' holds the status message for the command 
    Dim status As String = "" 
    Select Case command 
     Case NotifyCommandIn.Listen 
      'status = String.Format("Listening for server on {0} ...", CStr(data)) 
      status = "Waiting..." 
     Case NotifyCommandIn.Connected 
      'status = "Connected to Ultimate" ' + CStr(data) 
      status = "Receiving..." 
     Case NotifyCommandIn.Disconnected 
      status = "Waiting..." ' disconnected from Ultimate now ready... 
     Case NotifyCommandIn.ReceivedData 
      ' store the encrypted data then process 
      processDataIn(data) 
    End Select 
End Sub 

' called from socket object when a network event occurs. 
Private Sub NotifyCallbackIn(ByVal command As NotifyCommandIn, ByVal data As Object) Handles _serverPRC.Notify 
    ' need to call Invoke before can update UI elements 
    Dim args As Object() = {command, data} 
    Invoke(_processInStream, args) 
End Sub 

末级

任何帮助表示赞赏 非常感谢

+0

为什么你需要运行它作为服务?您是否想过使用Task Scheduler重复启动您的应用程序? –

+0

我没有看到您提供的课程中提到的代码?我不明白它的用途是什么? – jgauffin

+0

嗨,我想要这项服务,因为我希望它在没有任何用户登录的情况下运行,并且我希望它能够以Windows开始。 “提到的”代码位于sub notifyCallbackin下的代码部分的底部。谢谢 –

回答

1

InvokeSystem.Windows.Forms.Form成员,它是用来确保一定的方法是在UI线程调用。如果所涉及的方法涉及UI控件,则这是必要的。

在这种情况下,它看起来像你根本无法直接调用该方法,即

,而不是

Dim args As Object() = {command, data} 
Invoke(_processInStream, args) 

可以简单的写

ProcessSocketCommandIn(command, data) 

而且,在这种情况下,你可以摆脱_processInStream委托实例。

+1

非常感谢你这么快速和详细的答复 - 看着它,它应该有明显的,但这不是我已探索的VB领域。你的回答很有效,你让我的星期五!再次很多很多谢谢 –