带串口操作+显示的自定义控件如何异步?

pfworld 2009-11-03 01:01:24
自己做了一个LED显示通过串口获取的电子秤数据显示自定义控件,但是在运行中,经常界面卡死!!!

请问如何做带串口操作的自定义控件异步??


-------------
注:回复又分
...全文
337 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
pfworld 2009-11-27
  • 打赏
  • 举报
回复
自己再次UP一下!!
JayPan2008 2009-11-20
  • 打赏
  • 举报
回复
UP
jim646565 2009-11-20
  • 打赏
  • 举报
回复
....电子称 一直给串口发数据,而接受数据的事件一直都在运行...CPU资源一直都被占着不释放 不卡才怪
pfworld 2009-11-20
  • 打赏
  • 举报
回复
自己再次UP一下!!
pfworld 2009-11-18
  • 打赏
  • 举报
回复
希望大家帮助呀!!
Taiyangchen 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 builderc 的回复:]
net2.0下多线程实现串口接收GPS数据读写

这是个 BackgroundWork 和 Control.Invoke 综合使用的例子。
[/Quote]

我们公司就是这样用的
  • 打赏
  • 举报
回复
顶贴友好 好再来!
pfworld 2009-11-14
  • 打赏
  • 举报
回复
希望大家帮助呀!!
lee_b 2009-11-12
  • 打赏
  • 举报
回复
顶,,学习。。接分
pfworld 2009-11-12
  • 打赏
  • 举报
回复
=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=08000000=3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30 3D 30 38 30 30 30 30 30 30
pfworld 2009-11-12
  • 打赏
  • 举报
回复
读取电子秤数据用哪种方法最有效率?如果标准数据9字节,那么分值设置多少?
LutzMark 2009-11-11
  • 打赏
  • 举报
回复
推荐用BackgroundWorker
将耗时方法加到DoWork事件中。
更新UI方法加到RunWorkerCompleted 事件中。

BackgroundWorker backgroundWorker = new System.ComponentModel.BackgroundWorker();
this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.串口操作方法);
this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.更新UI的方法);
langziqian 2009-11-11
  • 打赏
  • 举报
回复
Mark
LutzMark 2009-11-11
  • 打赏
  • 举报
回复
异步操作分线程安全和非线程安全
下面例子分3种方式介绍:
1.开新线程异步操作(非线程安全)
2.根据目标控件的InvokeRequired属性判断是否需要回调操作(线程安全)
3.用BackgroundWorker(线程安全)



using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace winform_线程安全与非线程安全
{
public class Form1 : Form
{
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text);

// This thread is used to demonstrate both thread-safe and
// unsafe ways to call a Windows Forms control.
private Thread demoThread = null;

// This BackgroundWorker is used to demonstrate the
// preferred way of performing asynchronous operations.
private BackgroundWorker backgroundWorker1;

private TextBox textBox1;
private Button setTextUnsafeBtn;
private Button setTextSafeBtn;
private Button setTextBackgroundWorkerBtn;

private System.ComponentModel.IContainer components = null;

public Form1()
{
InitializeComponent();
Form.CheckForIllegalCrossThreadCalls = false;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

// This event handler creates a thread that calls a
// Windows Forms control in an unsafe way.
private void setTextUnsafeBtn_Click(
object sender,
EventArgs e)
{
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcUnsafe));

this.demoThread.Start();
}

// This method is executed on the worker thread and makes
// an unsafe call on the TextBox control.
private void ThreadProcUnsafe()
{
this.textBox1.Text = "This text was set unsafely.";
MessageBox.Show(this.textBox1.Text);
}

// This event handler creates a thread that calls a
// Windows Forms control in a thread-safe way.
private void setTextSafeBtn_Click(
object sender,
EventArgs e)
{
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));

this.demoThread.Start();
}

// This method is executed on the worker thread and makes
// a thread-safe call on the TextBox control.
private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
MessageBox.Show(this.textBox1.Text);
}

// This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}

// This event handler starts the form's
// BackgroundWorker by calling RunWorkerAsync.
//
// The Text property of the TextBox control is set
// when the BackgroundWorker raises the RunWorkerCompleted
// event.
private void setTextBackgroundWorkerBtn_Click(
object sender,
EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}

// This event handler sets the Text property of the TextBox
// control. It is called on the thread that created the
// TextBox control, so the call is thread-safe.
//
// BackgroundWorker is the preferred way to perform asynchronous
// operations.

private void backgroundWorker1_RunWorkerCompleted(
object sender,
RunWorkerCompletedEventArgs e)
{
this.textBox1.Text =
"This text was set safely by BackgroundWorker.";
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.setTextUnsafeBtn = new System.Windows.Forms.Button();
this.setTextSafeBtn = new System.Windows.Forms.Button();
this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 0;
//
// setTextUnsafeBtn
//
this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55);
this.setTextUnsafeBtn.Name = "setTextUnsafeBtn";
this.setTextUnsafeBtn.TabIndex = 1;
this.setTextUnsafeBtn.Text = "Unsafe Call";
this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click);
//
// setTextSafeBtn
//
this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);
this.setTextSafeBtn.Name = "setTextSafeBtn";
this.setTextSafeBtn.TabIndex = 2;
this.setTextSafeBtn.Text = "Safe Call";
this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);
//
// setTextBackgroundWorkerBtn
//
this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55);
this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn";
this.setTextBackgroundWorkerBtn.TabIndex = 3;
this.setTextBackgroundWorkerBtn.Text = "Safe BackgroundWorker Call";
this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click);
//
// backgroundWorker1
//
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(268, 96);
this.Controls.Add(this.setTextBackgroundWorkerBtn);
this.Controls.Add(this.setTextSafeBtn);
this.Controls.Add(this.setTextUnsafeBtn);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion


[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

}
}




Winform中非线程安全方式的异步操作要显示设置窗体属性, Form.CheckForIllegalCrossThreadCalls = false;
sjdev 2009-11-10
  • 打赏
  • 举报
回复
等有空给你回吧,异步处理我用的比较多。
ckl881003 2009-11-10
  • 打赏
  • 举报
回复
开个新的线程吧。。而且串口应该没有那么消耗资源的吧。。
rizher 2009-11-10
  • 打赏
  • 举报
回复
帮顶~~~
wartim 2009-11-10
  • 打赏
  • 举报
回复
自定义控件不一样么

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

X x = new X();
x.Parent = this;
}

class X : UserControl
{
public X()
{
this.BackColor = Color.Red;

Thread T = new Thread(new ThreadStart(DoThread));
T.IsBackground = true;
T.Start();
}

void DoThread()
{
int Start = Environment.TickCount;

try
{
while (true)
{
if (Environment.TickCount - Start > 1000)
{
this.Invoke(new Action(DoSetText));
Start = Environment.TickCount;
}
}
}
catch
{
// 忽略程序退出异常
}
}

void DoSetText()
{
// 修改form标题
this.Parent.Text = DateTime.Now.ToLongTimeString();
}
}
}
}
Thr21ough 2009-11-10
  • 打赏
  • 举报
回复
关注~
pfworld 2009-11-10
  • 打赏
  • 举报
回复
寻求相关解决方案!!UP又分!!
加载更多回复(14)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧