【发布时间】:2015-03-25 20:26:03
【问题描述】:
Someting 在我的代码中不起作用,所以我想问问是否有人可以帮助我。到目前为止我有这个:
private void searchList_TextChanged(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart(setLabel));
th.IsBackground = true;
th.Start();
//some code that needs time
if (searchBox.Text == String.Empty)
{
listViewType.Items.Clear();
fillListView();
}
else
{
listViewType.Items.Clear();
var matchings = stringTypes.FindAll(delegate(string s) { return s.StartsWith(searchBox.Text); });
for (int i = 0; i < matchings.Count; i++)
{
ListViewItem storeMatched = new ListViewItem(matchings[i]);
storeMatched.SubItems.Add(matchings[i]);
listViewType.Items.Add(storeMatched);
}
th.Abort();
searchLabel.Visible = false;
}
private void setLabel()
{
MethodInvoker set = () => searchLabel.Visible = true;
searchLabel.BeginInvoke(set);
}
所以 searchLabel 是我想要显示/隐藏的标签。我在这里尝试在操作开始之前显示标签并在完成后隐藏它。不知何故,它在代码执行后显示(//一些需要时间的代码)然后保持可见。如何正确编码?
【问题讨论】:
-
整个过程我不清楚。将“需要时间的代码”放到后台线程似乎更合乎逻辑。在 UI 线程(或主线程)中,您应该轻松更改标签的可见状态。之后,只需在后台线程中调用 Visible 状态为 True 即可。
-
我想那样做。可能吗?我现在尝试了“调用”,但还是一样
-
看看下面我的回答
标签: c# multithreading winforms