Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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动态搜索中断#_C#_Multithreading_Task Parallel Library_Search Engine_Xamarin.forms - Fatal编程技术网

C# 通过任务C动态搜索中断#

C# 通过任务C动态搜索中断#,c#,multithreading,task-parallel-library,search-engine,xamarin.forms,C#,Multithreading,Task Parallel Library,Search Engine,Xamarin.forms,我正在使用Db(通过SQLite.NET PCL,而不是异步版本)。目前,我有一个包含一些数据(取自数据库)的listview,还有一个搜索栏/条目(其nvm),用户可以在其中输入一些值,然后通过LINQ我将进行查询并更新列表的SourceItems 因此,性能问题就来了,因为myDB获得了数百万条记录,而simpleLINQ查询运行速度非常慢。换句话说,当用户输入太快的数据时,应用程序会出现巨大的延迟,有时会崩溃 为了解决这个问题,我想到了一些事情(理论上的解决方案): 1) 需要将方法(我在

我正在使用Db(通过SQLite.NET PCL,而不是异步版本)。目前,我有一个包含一些数据(取自数据库)的listview,还有一个搜索栏/条目(其nvm),用户可以在其中输入一些值,然后通过LINQ我将进行查询并更新列表的SourceItems

因此,性能问题就来了,因为myDB获得了数百万条记录,而simpleLINQ查询运行速度非常慢。换句话说,当用户输入太快的数据时,应用程序会出现巨大的延迟,有时会崩溃 为了解决这个问题,我想到了一些事情(理论上的解决方案):

1) 需要将方法(我在db中进行查询)放在任务上(解锁我的主UI线程)

2) 初始化计时器,然后打开并:

  • 如果超过1秒,则=>在任务上运行我的方法(查询)(类似于后台线程)
  • 如果1秒没有过去,则退出匿名方法
类似的东西或任何建议。谢谢

UPD:
所以说实话,我试得太多了,没有得到好结果
顺便说一句,我当前的代码(片段):
1) 我的搜索方法

public void QueryToDB(string filter)
        {
            this.BeginRefresh ();

            if (string.IsNullOrWhiteSpace (filter))
            {
                this.ItemsSource = SourceData.Select(x => x.name); // Source data is my default List of items
            }
            else 
            {
                var t = App.DB_Instance.FilterWords<Words>(filter); //FilterWords it's a method,where i make direct requests to the database 
                this.ItemsSource = t.Select(x => x.name); 
            }
            this.EndRefresh ();
        }
主要的问题是如何实现这一部分(使用这些案例),其中1秒过去了,然后我将进行查询以更新列表的sourceItems(每次,当用户向搜索栏输入一些值时,此触发器(计时器)必须再次刷新为零)

任何帮助都将不胜感激,谢谢

PS对不起我的英语技能

您希望在实际执行搜索之前等待。中途终止搜索任务可能会导致未定义的行为

您希望保存当前搜索筛选器,并在1秒后再次进行比较。如果这一点没有改变,请进行搜索。否则,中止:

searchBar.TextChanged += async (sender, e) => 
{
    var filter = searchBar.Text;
    await Task.Run(() =>
    {
        Thread.Sleep(1000);
        if (filter == searchBar.Text)
            listview.QueryToDB(searchBar.Text);
    });
};
要保持视图模型更新,请将您的
isBusy
工作分配移动到
QueryToDB
中,因为此时视图模型确实很忙:

public void QueryToDB(string filter)
{
    this.BeginRefresh ();
    ViewModel.isBusy = true;

    // do your search

    ViewModel.isBusy = false;
    this.EndRefresh ();
}

一种方法是组合
异步任务。运行
取消令牌源

CancellationTokenSource cancellationTokenSource;

searchView.TextChanged += async (sender, e) => 
{
    if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
    cancellationTokenSource = new CancellationTokenSource();
    var cancellationToken = cancellationTokenSource.Token;

    var searchBar = (sender as SearchBar);
    if (searchBar != null)
    {
        string searchText = searchBar.Text;
        try
        {
            await Task.Delay(650, cancellationToken);

            if (cancellationToken.IsCancellationRequested) return;

            var searchResults = await Task.Run(() => 
            {
                return ViewModel.Search(searchText);
            });

            if (cancellationToken.IsCancellationRequested) return;

            ViewModel.YouItems.Repopulate(searchResults);
        }
        catch (OperationCanceledException)
        {
            // Expected
        }
        catch (Exception ex)
        {
            Logger.Error(ex);
        }
    }
};

谢谢当然我需要的,很好用。PS但当第一次引入(搜索栏中有一些值)时,我得到了一个异常(例如:在android设备上运行-调试显示“AndroidRunTimeThrowException”),但随后工作正常,出现了奇怪的小故障!没问题。我在Android应用程序上使用它,没有任何例外。确保CancellationTokenSource CancellationTokenSource;位于类的顶级。是的,它的全局变量!奇怪……非常奇怪:)。明天我再查。ThanksThread.Sleep-我想这是用于UI线程的,不是吗?谢谢你的建议!
CancellationTokenSource cancellationTokenSource;

searchView.TextChanged += async (sender, e) => 
{
    if (cancellationTokenSource != null) cancellationTokenSource.Cancel();
    cancellationTokenSource = new CancellationTokenSource();
    var cancellationToken = cancellationTokenSource.Token;

    var searchBar = (sender as SearchBar);
    if (searchBar != null)
    {
        string searchText = searchBar.Text;
        try
        {
            await Task.Delay(650, cancellationToken);

            if (cancellationToken.IsCancellationRequested) return;

            var searchResults = await Task.Run(() => 
            {
                return ViewModel.Search(searchText);
            });

            if (cancellationToken.IsCancellationRequested) return;

            ViewModel.YouItems.Repopulate(searchResults);
        }
        catch (OperationCanceledException)
        {
            // Expected
        }
        catch (Exception ex)
        {
            Logger.Error(ex);
        }
    }
};