希望你一切都好。
我遇到了 BeginInvoke 的一个奇怪问题,我真的很需要你的帮助!
我有一个类 Reporting ,其中包含多个 Reporting 类型的实例
Class Reporting : UserControl
{
//Reports are inherited from UserControl
Report _report1;
Report _report2;
Report _report3;
//Instanciate and return the Report corresponding to the passed ID (The report is
//instanciated and created only if it's null)
public Report GetReport(int reportId);
public delegate void GenerateReportAsImageDelegate(int reportId,string path)
//Generate the report and save it as image
public void GenerateReportAsImage(int reportId,string path)
{
if(InvokeRequired)
{
BeginInvoke(new GenerateReportAsImageDelegate(GenerateReportAsImage),new object[]{reportId,path});
}
else
{
//... Generating the report etc..
}
}
....
}
它是一个显示在表单中的用户控件,同样的用户控件也用于 Windows 服务,以每分钟生成报告(并保存为图像)。
为了每分钟生成一份报告,我使用了 System.Threading.Timer。
这是我的班级在服务中生成报告的样子:
class ReportServiceClass
{
Reporting _reportingObject;
System.Threading.Timer _timer;
Public ReportingServiceClass(int reportId)
{
_timer = new Timer(new TimerCallback(this.CreateReport), reportId, 0, 1000)
}
private void CreateReport(Object stateInfo)
{
int reportId = Convert.ToInt32(stateInfo);
//To ensure that the _reportingObject is created on the same thread as the report
if(_reportingObject == null)
_reportingObject = new _reportingObject();
_reportingObject.GenerateReportAsImage(reportId,@"c:\reports\report.PNG")
}
}
几乎一切都运行良好.. 除了有时 CreateReport 是在 ThreadPool 的另一个线程中执行的。因此,当我对报告及其组件(已在另一个线程中创建)执行一些操作时,InvokeRequired 设置为 true,这非常明显......但是 BeginInvoke 不执行任何操作!这几乎就像创建报告的线程不再存在......
你们对如何避免这个问题有任何想法吗?
我已经面临这个问题一个星期了,我已经用谷歌搜索和 stackoverflowed 。但什么都没有!
非常感谢 !