Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过另一个线程显示/隐藏标签_C#_Multithreading_Winforms - Fatal编程技术网

C# 通过另一个线程显示/隐藏标签

C# 通过另一个线程显示/隐藏标签,c#,multithreading,winforms,C#,Multithreading,Winforms,有些东西在我的代码中不起作用,所以我想问问是否有人可以帮助我。到目前为止,我有: private void searchList_TextChanged(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(setLabel)); th.IsBackground = true; th.Start(); //s

有些东西在我的代码中不起作用,所以我想问问是否有人可以帮助我。到目前为止,我有:

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);
    }
private void searchList\u text已更改(对象发送方,事件参数e)
{
Thread th=新线程(新线程开始(设置标签));
th.IsBackground=true;
th.Start();
//一些需要时间的代码
if(searchBox.Text==String.Empty)
{
listViewType.Items.Clear();
fillListView();
}
其他的
{
listViewType.Items.Clear();
var matchings=stringTypes.FindAll(委托(字符串s){返回s.StartsWith(searchBox.Text);});
for(int i=0;isearchLabel.Visible=true;
searchLabel.BeginInvoke(集);
}

因此,searchLabel是我要显示/隐藏的标签。我在这里尝试在操作开始前显示标签,并在操作完成后隐藏标签。不知何故,它会在代码执行后显示(//一些需要时间的代码),然后保持可见。如何正确编码?

您正在处理UI线程

private void searchList_TextChanged(object sender, EventArgs e)
{
    searchLabel.Visible = false;
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    //some code that needs time
    Thread.Sleep(1000);
}

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    SetLabelVisible(true);
}

private delegate void SetLabelVisibleDelegate(bool status);

private void SetLabelVisible(bool status)
{
    if (searchLabel.InvokeRequired)
        searchLabel.Invoke(new SetLabelVisibleDelegate(SetLabelVisible), status);
    else
        searchLabel.Visible = status;
}
你的

将挂起UI线程,因此不会重新绘制,您应该使用新的async/await工具

Task.Run可能不适合您的情况,但我需要进一步了解“一些代码”

private async void searchList_TextChanged(object sender, EventArgs e)
{
    searchLabel.Visible = true;

    await Task.Run(() =>
    {
        // code that needs time 
    });

    // simulate
    await Task.Delay(5000);

    searchLabel.Visible = false;
}

您正在UI线程上进行工作

你的

将挂起UI线程,因此不会重新绘制,您应该使用新的async/await工具

Task.Run可能不适合您的情况,但我需要进一步了解“一些代码”

private async void searchList_TextChanged(object sender, EventArgs e)
{
    searchLabel.Visible = true;

    await Task.Run(() =>
    {
        // code that needs time 
    });

    // simulate
    await Task.Delay(5000);

    searchLabel.Visible = false;
}

您正在UI线程上进行工作

你的

将挂起UI线程,因此不会重新绘制,您应该使用新的async/await工具

Task.Run可能不适合您的情况,但我需要进一步了解“一些代码”

private async void searchList_TextChanged(object sender, EventArgs e)
{
    searchLabel.Visible = true;

    await Task.Run(() =>
    {
        // code that needs time 
    });

    // simulate
    await Task.Delay(5000);

    searchLabel.Visible = false;
}

您正在UI线程上进行工作

你的

将挂起UI线程,因此不会重新绘制,您应该使用新的async/await工具

Task.Run可能不适合您的情况,但我需要进一步了解“一些代码”

private async void searchList_TextChanged(object sender, EventArgs e)
{
    searchLabel.Visible = true;

    await Task.Run(() =>
    {
        // code that needs time 
    });

    // simulate
    await Task.Delay(5000);

    searchLabel.Visible = false;
}


整个过程对我来说并不清楚。将“需要时间的代码”放在后台线程似乎更符合逻辑。在UI线程(或主线程)中,您应该很容易地更改标签的可见状态。在这之后,只需在完成后在后台线程中将可见状态调用为True。我想这样做。可能吗?我尝试了“调用”现在,我的答案仍然是相同的。整个过程对我来说并不清楚。将“需要时间的代码”放在后台线程似乎更符合逻辑。在UI线程(或主线程)中您应该很容易地更改标签的可见状态。之后,只需在完成后在后台线程中将可见状态调用为True。我想这样做。可能吗?我现在尝试了“调用”,但在下面的答案中仍然是相同的。整个过程对我来说并不清楚。将“需要时间的代码”放在后面似乎更符合逻辑到后台线程。在UI线程(或主线程)中,您应该很容易地更改标签的可见状态。在这之后,只需在完成后在后台线程中将可见状态调用为True。我想这样做。可能吗?我尝试了“调用”现在,我的答案仍然是相同的。整个过程对我来说并不清楚。将“需要时间的代码”放在后台线程似乎更符合逻辑。在UI线程(或主线程)中您应该很容易地更改标签的可见状态。之后,只需在完成后在后台线程中将可见状态调用为True。我想这样做。可能吗?我现在尝试了“调用”,但在“一些代码”下面的回答中仍然是相同的只需在ListView中搜索。因此,它接受文本框输入,清除ListView,搜索列表中的子字符串,将匹配的字符串放入新列表,然后用这些字符串填充ListView并显示它们。这应该没问题,请确保不在非UI线程上操纵列表视图。请尝试上面的代码,并延迟你会看到它是有效的。他需要很长的时间来加载…比平常长得多。“一些代码”只需在ListView中搜索。因此,它接受文本框输入,清除ListView,搜索列表中的子字符串,将匹配的字符串放入新列表,然后用这些字符串填充ListView并显示它们。这应该没问题,请确保不在非UI线程上操纵列表视图。请尝试上面的代码,并延迟你会看到它是有效的。他需要很长的时间来加载…比平常长得多。“一些代码”只需在ListView中搜索。因此,它接受文本框输入,清除ListView,搜索列表中的子字符串,将匹配的字符串放入新列表,然后用这些字符串填充ListView并显示它们。这应该没问题,请确保不在非UI线程上操纵列表视图。请尝试上面的代码,并延迟你会看到它是有效的。他需要很长的时间来加载…比平常长得多。“一些代码”只需在ListView中搜索。因此,它接受文本框输入,清除ListView,搜索列表中的子字符串,将匹配的字符串放入新列表,然后用这些字符串填充ListView并显示它们。这应该没问题,请确保不在非UI线程上操纵列表视图。请尝试上面的代码,并延迟你会看到它是有效的。他需要很长的时间来加载…比平常长得多。不知何故,他不想启动“doWork”方法。它永远不会到达。@ScorpX你应该通过VS Designer添加BackgroundWorker控件,然后自动生成两个事件处理程序子例程:doWork和RunWo