我的主菜单上有一个名为“Active Reports”的菜单项。当用户选择要打印的报告时,我会在单独的线程中关闭水晶报告查看器。用户可以打开多个报表查看器,我想跟踪它们。主要是此菜单列表将允许用户单击报告,如果报告在处理报告数据时运行时间过长,我可以在该线程上触发中断以取消正在运行的报告。当每个报告查看器关闭时,我将从菜单列表中删除它。我有一个名为ReportThreadClass的类,它跟踪当前正在运行的每个报告及其所属的线程。在线程中关闭报告后,我想更新“活动报告”菜单项,并将正在运行的报告的名称添加到活动报告列表中。这是我创建线程和ReportThreadClass的代码。
public class ReportThreadClass
{
public Thread Thread { get; set; }
public bool AbortFlag { get; set; }
public string ReportName { get; set; }
public int ReportId { get; set; }
}
public void RunReport(UAReport report)
{
Thread staThread = new Thread(r => ReportDocumentThread((UAReport) r));
staThread.SetApartmentState(ApartmentState.MTA);
staThread.Start(report);
}
public void ReportDocumentThread(UAReport report)
{
var service = new UAReportService(report); //class that creates the report document that will be passed to the report viewer.
ReportDocument reportDocument = service.CreateReportDocument();
string reportDesc = report.ReportDesc;
object args = new object[2] {reportDocument, reportDesc};
var staThread = new Thread(new ParameterizedThreadStart(ReportViewerThread));
int nextReportId;
if (!Program.MainForm.ReportThreads.Any())
nextReportId = 0;
else
{
nextReportId = Program.MainForm.ReportThreads.Count() + 1;
}
ReportThreadClass reportThread = new ReportThreadClass();
reportThread.Thread = staThread;
reportThread.ReportName = reportDesc;
reportThread.AbortFlag = false;
reportThread.ReportId = nextReportId;
Program.MainForm.ReportThreads.Add(reportThread); //list of all active reports running.
Program.MainForm.BuildActiveReportsMenu(); //how do i do this from thread.
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start(args);
staThread.Join(100);
}
public void ReportViewerThread(object args)
{
Array argArray = new object[3];
argArray = (Array) args;
ReportDocument reportDocument = (ReportDocument) argArray.GetValue(0);
string reportDesc = (string) argArray.GetValue(1);
try
{
ReportViewerForm form = new ReportViewerForm(reportDocument, reportDesc);
form.BringToFront();
form.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
那么我在线程中发布新报告后如何构建“活动报告菜单”。基本上我将遍历ReportThreads列表并将活动报告添加到“Active Reports”菜单项。 ReportThreads列表在主窗口中声明和维护。我可以通过Program.MainWindow访问它。
答案 0 :(得分:0)
如果我正确理解您,您想要从另一个主题更改您的表单。为此,您可以使用Invoke方法,如下所示:
Program.MainForm.Invoke(() => Program.MainForm.BuildActiveReportsMenu());