简体   繁体   English

搜索文档时如何在WPF中使用进度条(圆形)?

[英]How to use progress bar(circular) in wpf while searching documents?

I want to use progress bar - so users should see that it is on progress while I am searching some documents through full text search. 我想使用进度条-所以当我通过全文搜索搜索某些文档时,用户应该看到进度条正在进行中。 Assume, I have 50 000 documents. 假设我有5万份文件。 I want to find documents which have word "building construction" using Full Text Search. 我想使用全文搜索查找包含单词“ building construction”的文档。 It is working fine for now. 目前工作正常。 But when I search I have to wait 5-15 seconds. 但是,当我搜索时,我必须等待5-15秒。 I want to use load balance in this period of search. 我想在此搜索期间使用负载平衡。 Is it possible? 可能吗? How should I use it? 我应该如何使用它? Using multi-threading or something else? 使用多线程还是其他? How can I understand if 1st thread finished working? 我如何理解第一线程是否完成工作? Please provide me with some clear ideas and codes. 请为我提供一些清晰的想法和代码。

Assuming you're talking about the wait cursor here, or do you mean a progress bar in your window? 假设您在这里谈论的是等待光标,还是在窗口中表示进度条? For the wait cursor you need this either in your window or on whichever element you want the cursor to appear over 对于等待光标,您需要在窗口中或希望光标出现在任何元素上的位置

<Window.Style>
    <Style TargetType="{x:Type Window}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsBusy}" Value="True">
                <Setter Property="Cursor" Value="Wait" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Style>

This can then be activated by changing the IsBusy property to true either on your ViewModel or in code behind if you're not doing MVVM 然后,可以通过在ViewModel上或在未执行MVVM的代码中将IsBusy属性更改为true来激活此功能

To tackle the rest of your post, are you looking for a way to push your sql call to a worker so that it won't lock up the UI? 为了解决您的其余文章,您是否正在寻找一种方法来将sql调用推送给工作程序,以使其不会锁定UI?

Assuming that this is what you wanted, here goes 假设这就是您想要的,请继续

public class SearchViewModel : INotifyPropertyChanged
{
    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;
            RaisePropertyChanged("IsBusy");
        }
    }

    public void Search(string searchPhrase)
    {
        IsBusy = true;
        Task.Factory
            .StartNew(p =>
            {
                // This is where you do your database thing and return your results
            }, searchPhrase)
            .ContinueWith(t =>
            {
                // And this is where you use the returned results in t.Result
                // don't forget to check for errors :)
                IsBusy = false;
            }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

or if you prefer the async approach 或者如果您更喜欢异步方法

public class SearchViewModel : INotifyPropertyChanged
{
    public async Task Search(string searchPhrase)
    {
        IsBusy = true;
        var results = await DoDatabaseSearch(searchPhrase);
        // do stuff with results
        IsBusy = false;
    }

    public async Task<IEnumerable<SearchResult>> DoDatabaseSearch(string searchPhrase)
    {
        // This is where you would do your search
    }
}

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

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