从WPF的后台线程返回bool值

本文关键字:返回 bool 线程 后台 WPF | 更新日期: 2023-09-27 18:01:58

public bool function()
{
    bool doesExist = false;          
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (o, ea) =>
        {
            // some work done here
        };
    worker.RunWorkerCompleted += (o, ea) =>
        {
            //somw logic here
            return doesExist;                               
        };
} 

我想要doesExist值作为函数的返回值,但我得到智能感知错误

system.componentmodel。Runworkercompletedeventhandler只返回void, return关键字不能后跟对象表达式

为什么我得到这个错误,如何返回bool值?

从WPF的后台线程返回bool值

public bool function()
{
   bool doesExist = false;          
   BackgroundWorker worker = new BackgroundWorker();
   worker.DoWork += (o, ea) =>
            {
                 // do some work
                 ea.Result = true; // set this as true if all goes well!
            };
    worker.RunWorkerCompleted += (o, ea) =>
        {
              // since the boolean value is calculated here &
              // RunWorkerCompleted returns void
              // you can create a method with boolean parameter that handle the result.
              Another_Way_To_Return_Boolean(doesExist)

        };
} 

private void Another_Way_To_Return_Boolean(bool result)
{
    if (result)
    {
    }
}