Skip to main content
 首页 » 编程设计

c#之GUI 在 Dispatcher.BeginInvoke 或 Task.StartNew 时卡住

2024年02月20日17kenshinobiy

注意:取决于 this quetion

我有一个像这样的 View 模型:

public class ViewModel { 
    private readonly IPersonService _personService; 
    private readonly ObservableCollection<SearchPersonModel> _foundedList; 
    private readonly DispatcherTimer _timer; 
    private readonly Dispatcher _dispatcher; 
    private CancellationTokenSource _tokenSource; 
 
    public SearchPatientViewModel(IPersonService personService) { 
        _personService = personService; 
        _foundedList = new ObservableCollection<SearchPersonModel>(); 
        _dispatcher = (/*CurrentApplication*/).Dispatcher; 
        _timer = new DispatcherTimer( 
            TimeSpan.FromMilliseconds(1000), 
            DispatcherPriority.Normal, 
            TimerCallBack, 
            _dispatcher); 
        _tokenSource = new CancellationTokenSource(); 
    } 
 
    public string Term { 
        get { return _term; } 
        set { 
            // implementing INotifyPropertyChanged 
            if(_term== value) 
                return; 
            _term= value; 
            OnPropertyChanged(() => Term); 
            tokenSource.Cancel(); // canceling prev search query 
            _timer.Stop(); // stop the timer to reset it 
            // start it again to do a search query if user change not the term for 1000ms 
            _timer.Start();  
        } 
    } 
 
    private void TimerCallBack(object sender, EventArgs e) { 
        _timer.Stop(); 
        _tokenSource = new CancellationTokenSource(); 
        var task = Task<IEnumerable<SearchPersonModel>>.Factory 
            .StartNew(Search, _tokenSource.Token); 
        _dispatcher.BeginInvoke((Action)(() => { 
            _foundedList.Clear(); 
            foreach(var item in task.Result) 
                _foundedList.Add(item); 
        }), DispatcherPriority.Background); 
    } 
 
    private IEnumerable<SearchPersonModel> Search() { 
        return _personService.DoSearch(this.Term); 
    } 
 
} 

在 IPersonService 实现中,我这样做:

public class PersonService : IPersonService { 
    public IEnumerable<SearchPersonModel> DoSearch(string term){ 
        System.Threading.Thread.Sleep(10000); 
        return some-search-result; 
    } 
} 

但是,我希望在执行搜索查询时,GUI 是免费的。但是它卡住了!你知道我的错误在哪里吗?

请您参考如下方法:

问题是评估 task.Result 将阻塞,直到查询完成。

最简单的选择可能是让 Search 方法在最后执行 _dispatcher.BeginInvoke 调用。

另一个选项(使用 C# 5 会变得更容易)是向任务添加延续,以便在任务完成时更新 UI。目前你会使用 Task.ContinueWith;对于 C# 5,您将使用 async 和 await。