紧凑框架Func问题
我正在尝试在后台工作进程上做一些工作,当工作完成时,我想显示一个模式对话框。我的代码可以很好地用于简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译时收到的错误与 Compact 框架无关,而是与泛型使用不正确有关。正如其他人所说,类型 TResult 超出了类的范围,除非您在类本身上声明它。
如果我看看您想要实现的目标,我会说您可以通过将所有类型特定于该方法本地来获得您想要的东西。
进行如下更改:
您的“工作”现在由调用您的函数的匿名委托执行,并且函数的返回值保存在 ShowDialogWhile 函数的范围内。又好又简单的技巧。
只是为了在那里进行重构,您可以使用现有的方法来处理工作人员和对话逻辑:
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:
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:
您有一个带有 TResult 的通用方法,但也有一个使用该类型的字段。如果您在运行时需要该类型的字段,请将泛型定义从方法移至类。
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.
您的问题是
TResult
是在ShowDialogWhile
方法上定义的泛型,但您已将_workWithReturn
声明超出该范围。一种修复方法是将TResult
作为类声明的一部分。Your problem is that
TResult
is a generic defined on theShowDialogWhile
method, but you've declared your_workWithReturn
out of that scope. One fix would be to haveTResult
as part of the class declaration instead.