我有一個Windows窗體,其中包含一個列表框(Listbox1
),一個標籤(label1
)和一個按鈕(button1
)。我已經指派一個click事件button1
,代碼如下:如何計算C#中更新的列表框中的條目?
public void button1_Click(object sender, EventArgs e)
{
label1.Text = "Parsing entries && initializing comms ...";
apples = new Task(Apple);
apples.Start();
Task.WaitAll(apples);
label1.Text = "No. of items: " + Listbox1.Items.Count.ToString();
if (Listbox1.Items.Count >= 2)
{
Listbox1.SetSelected(1, true);
}
}
public void Apple() {
//Send 1st command - 90000
command = "90000";
CommPort com = CommPort.Instance;
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Send 2nd command - 90001
command = "90001";
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Send 3rd command - 90002
command = "90002";
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Send 4th command - 90003
command = "90003";
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Send 5th command - 90004
command = "90004";
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Send 6th command - 90005
command = "90005";
if (command.Length > 0)
{
command = ConvertEscapeSequences(command);
com.Send(command);
}
Thread.Sleep(100); //allow 100ms delay for receiving response from serial port
//Listbox1 eventually contains some (~6) entries
}
然而,當我點擊button1
,label1
顯示文本No. of items: 0
,雖然Listbox1
實際上包含6個項目。爲什麼代碼在Listbox1
中有6個項目時返回0?
您的應用程序是否凍結或是否仍然響應?你不應該在UI線程上使用阻塞調用,比如'WaitAll',因爲這會導致死鎖。 –
Ned的評論是正確的,因爲你不應該在UI線程上用'Task.WaitAll()'阻塞。但是,除了這個問題,您提供的代碼並不能解釋計數不匹配:'(Listbox1.Items.Count).ToString()'是正確的,應該顯示當前在'Listbox1'中的項目數。還有其他一些事情正在發生。 –
@特殊醬,我同意。我只是想了解有關該計劃行爲的更多信息。我懷疑我們需要知道'Apple()'裏面發生了什麼, –