简体   繁体   English

在C#WPF应用程序中处理线程

[英]Handling threads in C# wpf application

I am making a C# wpf application with Visual Studio 2012. There are two textboxes named textboxInput and textboxOutput. 我正在用Visual Studio 2012开发C#wpf应用程序。有两个名为textboxInput和textboxOutput的文本框。 My task is when I am typing in textboxInput it should bring the details in realtime into textboxOutput by searching a database. 我的任务是,当我在textboxInput中键入内容时,应该通过搜索数据库将详细信息实时地带入textboxOutput中。 Since this takes some moment, it slows down my typing speed which the user never want. 由于这花了一些时间,因此减慢了用户从未希望的打字速度。

The thing is when the user is typing "foo", it searches for "f", then "fo" and then "foo". 问题是当用户键入“ foo”时,它先搜索“ f”,然后搜索“ fo”,然后搜索“ foo”。 There are database entries for all of that. 所有这些都有数据库条目。 The user only want to see the details of "foo" not others. 用户只希望查看“ foo”的详细信息,而不希望查看其他内容。 But I cannot predict where will he stops typing. 但是我无法预测他将在哪里停止打字。 I want to give him a smooth typing without lagging. 我想给他一个流畅的打字而又不拖延。 So my idea is using threads. 所以我的想法是使用线程。 In the testChanged event I search it with a Thread and if it again come to the textChange event(typed another letter) it kills the previous thread and runs a new Thread. 在testChanged事件中,我用一个线程进行搜索,如果再次遇到textChange事件(键入另一个字母),它将杀死前一个线程并运行一个新线程。

My current approach. 我目前的做法。

private void textBoxInput_TextChanged(object sender, TextChangedEventArgs e){
    //textBoxOutput.Text=search(textBoxInput.Text);  //previos approach

    //Current approach
    new Thread(delegate() {search(textBoxInput.Text); }).Start();
}

Could you please help me with that code inside 你能帮我里面的代码吗

  1. My current code gives me the exception "The calling thread cannot access this object because a different thread owns it". 我当前的代码给我一个例外“调用线程无法访问该对象,因为另一个线程拥有它”。 How to handle it ? 怎么处理呢?
  2. How can I implement the previous thread killing mechanism ? 如何实现以前的线程杀死机制? (I am a beginner :)) (我是初学者:))
  3. Are there any other good ways to achieve my task ? 还有其他好的方法可以完成我的任务吗?

you can do it like this: 您可以这样做:

   Thread thread;
            bool flag = false;
            public MainWindow()
            {
                InitializeComponent();
                thread = new Thread(new ThreadStart(search));
                thread.Start();
            }

            private void textBoxInput_TextChanged(object sender, TextChangedEventArgs e)
            {
                if(flag)
                   return;
                flag = true;
            }

            private void search()
            {
                while(true)
                {
                    if(flag)
                    {
                        string result = search(textBoxInput.Text);
                        this.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            textBoxOutput.Text = result;
                        }));
                        flag = false;
                    }
                    Thread.Sleep(200);
                }
            }

never do it like this: it kills the previous thread and runs a new Thread . 永远不要这样做: it kills the previous thread and runs a new Thread

The key point is that you can't access UI elements from background threads -- textBoxInput, for example. 关键是您不能从后台线程访问UI元素-例如,textBoxInput。 So you'll have to create a local copy of the Text property first: 因此,您必须首先创建Text属性的本地副本:

string text = textBoxInput.Text;
new Thread(delegate() { search(text); }).Start();

Also, if the "search" function does some kind of UI manipulation, which I assume it does, then you'll need to send that operation back to the UI thread using Dispatcher.BeginInvoke() : 另外,如果“搜索”功能执行某种类型的UI操作(我认为是这样),那么您将需要使用Dispatcher.BeginInvoke()将操作发送回UI线程:

Dispatcher.BeginInvoke(new Action(() => {
    // UI operation here
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM