C#后台工作者未完成

时间:2018-09-16 19:13:20

标签: c# asynchronous backgroundworker

我遇到问题,我的后台工作人员没有完成,因此在运行一次后挂起。后台工作者处理运行线性阶段的程序,因此正确的时机非常重要(因此,所有Thread.Sleep()调用都如此。)

这是我的代码:

public MainForm()
        {
            InitializeComponent();
            bgW2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgW2_RunWorkerCompleted);
        }
private void bgW2_DoWork(object sender, DoWorkEventArgs e)
        {
            string program = @"C:\Users\mikegjohn\source\repos\CLL AutoForce\Test.csv";
            var stream = new FileStream(program, FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(stream))
            {
                string ln;
                while ((ln = streamReader.ReadLine()) != null)
                {
                    string[] lnArr = ln.Split(',');

                    switch (lnArr[1])
                    {
                        case "0":

                            sendCommand("L-");
                            string status = "";
                            while (status != "0")
                            {
                                Thread.Sleep(250);
                                status = loopCommands("PS");
                            }
                            Thread.Sleep(1000);
                            break;

                        case "1":
                            string p = Fn.convertPos(lnArr[3]);
                            sendCommand("X"+p);
                            string s = Convert.ToString(Fn.backconvert(loopCommands("PX")));

                            while (s != lnArr[3])
                            {
                                Thread.Sleep(250);
                                s = Convert.ToString(Fn.backconvert(loopCommands("PX")));
                            }
                            Thread.Sleep(1000);
                            break;
                        case "2":
                            Fn.MeasureLabel();
                            break;
                        case "3":
                            sendCommand("HSPD=500000");
                            Thread.Sleep(500);
                            sendCommand("LSPD=250000");
                            Thread.Sleep(500);
                            sendCommand("X0");
                            string x = Convert.ToString(Fn.backconvert(loopCommands("PX")));

                            while (x != lnArr[4])
                            {
                                Thread.Sleep(250);
                                s = Convert.ToString(Fn.backconvert(loopCommands("PX")));
                            }
                            Thread.Sleep(1000);
                            sendCommand("HSPD=250000");
                            sendCommand("LSPD=50000");
                            break;
                    }

                }
            }

        }


private void btnRun_Click(object sender, EventArgs e)
    {

        bgW2.RunWorkerAsync();
    }

    private void bgW2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        MessageBox.Show("Program completed");
    }

Messagebox.Show从不触发。我看了一些与此类似的其他问题,这些问题主要指向UI线程的阻塞。有人可以为此指出正确的方向吗?

2 个答案:

答案 0 :(得分:1)

您可能缺少DoWork事件。

public Form1() { 
    InitializeComponent(); 
    bgW2.DoWork += new DoWorkEventHandler(bgW2_DoWork) ;
    bgW2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgW2_RunWorkerCompleted);
}

https://stackoverflow.com/a/14734610

答案 1 :(得分:0)

发现了我的问题-这是(function() { angular.module('myModule') .controller('mainCtrl', function ($scope) { // controller logic })(); 中的一个错误,我没有发现

case "3"