利用多线程从长时间运行的操作中分离出用户界面的问题

ngwxp 2008-07-31 02:33:53
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());
这里提示参数计数不匹配,不知道怎么弄,

请大家帮忙,谢谢
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laxila 2008-09-30
  • 打赏
  • 举报
回复
强烈支持一下下,呵呵
majiajun_no_2 2008-09-29
  • 打赏
  • 举报
回复
学习了,留个脚印
lehedele 2008-09-28
  • 打赏
  • 举报
回复
新手学习中
marvelstack 2008-08-02
  • 打赏
  • 举报
回复
这种情况下建议使用BackgroundWorker来做,这里有指导性的文章,
使用BackgroundWorker组件进行异步操作编程
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699305.aspx
rangeon 2008-07-31
  • 打赏
  • 举报
回复
给个参考,VC的,借签一下吧
http://topic.csdn.net/t/20050621/08/4095521.html
赵一一 2008-07-31
  • 打赏
  • 举报
回复
帮你顶一下。顶成热贴。

111,083

社区成员

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

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

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