Ejemplo n.º 1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            source           = new CancellationTokenSource();
            pbStatus.Maximum = lvList.Items.Count;
            lblMax.Text      = pbStatus.Maximum + "";
            pbStatus.Value   = 0;
            var items = lvList.Items;

            foreach (ListViewItem lvItem in items)
            {
                var i = lvList.Items.IndexOf(lvItem);
                Task.Factory.StartNew(() =>
                {
                    sem.Wait();
                    if (!source.Token.IsCancellationRequested)
                    {
                        nowValueDelegate now = new nowValueDelegate(setNow);
                        Invoke(now, i, "等待中……", false);
                        Thread.Sleep(100);
                    }

                    return(4);
                }).ContinueWith((item) =>
                {
                    if (!source.Token.IsCancellationRequested)
                    {
                        nowValueDelegate now = new nowValueDelegate(setNow);
                        Invoke(now, i, "完成", true);
                        Console.WriteLine(i);
                    }
                    sem.Release();
                });;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 线程执行中的事件,设置进度条当前进度
        /// 但是我不能直接操作进度条,需要一个委托来替我完成
        /// </summary>
        /// <param name="sender">ThreadMethod函数中传过来的当前值</param>
        /// <param name="e"></param>
        void method_threadEvent(object sender, EventArgs e)
        {
            int nowValue         = Convert.ToInt32(sender);
            nowValueDelegate now = new nowValueDelegate(setNow);

            this.Invoke(now, nowValue);
        }
Ejemplo n.º 3
0
        private void SetProgressValue(int maximun, int position)
        {
            maxValueDelegate maxValueDelegate = new maxValueDelegate(SetMaxValue);
            nowValueDelegate nowValueDelegate = new nowValueDelegate(SetNowValue);

            this.pbcProcess.Invoke(maxValueDelegate, maximun);
            this.pbcProcess.Invoke(nowValueDelegate, position);

            toolStripProgressBar1.ProgressBar.Invoke(maxValueDelegate, maximun);
            toolStripProgressBar1.ProgressBar.Invoke(nowValueDelegate, position);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 线程执行中的事件,设置进度条当前进度
 /// 但是我不能直接操作进度条,需要一个委托来替我完成
 /// </summary>
 /// <param name="sender">ThreadMethod函数中传过来的当前值</param>
 /// <param name="e"></param>
 void method_threadEvent(object sender, EventArgs e)
 {
     int nowValue = Convert.ToInt32(sender);
     nowValueDelegate now = new nowValueDelegate(setNow);
     Invoke(now, nowValue);
 }