往数据库导入文件的时候,由于数据多~我做了一个导入等候,想在上面显示导入的进度~但是不知道怎么给进程传递参数。

解决方案 »

  1.   

           //设置进度条方法
            private void SetprogressBar()
            {
                this.progressBar.Visible = true;            this.progressBar.Minimum = 0;
                this.progressBar.Maximum = this.m_DataSet.Tables[0].DefaultView.Count;
                this.progressBar.Value = 0;
                this.progressBar.Step = 1;        }
    private void toolStripButtonToPC_Click(object sender, EventArgs e)
    {
         this.m_UpLoadDataThread = new Thread(new ThreadStart(this.UpLoadOrderData));
                        this.m_UpLoadDataThread.IsBackground = true;
                        this.m_UpLoadDataThread.Start();}
    private void UpLoadOrderData()
            {
                try
                {
                    this.SetprogressBar();                int x = 0;                int Count = this.m_DataSet.Tables[0].DefaultView.Count;                foreach (DataRow row in m_DataSet.Tables[0].Rows)
                    {
                        if (this.Stop)
                        {
                            return;
                        }                    //插入数据库
                        InExcelHelper.AddSkee(info);
                        // 添加显示列表
                        this.bindingSource.Add(info);
                        //进度条
                        this.progressBar.Value = x;
                        x++;                }            }            catch (Exception ex)
                {
                    MainHelper.ShowException(ex);
                }
     }
      

  2.   

    public class Test 
    {
        private string s1;
        private string s2;
        public Test(string a,string b) 
        {
            this.s1 = a;
            this.s2 = b;
        }
        public void proc() 
        {
            MessageBox.Show(s1+s2);
        }
    }public static void Main() 
    {
        Test test = new Test("hello ", " world");
        Thread t = new Thread(new ThreadStart(test.proc));
        t.Start();
    }
      

  3.   

    线程?进程??
    线程的话~
    using System;
    using System.Threading;//ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
    public class ThreadWithState {
         //要用到的属性,也就是我们要传递的参数
         private string boilerplate;
         private int value;//包含参数的构造函数
         public ThreadWithState(string text, int number) 
         {
             boilerplate = text;
             value = number;
         }    //要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
         public void ThreadProc() 
         {
             //这里就是要执行的任务,本处只显示一下传入的参数
              Console.WriteLine(boilerplate, value); 
         }
    } //用来调用上面方法的类,是本例执行的入口
    public class Example {
         public static void Main() 
         {
            //实例化ThreadWithState类,为线程提供参数
             ThreadWithState tws = new ThreadWithState(
                 "This report displays the number {0}.", 42);       // 创建执行任务的线程,并执行
             Thread t = new Thread(new ThreadStart(tws.ThreadProc));
             t.Start();
             Console.WriteLine("Main thread does some work, then waits.");
             t.Join();
             Console.WriteLine(
                 "Independent task has completed; main thread ends.");  
         }
      

  4.   

    楼上说的要么文不对题要么就是太空泛了 我给点实际的
    //在窗体上添加一个ProgressBar progressBar1 
    //添加一个按钮 开始导入 button1
    button1.Click +=delegate(object sender,EventArgs e)
    {
    new Thread(delegate()
    {
    //在这里导入数据 
    //for(i=0;i<n;i++)
    //{
    this.Invoke(new MethodInvoker(delegate(){this.progressBar1.Value=progressBar1.MaxValue*(i/n);}));
    //}结束导入
    }).Start();
    };
      

  5.   

    楼上的代码我是该放在button1的CLICK事件里嘛
      

  6.   

    假设你窗口上 有progressBar1和button1 把下面的代码放到Form1_Load里面 启动 点击按钮 就能看到效果了
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;            button1.Click += delegate(object sd, EventArgs ea)
                {
                    new System.Threading.Thread(delegate()
                    {
                        for (int i = 0; i < 500; i++) //假设你导入数据需要500次动作
                        {
                            //在这里做一次导入动作
                               //...
                            //下面一句就是刷新进度条的语句
                            this.Invoke(new MethodInvoker(delegate() { this.progressBar1.Value = i*100/500; }));
                            //这一句仅仅为了这一段显示效果,你写上导入代码以后把这句删了
                            System.Threading.Thread.Sleep(100);
                        }
                    }).Start();
                };
      

  7.   

    10楼的方法~我重新绑定DATAGRIDVIEW的时候报错了
      

  8.   

    这种问题是比较常见的,基本可以用委托来实现。有时间我总结一下委托的应用。你可以在网上查INVOKE的用法。或者不同线程间调用控件,即可找到N多解决方法。