紧凑框架Func问题

发布于 2024-11-15 22:46:33 字数 2095 浏览 6 评论 0原文

我正在尝试在后台工作进程上做一些工作,当工作完成时,我想显示一个模式对话框。我的代码可以很好地用于简单的 Action 委托,但希望能够传入 Func 委托并获取返回类型。 到目前为止我所拥有的是一个带有方法的表单

    public void ShowDialogWhile(Action work)
    {
        _work = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();
    }

我接下来想要工作的是到目前为止

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }

我还没有任何运气,因为在声明我的 _workWithReturn 类型时我一直跌倒

        Func<TResult> _workWithReturn;

有人有什么想法吗?

这是完整的代码。

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    BackgroundWorker _worker = new BackgroundWorker();
    Action _work;
    Func<TResult> _workWithReturn;
    public object Result { get; private set; }


    public AsyncWaitDialog()
    {
        InitializeComponent();
        _worker.DoWork += HandleDoWorkEvent;
        _worker.RunWorkerCompleted += HandleWorkerCompleted;
    }


    public void ShowDialogWhile(Action work)
    {
        _work = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();
    }

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }


    private void HandleDoWorkEvent(object sender, EventArgs e)
    {
        try
        {
            if (_work != null)
            {
                _work();
            }
            if (_workWithReturn != null)
            {
                Result = _workWithReturn();
            }

        }
        catch (Exception)
        {
            this.Close();
            throw;
        }
    }

    private void HandleWorkerCompleted(object sender, EventArgs e)
    {
        this.Close();
    }

}

I am trying to do some work on a backgroundworker process and while the work is done I want to display a modal dialog. I have got the code working nicely for a simple Action delegate but want to be able to pass in a Func delegate and get a return type.
What I have so far is a Form with a method

    public void ShowDialogWhile(Action work)
    {
        _work = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();
    }

What I'd like to get working next is something like

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }

I haven't had any luck so far because I keep falling over when declaring the type of my _workWithReturn

        Func<TResult> _workWithReturn;

Does anyone have any thoughts?

Here is the full code.

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    BackgroundWorker _worker = new BackgroundWorker();
    Action _work;
    Func<TResult> _workWithReturn;
    public object Result { get; private set; }


    public AsyncWaitDialog()
    {
        InitializeComponent();
        _worker.DoWork += HandleDoWorkEvent;
        _worker.RunWorkerCompleted += HandleWorkerCompleted;
    }


    public void ShowDialogWhile(Action work)
    {
        _work = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();
    }

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }


    private void HandleDoWorkEvent(object sender, EventArgs e)
    {
        try
        {
            if (_work != null)
            {
                _work();
            }
            if (_workWithReturn != null)
            {
                Result = _workWithReturn();
            }

        }
        catch (Exception)
        {
            this.Close();
            throw;
        }
    }

    private void HandleWorkerCompleted(object sender, EventArgs e)
    {
        this.Close();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末が日狂欢 2024-11-22 22:46:33

编译时收到的错误与 Compact 框架无关,而是与泛型使用不正确有关。正如其他人所说,类型 TResult 超出了类的范围,除非您在类本身上声明它。

如果我看看您想要实现的目标,我会说您可以通过将所有类型特定于该方法本地来获得您想要的东西。
进行如下更改:

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    ...
    //Func<TResult> _workWithReturn; REMOVE THIS LINE
    ...

    //Change your function to be this:
    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        var result = default(TResult);
        _work = () =>
        {
            result = work();
        };
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return result;
    }
}

您的“工作”现在由调用您的函数的匿名委托执行,并且函数的返回值保存在 ShowDialogWhile 函数的范围内。又好又简单的技巧。

只是为了在那里进行重构,您可以使用现有的方法来处理工作人员和对话逻辑:

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        var result = default(TResult);
        ShowDialogWhile(() =>
        {
            result = work();
        });
        return result;
    }

The errors you are receiving when compiling are not anything to do with Compact framework, but rather the incorrect use of Generics. As others have said, the type TResult is out of scope in your class unless you declare it on the class itself.

If I look at what you are trying to achieve I would say that you could get what you are wanting through keeping everything type specific local to the method.
Make changes as follows:

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    ...
    //Func<TResult> _workWithReturn; REMOVE THIS LINE
    ...

    //Change your function to be this:
    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        var result = default(TResult);
        _work = () =>
        {
            result = work();
        };
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return result;
    }
}

Your 'work' is now being performed by an anonymous delegate which calls your function and the return of the function is held within the scope of the ShowDialogWhile function. Nice and easy trick.

Just to throw a refactoring in there, you could use your existing method to handle the worker and dialog logic:

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        var result = default(TResult);
        ShowDialogWhile(() =>
        {
            result = work();
        });
        return result;
    }
我不吻晚风 2024-11-22 22:46:33

您有一个带有 TResult 的通用方法,但也有一个使用该类型的字段。如果您在运行时需要该类型的字段,请将泛型定义从方法移至类。

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    ...
    Func<TResult> _workWithReturn; //TRESULT IS NOT IN SCOPE
    ...

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        //TResult **IS** in scope here
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }


// possible solution, move the generic definition of TResult to the class definition

public partial class AsyncWaitDialog<TResult> : Form, IAsyncDialog { ... }

You have a generic method with TResult, but you also have a field using the type. Move the generic definition from the method to the class if you need a field of that type at runtime.

public partial class AsyncWaitDialog : Form, IAsyncDialog
{
    ...
    Func<TResult> _workWithReturn; //TRESULT IS NOT IN SCOPE
    ...

    public TResult ShowDialogWhile<TResult>(Func<TResult> work)
    {
        //TResult **IS** in scope here
        _workWithReturn = work;
        _worker.RunWorkerAsync();
        this.CenterForm();
        this.ShowDialog();

        return (TResult)Result;
    }


// possible solution, move the generic definition of TResult to the class definition

public partial class AsyncWaitDialog<TResult> : Form, IAsyncDialog { ... }
亣腦蒛氧 2024-11-22 22:46:33

您的问题是 TResult 是在 ShowDialogWhile 方法上定义的泛型,但您已将 _workWithReturn 声明超出该范围。一种修复方法是将 TResult 作为类声明的一部分。

Your problem is that TResult is a generic defined on the ShowDialogWhile method, but you've declared your _workWithReturn out of that scope. One fix would be to have TResult as part of the class declaration instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文