C#,后台工作者类

发布于 2024-10-16 07:36:38 字数 1312 浏览 4 评论 0原文

当我编译这段代码时,我得到了他的错误,对象引用设置为null,并且错误位置在Dowork中,argumenttest.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);

    }
}

when i compile this code i get his error, object reference set to null, and the error location is in Dowork, argumenttest.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);

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2024-10-23 07:36:38

argumenttest 最终成为 null 有两种可能的方式:

  1. argumenttest 被作为 null 发送到 < code>RunWorkerAsync。

  2. e.Argument as test; e.Argumenttest 不兼容,并且 as运算符使其为 null

很难看出是哪一个,因为上面的代码示例非常混乱。

编辑

您能否确认您的代码完全与 decyclone 编辑的一样?在这种情况下,它看起来不错,并且据我所知应该可以工作。

在 DoWork 方法的第一行设置断点,调试时您应该可以通过将鼠标悬停在 e.Argument 上轻松查看 1. 或 2. 是否是问题。

There are two possible ways argumenttest ends up as null:

  1. argumenttest was sent as null into the RunWorkerAsync.

  2. e.Argument as test; e.Argument is something not compliant with test, and the as operator makes it 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? 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 .

向地狱狂奔 2024-10-23 07:36:38

我看到的一个问题是,您在运行工作程序之前没有设置事件处理程序,因此

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

必须在运行之前调用这两行

    bgw1.RunWorkerAsync(test1);

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);
所有深爱都是秘密 2024-10-23 07:36:38

您应该在表单加载中或在调用 RunWorkerAsync 之前订阅 DoWork 和 RunCompleted。

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

将以上行从 Button Click 事件处理程序移至 Form_Load。

并将 bgw1.RunWorkerAsync(test1); 从 Form Load 方法移至按钮单击处理程序。

You should subscribe to DoWork and RunCompleted in Form load or before you make a call to RunWorkerAsync.

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

move the above lines to Form_Load from Button Click event handler.

And move bgw1.RunWorkerAsync(test1); to button click handler from Form Load method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文