问题  BackgroundWorker。我如何通过浏览树显示当前进程

csharBeginer

新成员
已加入
2018年9月1日
留言内容
3
编程经验
Beginner
场景_1.
1.用户。按下"Start" button;
2.程序。创建用户元素的实例"ucBackgroundWorker" in "flowLayoutPanel1";
3.程序。在"treeView1";

"Scenario_1"可以无限次重复。

方案_2。
有:-几个过程开始于"flowLayoutPanel1";
- 在"treeView1"有一个过程树;

设想:
1.用户。将光标移到"treeView1";
2.程序。在"flowLayoutPanel2"它显示过程(实例
"ucBackgroundWorker" ")游标所在的节点;


据我了解,我尝试实施此方案(Scenario_1 + Scenario_2)。我认为可以为用户元素实例分配索引,然后根据索引显示在"flowLayoutPanel2".
为了进行测试,该脚本的按钮做了一个按钮"btShowTheProces"
但是该项目不起作用。
调试时出现错误。

Form3.cs

flowLayoutPanel1. 控制项 . 添加 (ucBgWorker [ i_ucBWrk ]);

错误"对象引用不指向该对象的实例。"
ucBgWorker = null;


问题
1.如何解决错误?
2.我可以正确解决我的问题吗?
3.完成此方案的其他方法有哪些?


Form3.cs
C#:
namespace rsh
{
    public partial class Form3 : Form
    {
        // ucBackgroundWorker[] ucBgWorker = null;
        
        int i_ucBWrk=0;
        private ucBackgroundWorker[] ucBgWorker;


        int procNumb;


        public Form3()
        {
            InitializeComponent();
        }


        // ????? ?????? ???? ??????
        void treeView1_AfterSelect(object sender, TreeViewCancelEventArgs e)
        {


        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            i_ucBWrk++;


            ucBgWorker[i_ucBWrk] = new ucBackgroundWorker();


            // ucBgWorker[i_ucBWrk].Done += new Action<string, EventArgs>(Worker_Done);
            // ucBgWorker[i_ucBWrk].Cancel += new Action<string, EventArgs>(Worker_Cancel);


            flowLayoutPanel1.Controls.Add(ucBgWorker[i_ucBWrk]);


            ucBgWorker[i_ucBWrk].Run(1);            
            // ucBgWorker.Run(Convert.ToInt32(textBox1.Text));
        }


        void Worker_Done(string arg, EventArgs evtarg)
        {
            label1.Text = arg + " Done One";
            //System.Threading.Thread.Sleep(1000);
        }


        void Worker_Cancel(string arg, EventArgs evtarg)
        {
            label1.Text = arg + " Cancel Click";
            //System.Threading.Thread.Sleep(1000);
        }




        //  show the process
        private void btShowTheProces_Click(object sender, EventArgs e)
        {
            procNumb = Convert.ToInt32(numericUpDown1.Value);


            flowLayoutPanel2.Controls.Clear();
            flowLayoutPanel2.Controls.Add(ucBgWorker[procNumb]);
        }
    }
}


ucBackgroundWorker.cs

C#:
namespace rsh
{
    public partial class ucBackgroundWorker : UserControl
    {
        BackgroundWorker bgWorker = null;
        public event Action<string, EventArgs> Done;
        public event Action<string, EventArgs> Cancel;


        private static bool m_continue = true;


        // ?????????? ???? ??? ????? ????????? ??????? ? ???, ??? ????????? ???????. ???? ????? ?? ???????????.
        // //msdn.microsoft.com/ru-ru/library/system.threading.manualresetevent(v=vs.110).aspx
        private ManualResetEvent _resetEvent = new ManualResetEvent(false);
        //Semaphore sWaiter = new Semaphore(0, 1);




        public ucBackgroundWorker()
        {
            InitializeComponent();


            bgWorker = new BackgroundWorker();


            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.WorkerReportsProgress = true;


            bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            bgWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }


        public void Run(int counter)
        {
            if (!bgWorker.IsBusy)
            {
                bgWorker.RunWorkerAsync(counter);
            }
            _resetEvent.Set();
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int input = int.Parse(e.Argument.ToString());


            this.BeginInvoke((MethodInvoker)delegate
            {
                lblStatus.Text = "Running";
            });


            for (int i = 1; i <= input; i++)
            {
                _resetEvent.WaitOne();
                Thread.Sleep(1000);
                (sender as BackgroundWorker).ReportProgress(i*1); // ???
                // (sender as BackgroundWorker).ReportProgress(i * 10);
                if ((sender as BackgroundWorker).CancellationPending)
                {
                    this.BeginInvoke((MethodInvoker)delegate
                    {
                        lblStatus.Text = "Cancel";
                    });


                    e.Cancel = true;
                    return;
                }
            }
            Thread.Sleep(1000);
        }




        // This event handler deals with the results of the 
        // background operation.
        // ???? ?????????? ??????? ????? ???? ? ????????????
        // ??????? ????????.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown. 
            // ??????? ???????????? ??????, ????? ???? ??????? ??????????.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                if (Cancel != null)
                    Cancel(this.Name, EventArgs.Empty);
            }
            else
            {
                this.BeginInvoke((MethodInvoker)delegate
                {
                    lblStatus.Text = "Done";
                });


                if (Done != null)
                    Done(this.Name, EventArgs.Empty);
            }
            _resetEvent.Reset();
        }


        // This event handler updates the progress bar. 
        // ???? ?????????? ??????? ????????? ????????? ??????????.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pBar.Refresh();
            pBar.Value = e.ProgressPercentage;
        }






        // *** *** *** ***
        // *** ??????  ***
        // *** *** *** ***
        
        // Cancel
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (bgWorker.IsBusy)
            {
                bgWorker.CancelAsync();
            }
        }


        // Pause
        private void btnPause_Click(object sender, EventArgs e)
        {
            if (bgWorker.IsBusy)
            {
                if (btnPause.Text.ToUpper() == "PAUSE")
                {
                    btnPause.Text = "Resume";
                    m_continue = false;
                    _resetEvent.Reset();
                }
                else 
                if (btnPause.Text.ToUpper() == "RESUME")
                {


                    btnPause.Text = "Pause";
                    m_continue = true;
                    _resetEvent.Set();


                }
            }
        }




    }
}


 

附件

  • 2018-09-04_18-03-05.png
    2018-09-04_18-03-05.png
    12.1 KB · Views: 94
  • 01_pr.zip
    15.9 KB · Views: 46
由主持人最后编辑:

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,523
地点
悉尼,澳大利亚
编程经验
10+
在阅读您的文章之前,我先要说一件事。那是太多代码无法发布。您应该尽一切努力缩小问题范围,仅张贴相关代码。如有必要,您应该创建一个新的测试项目,并仅添加重现该问题所需的内容。这样,我们就不必浪费时间浪费在无关紧要的事情上。如果有机会,我会看看您的问题,但现在我没有时间去解决所有问题。
 
最佳 底部