C#,后台工作者类
当我编译这段代码时,我得到了他的错误,对象引用设置为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
argumenttest
最终成为null
有两种可能的方式:argumenttest
被作为null
发送到 < code>RunWorkerAsync。e.Argument as test;
e.Argument
与test
不兼容,并且as
运算符使其为null
。很难看出是哪一个,因为上面的代码示例非常混乱。
编辑
您能否确认您的代码完全与 decyclone 编辑的一样?在这种情况下,它看起来不错,并且据我所知应该可以工作。
在 DoWork 方法的第一行设置断点,调试时您应该可以通过将鼠标悬停在
e.Argument
上轻松查看 1. 或 2. 是否是问题。There are two possible ways
argumenttest
ends up asnull
:argumenttest
was sent asnull
into theRunWorkerAsync
.e.Argument as test;
e.Argument
is something not compliant withtest
, and theas
operator makes itnull
.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
.我看到的一个问题是,您在运行工作程序之前没有设置事件处理程序,因此
必须在运行之前调用这两行
One problem that I see is that you don't set the event handler before you run the worker, so these 2 lines
have to be called before
您应该在表单加载中或在调用 RunWorkerAsync 之前订阅 DoWork 和 RunCompleted。
将以上行从 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.
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.