简体   繁体   English

C#,后台工作者类

[英]C#, background worker class

when i compile this code i get his error, object reference set to null, and the error location is in Dowork, argumenttest.valueone = 8; 当我编译此代码时,我得到了他的错误,对象引用设置为null,并且错误位置在Dowork中,argumentstest.valueone = 8;。

public partial class Form1 : Form
{
    BackgroundWorker bgw1 = new BackgroundWorker();
    public Form1()
    {
        InitializeComponent();
        // bgw1.RunWorkerAsync(test1);

        test test1 = new test
        {
            valueone = 5,
            valuetwo = 10
        };
        bgw1.RunWorkerAsync(test1);
    }

    class test
    {

        public int valueone { get; set; }
        public int valuetwo { get; set; }
    }

    private void bgw1_DoWork(Object sender, DoWorkEventArgs e)
    {
        test argumenttest = e.Argument as test;
        Thread.Sleep(10);

        argumenttest.valueone = 8;
        argumenttest.valuetwo = 10;

        e.Result = argumenttest;
    }

    private void bgw1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
    {
        test test12 = e.Result as test;
        button1.Text = test12.valueone.ToString();// +test.valuetwo.ToString();
        //this.Text = test.valueone.ToString() + " "+ test.valuetwo.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        bgw1.DoWork += bgw1_DoWork;
        bgw1.RunWorkerCompleted += bgw1_RunWorkerCompleted;
        //bgw1.RunWorkerAsync(test);

    }
}

There are two possible ways argumenttest ends up as null : argumenttest test最终有两种可能为null

  1. argumenttest was sent as null into the RunWorkerAsync . argumenttest作为null发送到RunWorkerAsync

  2. e.Argument as test; e.Argument is something not compliant with test , and the as operator makes it null . e.Argument是与test不兼容的东西, as运算符使它为null

It hard to see which one, since your code example above is quite messed up. 很难看到哪一个,因为上面的代码示例已经很混乱。

EDIT 编辑

Can you confirm that your code is exactly as decyclone edited it? 您可以确认您的代码与decyclone 完全一样吗? In that case, it looks fine, and should have worked as far as I can see. 在这种情况下,它看起来还不错,并且据我所知应该可以正常工作。

Set a breakpoint on the first line of the DoWork method, and when debugging you should be easily able to see if 1. or 2. is the problem by hovering over e.Argument . 在DoWork方法的第一行上设置一个断点,并且在调试时,您应该可以将e.Argument悬停在e.Argument上轻松查看1.还是2.是问题。

One problem that I see is that you don't set the event handler before you run the worker, so these 2 lines 我看到的一个问题是您在运行工作程序之前未设置事件处理程序,因此这两行

    bgw1.DoWork += bgw1_DoWork;
    bgw1.RunWorkerCompleted += bgw1_RunWorkerCompleted;

have to be called before 必须先打电话

    bgw1.RunWorkerAsync(test1);

You should subscribe to DoWork and RunCompleted in Form load or before you make a call to RunWorkerAsync. 您应该在窗体加载中或在调用RunWorkerAsync之前订阅DoWork和RunCompleted。

bgw1.DoWork += bgw1_DoWork;
bgw1.RunWorkerCompleted += bgw1_RunWorkerCompleted;

move the above lines to Form_Load from Button Click event handler. 将以上各行从Button Click事件处理程序移至Form_Load。

And move bgw1.RunWorkerAsync(test1); 并移动bgw1.RunWorkerAsync(test1); to button click handler from Form Load method. 从Form Load方法中单击单击处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM