當我編譯此代碼時,我得到他的錯誤,對象引用設置爲null,並且錯誤位置在Dowork,argumenttest.valueone = 8;C#,後臺工作者類
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);
}
}
您是否可以在代碼中更頻繁地使用單詞測試? ;) – thumbmunkeys 2011-02-09 11:26:03
@ Henk HolterMan:我只是新來的這個背景工作者(線程),我在實踐它之前就做了一些實踐工作。 – safi 2011-02-09 11:31:19