首页 新闻 会员 周边

c#异步委托,请大家帮忙

0
悬赏园豆:5 [已关闭问题] 关闭于 2009-10-31 21:23

namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public delegate void changeProgress(int current);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            begin b = new begin();
            b.Go();
        }
        public  void progressChange(int current)
        {
            progressBar1.Value = current;
        }
    }
}


namespace WindowsForm
{
    public class begin
    {
        public void Go()
        {
            Form1 fom1 = new Form1();
            WindowsForm.Form1.changeProgress cp = new Form1.changeProgress(fom1.progressChange);
            for (int i = 1; i < 100; i++)
            {
                Console.WriteLine(i.ToString());
                cp.BeginInvoke(i, null, null);
                Thread.Sleep(1);
            }


        }

    }
}
为什么滚动条没有变化。

wuhan的主页 wuhan | 初学一级 | 园豆:11
提问于:2009-10-30 16:39
< >
分享
所有回答(3)
0

你实际调试一下,看执行的时候有没有执行到 public  void progressChange(int current)
        {
            progressBar1.Value = current;
        }
这个方法里。

如果执行到了,但实际界面上进度条没有反应的话,你用异步来操作这个begin(多线程)

李.net | 园豆:730 (小虾三级) | 2009-10-30 16:53
调试了,执行到了,但是滚动条就是没有变化。子进程和住进程差距在50左右。
支持(0) 反对(0) wuhan | 园豆:11 (初学一级) | 2009-10-30 16:59
0

是不是要重绘才能看到啊,把全部代码发上来瞧瞧

elite_lcf | 园豆:205 (菜鸟二级) | 2009-10-30 21:26
0

也许要这样    

public class begin

    {

        public void Go(Form1 form1)

        {

            Form1.changeProgress cp = new Form1.changeProgress(form1.progressChange);

            for (int i = 0; i < 100; i++)

            {

                cp(i);

                Thread.Sleep(1);

            }

        }

索夫特威尔 | 园豆:210 (菜鸟二级) | 2009-10-30 23:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册