none
[VBNET] System.InvalidOperationException 未處理? RRS feed

  • 問題

  • Imports System.Text
    Public Class Form1
      WithEvents Winsock1 As New JSWinsock.tcpWinsock
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = Winsock1.localHost.ToString
        Winsock1.localPort = CInt(TextBox2.Text)
        Winsock1.Listen()
    
      End Sub
      Private Sub Winsock1_ConnectionRequest_Event(ByRef requestID As System.Net.Sockets.Socket) Handles Winsock1.ConnectionRequest_Event
        If Winsock1.State = JSWinsock.tcpWinsock.StateConstants.sckListening Then
          Winsock1.Accept(requestID)
          RichTextBox2.Text &= (TimeString + ":" + "連接成功" & vbNewLine)
    
        End If
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Winsock1.SendData(TextBox1.Text)
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Winsock1.localPort = CInt(TextBox2.Text)
        Winsock1.Listen()
      End Sub
    
      Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
      End Sub
    
      Private Sub Winsock1_DataArrival_Event(ByVal bytesTotal As Integer) Handles Winsock1.DataArrival_Event
        Dim strData(bytesTotal) As Byte
    
        Winsock1.GetData(strData)
        Dim txt = Encoding.Unicode.GetString(strData)
      ListBox1.Items.Add(txt.tostring)
      End Sub
      
    End Class
    
    
    System.InvalidOperationException 未處理
      Message=跨執行緒作業無效: 存取控制項 'ListBox1' 時所使用的執行緒與建立控制項的執行緒不同。
      Source=System.Windows.Forms
      StackTrace:
           於 System.Windows.Forms.Control.get_Handle()
           於 System.Windows.Forms.ListBox.NativeAdd(Object item)
           於 System.Windows.Forms.ListBox.ObjectCollection.AddInternal(Object item)
           於 System.Windows.Forms.ListBox.ObjectCollection.Add(Object item)
           於 WindowsApplication1.Form1.Winsock1_DataArrival_Event(Int32 bytesTotal) 於 D:\Users\Janus\Documents\Visual Studio 2010\Projects\socket2\socket2\Form1.vb: 行 40
           於 JSWinsock.tcpWinsock.x16408cace4347a6f()
           於 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           於 System.Threading.ExecutionContext.runTryCode(Object userData)
           於 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           於 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           於 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           於 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           於 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    2010年9月29日 下午 04:13

解答

所有回覆