1.在Winform中要做一个多线程的东西。
有两个数组。int a[1000],int b[1000]
2.点击开始按钮后,分别启动两个线程去遍历数组a 和数组b.在遍历的同时,将遍历信息输出到TextBox或者其他的可以显示遍历信息的控件。或者用progerssbar显示遍历的进度。这两个线程不涉及到通讯。
3.点击取消按钮,如果a,b数组没有遍历完的话,就停止遍历。不知道这个功能怎么做。要是哪位大虾能给个Demo就好了。或者给个提示。我自己做多线程的时候,比如点击开始按钮后,执行如下功能:
Thread t1 = new Thread(new ThreadStart(Func1));//
Thread t2 = new Thread(new ThreadStart(Func2));//
t1.Start();
t2.Start();
t1.Join();
t2.Join(); private void Func1()
{
for (int i = 0; i < 1000; i++)
{
TextBox.Text=i.ToString();
好像多线程调用控件时,会出问题。
Thread.Sleep(500);
}
}各位帮我想想办法把。不知道怎么办的帮我顶顶吧。没天发一贴,系统给你加人情分哦!
有两个数组。int a[1000],int b[1000]
2.点击开始按钮后,分别启动两个线程去遍历数组a 和数组b.在遍历的同时,将遍历信息输出到TextBox或者其他的可以显示遍历信息的控件。或者用progerssbar显示遍历的进度。这两个线程不涉及到通讯。
3.点击取消按钮,如果a,b数组没有遍历完的话,就停止遍历。不知道这个功能怎么做。要是哪位大虾能给个Demo就好了。或者给个提示。我自己做多线程的时候,比如点击开始按钮后,执行如下功能:
Thread t1 = new Thread(new ThreadStart(Func1));//
Thread t2 = new Thread(new ThreadStart(Func2));//
t1.Start();
t2.Start();
t1.Join();
t2.Join(); private void Func1()
{
for (int i = 0; i < 1000; i++)
{
TextBox.Text=i.ToString();
好像多线程调用控件时,会出问题。
Thread.Sleep(500);
}
}各位帮我想想办法把。不知道怎么办的帮我顶顶吧。没天发一贴,系统给你加人情分哦!
解决方案 »
- 急需一个DEV EXPRESS的gridControl控件的案例
- NorthwindEntities 整个程序用一个好,还是用到时NEW一个好
- WinFrom TreeView 数据库动态加载方式,在线等
- 文本文件读写速度挺慢的
- 請教:關於重寫Equals(object obj)、GetHashCode()
- [求资料]是关于数据库正确设计的最好有案例
- 救急啊~~~treeview
- 个人开发软件,何时请美工做界面比较合适?
- DataSet同时更新其中的多个表时,在处理完第一个表后会报告后边的表找不到前边表里的列,如何解决?
- WINDOWS服务启动应用程序看不到界面
- C#如何实现SECS标准语句?急,急,急
- c#中RichTexBox的格式问题
private void bgwCopyAssembly_DoWork(object sender, DoWorkEventArgs e)
{
DoWork();
} private void bgwCopyAssembly_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.PerformStep();
} private void bgwCopyAssembly_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar.Value = progressBar.Maximum;
Close();
}
private void Func1()
{
if (this.textBox.InvokeRequired)//等待异步
{
Client cl = new Client(Func1);
this.Invoke(cl);//通过代理调用刷新方法
}
else
{
for (int i = 0; i < 1000; i++)
{
TextBox.Text=i.ToString();
Thread.Sleep(500);
}
}
}
创建代理delegate void SetTextCallback(string text);
创建和启动线程this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
线程中要求改主窗体UI中的text属性private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
}
调用窗体中的函数用invoke传递参数private void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
可以在构造函数里设置.. this.CheckForIllegalCrossThreadCalls =false;
方法2:
通过委托:
创建代理delegate void SetTextCallback(string text);
创建和启动线程this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe));
this.demoThread.Start();
线程中要求改主窗体UI中的text属性private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
}
调用窗体中的函数用invoke传递参数private void SetText(string text) { if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
如果,只需要在某些Form中消除Cross-thread InvalidOperationException建议,可以在Form的.ctor中,InitializeComponent语句后将CheckForIllegalCrossThreadCalls设置为false 。
public Form1() {
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
http://blog.sina.com.cn/s/blog_4cb80898010009de.html
另外backgroupwork控件我觉得也是可以做到,但是要用到两个,显示给UI的提示信息不太好。我在试试。各位如果有更好的方法,贴出来一起学习下。
搞不出来了,现在先尝试用backgroupwork控件做做。如何用多线程的东西,比较麻烦,我比较偏向使用多线程去完成这个功能。
各位如果写代码的话,尽量详细点,最好是个 DEMO。
谢谢了。看到大家这么热心。泪奔
{
TextBox.Text=i.ToString();
好像多线程调用控件时,会出问题。
Thread.Sleep(500);
}执行完这段代码后才能返回,返回后才能看到运行结果,所以当然不对了呀。
Thread.Sleep(500)是程序停止执行,等500毫秒呀。
用线程,Invoke,委托。
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(Func1));
t.Start();
}
private delegate void Change(int i);
private void test(int i)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new Change(test), i);
}
else
{
textBox1.Text = i.ToString();
}
}
private void Func1()
{
for (int i = 0; i < 1000; i++)
{
test(i);
Thread.Sleep(500);
}
}
我想中途退出,点了X和用this.dispose()都不管用呢??如何杀死这个线程呢??
不知道你们有什么更好方法没?我随时关注。贴我肯定会结的。能提出好的方法的,分数肯定是有的,就当是讨论。大家一起出处主意。好的方法最好把例子程序贴出来。这样我也好在机器上跑跑,实验下方法。
辛苦各位了。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;namespace 多线程_读取数组显示TextBox_03._5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
st = new setText(this.SetTextBox1);
}
int[] s1 = new int[5] { 1, 2, 3, 4, 5 };
string[] s2 = new string[5] { "000", "001", "002", "003", "004" };
Thread t1;
Thread t2;
public delegate void setText (string str );
public void SetTextBox1(string str)
{ if (this.InvokeRequired)
{
this.Invoke(this.st, str);
}
else
{
this.textBox1.Text += str;
}
}
setText st ;
public static object o= new object();
public void F1()
{
for (int i = 0; i < s1.Length; i++)
{
string str = s1[i].ToString() + "\r\n";
st(str);
Thread.Sleep(500);
} }
public void F2()
{
for (int i = 0; i < s2.Length; i++)
{
string str = s2[i].ToString() + "\r\n";
st(str);
Thread.Sleep(500);
} }
private void button1_Click(object sender, EventArgs e)
{
t1 = new Thread(new ThreadStart(F1));
t2 = new Thread(new ThreadStart(F2));
t1.Start();
t2.Start();
} private void button2_Click(object sender, EventArgs e)
{
t1.Join();
t2.Join();
}
}}
使用BackgroundWorker组件进行异步操作编程
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699305.aspx实现增强的异步任务执行组件
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699847.aspx