using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace ShowprocessCeshi
{
public partial class Form1 : Form
{
int digits = 0;
public Form1()
{
InitializeComponent();
}
protected delegate void JisuanDelegate(int digits);
protected delegate void showprocess(string pi, int totalDigits, int digitsSoFar);
private void button1_Click(object sender, EventArgs e)
{
digits = Convert.ToInt32(this.textBox1.Text);
JisuanDelegate jisuan = new JisuanDelegate(Jisuan);
jisuan.BeginInvoke(digits, null, null);
//this.BeginInvoke(jisuan);
}
private void Jisuan(int digits)
{
StringBuilder pi = new StringBuilder("3", digits + 2);
ShowProcess(pi.ToString(), digits, 0);
if (digits > 0)
{
pi.Append("."); for (int i = 0; i < digits; i += 9)
{
pi.Append(i);
ShowProcess(pi.ToString(), digits, i);
//Thread.Sleep(1000);
}
}
}
private void ShowProcess(string pi,int totalDigits,int digitsSoFar)
{
if (this.textBox2.InvokeRequired == false)
{
this.listBox1.Items.Add(pi);
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
this.listBox1.Refresh();
progressBar1.Value = digitsSoFar;
progressBar1.Maximum = totalDigits;
}
else
{
showprocess Jindu = new showprocess(ShowProcess);
this.BeginInvoke(Jindu, new object[] { totalDigits, digitsSoFar });
}
}
}
}想利用多线程从长时间运行的操作中分离出用户界面
但是一运行就会在 program.cs 那个
Application.Run(new Form1());
这里提示参数计数不匹配,不知道怎么弄,请大家帮忙,谢谢
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace ShowprocessCeshi
{
public partial class Form1 : Form
{
int digits = 0;
public Form1()
{
InitializeComponent();
}
protected delegate void JisuanDelegate(int digits);
protected delegate void showprocess(string pi, int totalDigits, int digitsSoFar);
private void button1_Click(object sender, EventArgs e)
{
digits = Convert.ToInt32(this.textBox1.Text);
JisuanDelegate jisuan = new JisuanDelegate(Jisuan);
jisuan.BeginInvoke(digits, null, null);
//this.BeginInvoke(jisuan);
}
private void Jisuan(int digits)
{
StringBuilder pi = new StringBuilder("3", digits + 2);
ShowProcess(pi.ToString(), digits, 0);
if (digits > 0)
{
pi.Append("."); for (int i = 0; i < digits; i += 9)
{
pi.Append(i);
ShowProcess(pi.ToString(), digits, i);
//Thread.Sleep(1000);
}
}
}
private void ShowProcess(string pi,int totalDigits,int digitsSoFar)
{
if (this.textBox2.InvokeRequired == false)
{
this.listBox1.Items.Add(pi);
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
this.listBox1.Refresh();
progressBar1.Value = digitsSoFar;
progressBar1.Maximum = totalDigits;
}
else
{
showprocess Jindu = new showprocess(ShowProcess);
this.BeginInvoke(Jindu, new object[] { totalDigits, digitsSoFar });
}
}
}
}想利用多线程从长时间运行的操作中分离出用户界面
但是一运行就会在 program.cs 那个
Application.Run(new Form1());
这里提示参数计数不匹配,不知道怎么弄,请大家帮忙,谢谢
http://topic.csdn.net/t/20050621/08/4095521.html
使用BackgroundWorker组件进行异步操作编程
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699305.aspx