none
关于form的invoke的问题 RRS feed

  • 问题

  • 调用invok函数,有个很奇怪的问题:

    var data = BuildDefaultCmdStruct();
    var au = new AutoResetEvent(false);

    data.data = ModbusFunction.BuildCmd03(txtID.ToByte(1), 4200, 12);

    bool ia = false;
    byte[] ba = null;

    data.AfterReceiveData += (s, e1) =>
                                       {
                                          ba = (byte[]) e1.DataReceive;
                                          if (ba.Length <= 0)
                                          {
                                               ia = false;
                                               return;
                                          }

                                          this.Invoke(new MethodInvoker(()=>
                                                                 {
                                                                    this.Text = "dddd";
                                                                      
                                                                  }));
                                              
                                          ia = true;
                                            
                                          au.Set();
                                              
                                       };


    trans.Send(data);

    au.WaitOne();

    if (ia)
    {}

    那个lamba表达式是在串口 serialport 的 接收到数据的事件中回调的, 最大问题是在调用到 this.Invoke 的时候,程序就停了不动了????有没有人知道这是为什么啊?

    2009年11月5日 2:58

答案

  • 参考下我在VB.NET中调用Invoke的例子,给出更多的代码让我们了解你的问题:

    Imports System
    Imports System.IO.Ports
    Public Class Form1
        Private received As String = String.Empty
        Dim WithEvents port As SerialPort = New  _
        System.IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
        Public Delegate Sub myDelegate()
        Public Sub updateTextBox()
            With TextBox2
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                received = port.ReadTo("!%")
                .AppendText(received)
            End With
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As  _
        System.EventArgs) Handles Me.Load
            CheckForIllegalCrossThreadCalls = False
            If port.IsOpen = False Then
                port.Open()
            End If
        End Sub
        Private Sub port_DataReceived(ByVal sender As Object, ByVal e As  _
        System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
            TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
            received = String.Empty
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If port.IsOpen = False Then
                port.Open()
            End If
            port.Write(TextBox1.Text & "!%")
            TextBox1.Text = ""
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            If port.IsOpen <> False Then
                port.Close()
            End If
        End Sub
    End Class

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年11月10日 10:13
    版主

全部回复

  • 我把断点打在  this.Text = "dddd"; 这句的时候,根本就没执行进来的?整个大的函数是在form的一个按钮事件中执行的
    2009年11月5日 2:59
  • 参考下我在VB.NET中调用Invoke的例子,给出更多的代码让我们了解你的问题:

    Imports System
    Imports System.IO.Ports
    Public Class Form1
        Private received As String = String.Empty
        Dim WithEvents port As SerialPort = New  _
        System.IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
        Public Delegate Sub myDelegate()
        Public Sub updateTextBox()
            With TextBox2
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                received = port.ReadTo("!%")
                .AppendText(received)
            End With
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As  _
        System.EventArgs) Handles Me.Load
            CheckForIllegalCrossThreadCalls = False
            If port.IsOpen = False Then
                port.Open()
            End If
        End Sub
        Private Sub port_DataReceived(ByVal sender As Object, ByVal e As  _
        System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
            TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
            received = String.Empty
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If port.IsOpen = False Then
                port.Open()
            End If
            port.Write(TextBox1.Text & "!%")
            TextBox1.Text = ""
        End Sub
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            If port.IsOpen <> False Then
                port.Close()
            End If
        End Sub
    End Class

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年11月10日 10:13
    版主