积极答复者
关于form的invoke的问题

问题
-
调用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 的时候,程序就停了不动了????有没有人知道这是为什么啊?- 已移动 Sheng Jiang 蒋晟Moderator 2009年11月5日 18:24 Windows表单类库问题 (发件人:Visual C#)
答案
-
参考下我在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.- 已建议为答案 Raymond TangModerator 2009年11月18日 5:02
- 已标记为答案 韦恩卑鄙 waywaModerator 2011年3月10日 13:07
全部回复
-
参考下我在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.- 已建议为答案 Raymond TangModerator 2009年11月18日 5:02
- 已标记为答案 韦恩卑鄙 waywaModerator 2011年3月10日 13:07