在循环中使用yield关键字来更新C#中的进度条
我可以使用yield关键字更新循环的进度吗?
foreach(something obj in somelist)
{
yield return obj;
}
我知道我可以做到这一点,但如何使用这些值来更新进度条?
谢谢。
Can I update a progress of a loop using the yield keyword?
foreach(something obj in somelist)
{
yield return obj;
}
I know I can do this, but how do I use the values to update a progress bar ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的评论,您是
这是我在最近的项目中使用的解决方案。示例代码适用于下载文件(即 pdf、jpg 等),但也可以应用于任何可流式传输的数据。
第一个重要注意事项是,您的 WCF 绑定选择将决定这是否可行。如果您使用 basicHttpBinding 或 netTcpBinding,那么该方法应该可以工作,但是如果您使用 WsHttpBinding,那么您就不走运了。
示例绑定可能类似于:
需要注意的重要一点是传输模式“Streamed”。
要报告 WCF 服务和 WinForm 之间的数据传输进度,您需要确保 WCF 服务使用的是 MessageContracts 而不是 DataContracts。原因是 MessageContract 允许您将单个流作为响应正文。例如,您可能有一个请求/响应消息契约,如下所示:
请求...
响应...
现在一切都很好,但是您需要有一种方法来报告下载(或上传)进度给客户。不幸的是,.NET 没有提供一种开箱即用的好方法来执行此操作...正如其他人提到的,您正在寻找事件...具体来说,是“ObservableStream”的实现。再说一遍,这是我喜欢使用的实现...
向订阅者报告读取或写入操作的事件参数...
当然还有流实现本身...
最后,您必须使用以下数据 :你的 Windows 客户端...
像平常一样简单地调用你的服务方法(ServiceChannelFactory 是 ChannelFactory 实现的包装器...不用担心)。
然后连接到您的可观察流以更新用户界面(显然必须在后台线程上工作,否则您将阻塞用户界面)
编辑
哦,服务方法本身非常简单...忘记了将其包括在内;您所要做的就是返回您的流:
Based on your comment that you are
Here is the solution that I have used on a recent project. The sample code is geared towards downloading a file (i.e., pdf, jpg, etc), but can be applied to any streamable data.
The first important note, your WCF binding choice will determine whether or not this is even possible. If you are using basicHttpBinding or netTcpBinding, then the approach should work, however if you are using WsHttpBinding, you are out of luck.
A sample binding may look something like:
The important thing to note is the transfer mode "Streamed".
To report progress on the transfer of data between a WCF service and a WinForm you need to ensure that your WCF service is using MessageContracts over DataContracts. The reason is that a MessageContract allows you to have a single stream as the response body. For instance, you may have a request/response message contract as follows:
The request...
The response...
Now this is all well and good, but you need to have a way to report that download (or upload) progress back to the client. Unfortunately, .NET doesn't provide a nice way out of the box to do this... as mentioned by others, you are looking for events... specifically, the implementation of an "ObservableStream". Again, here is the implementation that I like to use....
The event args to report a read or write operation to a subscriber...
And of course the stream implementation itself...
Finally, you have to consume the data in your windows client...
Simple call your service method as you normally would (ServiceChannelFactory is a wrapper around a ChannelFactory implementation... don't worry about that).
and then hookup to your observable stream to update the ui (obviously must be working on a background thread, otherwise you will just block the ui)
EDIT
Oh, the service method itself is pretty simple... forgot to include it; all you have to do is return your stream:
正如用户 digEmAll 所评论的,您更有可能想为此使用事件。
但是,如果您想要一个更新
yield return
进度的解决方案,您可能希望在迭代集合之前获取元素的数量。然后,对于返回的每个元素,您可以将进度条更新一。如果您详细阐述一下您的问题,也许会很有用 - 您到底想实现什么目标。
As commented by user digEmAll, it is more likely that you want to use an event for this.
But if you want a solution that updates progress on
yield return
, you will probably want to get the number of elements before iterating through your collection. Then, for each element you get returned, you can update your progressbar by one.Perhaps it would be useful if you elaborated your question a bit - what exactly are you trying to accomplish.
为什么不从循环内部更新进度条?
Why not update the progress bar from inside the loop?