我写了段代码,单个计数没问题,两个文本框同时计数页面卡死,好像线程阻塞一样,求解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 ParaThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
setTextDel = new SetTextBoxTextDelegate(this.SetText);
}
delegate void SetTextBoxTextDelegate(TextBox textbox,string text);
SetTextBoxTextDelegate setTextDel = null;
private void SetText(TextBox textbox, string text)
{
textbox.Text = text;
}
private void btnCountA_Click(object sender, EventArgs e)
{
ParameterizedThreadStart ptsA = new ParameterizedThreadStart(this.process);
Thread thA = new Thread(ptsA);
thA.IsBackground = true;
thA.Start(txtNumA);
} private void process(object textBox)
{
TextBox tb = (TextBox)textBox;
for (int i = 0; i < 9999999; i++)
{
this.Invoke(setTextDel,tb,i.ToString());
//Thread.Sleep(100);加上Sleep,两个文本框能同时计数
}
} private void btnNumB_Click(object sender, EventArgs e)
{
ParameterizedThreadStart ptsB = new ParameterizedThreadStart(this.process);
Thread thB= new Thread(ptsB);
thB.IsBackground = true;
thB.Start(txtNumB);
}
}
}
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 ParaThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
setTextDel = new SetTextBoxTextDelegate(this.SetText);
}
delegate void SetTextBoxTextDelegate(TextBox textbox,string text);
SetTextBoxTextDelegate setTextDel = null;
private void SetText(TextBox textbox, string text)
{
textbox.Text = text;
}
private void btnCountA_Click(object sender, EventArgs e)
{
ParameterizedThreadStart ptsA = new ParameterizedThreadStart(this.process);
Thread thA = new Thread(ptsA);
thA.IsBackground = true;
thA.Start(txtNumA);
} private void process(object textBox)
{
TextBox tb = (TextBox)textBox;
for (int i = 0; i < 9999999; i++)
{
this.Invoke(setTextDel,tb,i.ToString());
//Thread.Sleep(100);加上Sleep,两个文本框能同时计数
}
} private void btnNumB_Click(object sender, EventArgs e)
{
ParameterizedThreadStart ptsB = new ParameterizedThreadStart(this.process);
Thread thB= new Thread(ptsB);
thB.IsBackground = true;
thB.Start(txtNumB);
}
}
}
解决方案 »
- C# Wince 自定义控件开发
- 操作的动态显示问题
- 哪种设置背景图的方法比较好
- c# https 验证的问题
- 新建BasePage类,aspx继承它。获取TextBox控件的值的问题?
- C#调用一个vc的dll会报access violation的异常!
- 如何在一个TextBox里面输出数组且自动换行?
- DataSet设置主键问题
- 索引超出范围。DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles[0];
- c# 调用Matlab组件检索 COM 类工厂中 CLSID 为 {56316DA6-9A52-4025-A23A-D5DDC98B2968} 的组件时失败,原
- 为什么把winform的菜单与工具栏的rendermode设置成system后。效果却跟系统不一样?
- 模拟post 重定向,应该怎么处理?
this.Invoke(setTextDel,tb,i.ToString());
改为:this.BeginInvoke(setTextDel,tb,i.ToString());
WindowsFormsSynchronizationContext Sync = new WindowsFormsSynchronizationContext();
private void SetText(TextBox textbox, string text)
{
Sync.Send(_=> { textbox.Text = text; },null);
}
你可以通过Thread.Sleep(1);来让当前循环休眠一下,这样UI就不会被阻塞了。总之就是你process里面一次循环的间隔时间稍微长一点的话,UI的更新就不会频繁到阻塞了。