BackgroundWorker-如何在工作完成时访问项目 [英] BackgroundWorker - How to access to item on work completion

查看:75
本文介绍了BackgroundWorker-如何在工作完成时访问项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

今天的问题是:是否可以访问在BackgroundWorker中完成运行的Item中的字段?

Today question is : Is it possible to access a field in the Item which complete run in BackgroundWorker?

我知道_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e)-它不提供对传递给RunWorkerAsync(对象pObjectToRun)的对象的访问.

I'm aware of _RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - it didn't provide access to object  passed to RunWorkerAsync(object pObjectToRun).

背景: 

我的T4模板集很少,并且期望模板的数量会增加.

I have few set's of T4 templates and expect a grow of amount of templates. 

在我使用模板的方式中,它们递归地调用,无限深.

In way in which I use templates they call one another recursively, unlimited deep.

每个T4模板实例都使用StringBuilder来存储生成的文本.

Each instance of T4 template use a StringBuilder to store generated text.

此StringBuilder相对昂贵.在默认版本的T4基类中,StringBuilder创建的时间占执行时间的30%以上.第二个限制是我无法在应用程序中实例化超过250个StringBuider -因此无限的深度非常有限.

This StringBuilder's are relatively expensive items. In the default version of T4 base class time of StringBuilder creation took more than 30% of execution time. Second limit is that I was not able to instantiate more than 250 StringBuider in application - so unlimited deep is very much limited. 

对于这种情况,几乎没有解决方案.至少我可以使用GC或进行拉取,并在T4不再使用它时将现有的StringBuilder返回给拉取.我用拉.

There are few solution for this situation. At least I can use GC or have a pull and return existing StringBuilder to the pull when it not longer used by T4. I use a pull.

很长时间以来,我一直使用单线程应用程序,对此感到满意.最近,我达到了实例化StringBuilder的极限(即使将它们返回到拉取中也是如此).这迫使我使用多线程.这允许解决实例化量的问题 使用ThreadStatic版本的StringBuilder.我什至可以允许线程实例化并删除StringBuilder,但我想让它们在pull时可以重用.

For long time I use a single thread application and was happy with that. Recently I reach a limit ot instantiated StringBuilder (even with returning them to the pull). This force me to use a multithreading. This allow to resolve problem with amount of instantiated StringBuilder by using ThreadStatic version. I even can allow thread to instantiate and drop StringBuilder, but I want to keep them reused over pull.

问题是:如何在线程完成t4运行时如何将SB返回到pull上?

Question Is : How to get return SB to the pull when thread complete t4 run?

推荐答案

嗯...我不会说async/await旨在替代BackgroundWorker.

Humm... I won't say async/await is designed to replace BackgroundWorker.

BackgroundWorker设计为在单独的线程中生成任务,并公开了ProgressChanged事件处理程序,使您可以更新UI元素 在工作时,而无需检查您是否在UI线程上.这比异步/等待提供的更多.

BackgroundWorker is designed to spawn task in separate thread, with ProgressChanged event handler exposed that let you update UI element while you're doing work without ever need to check whether you're on the UI thread. That's more than what async/await offers.

======

可以通过 RunWorkerCompletedEventArgs.Result 属性.请注意,释放它不会释放StringBuilder,因为后台工作程序本身可能仍然没有被释放,并保留对该StringBuilder实例的引用.尝试在其中创建StringBuilder 您的DoWork方法并仅返回结果字符串.

The result can be accessed through the RunWorkerCompletedEventArgs.Result property. Note that freeing it does not free the StringBuilder because the background worker itself may still not be disposed and holds reference to that StringBuilder instance. Try create StringBuilder within your DoWork method and return the resulting string only.


这篇关于BackgroundWorker-如何在工作完成时访问项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆