簡體   English   中英

C#創建/關閉多個表單(線程)-Crystal Report Viewer

[英]C# Creating/Closing Multiple Forms (Threads) - Crystal Report Viewer

我已經解決了6個小時的問題,但仍然無法解決。 我希望你能幫助我。

我正在研究一個與Crystal-Report-Engine有關的項目(SAP,.NET Framework 4.0中的最新版本,32位C#應用程序)。 基本上,它像這樣工作:

我正在打開我的主窗體,該窗體動態地向我顯示特定報告文件(.rpt)的所有參數。 輸入我的參數值后,應用程序將進行一些語法檢查,並將參數傳遞給ReportDocument。

問題:現在,我正在創建一個新的ReportForm(我自己的類),該類是Windows.Form中帶有CrystalReportViewer的派生類。 ReportForm使用傳遞的參數將crystalReportViewer-Object的ReportSource設置為先前創建的ReportDocument。 ->顯示表單時,正在加載Crystal Report Viewer。

問題1:當Crystal Report Viewer加載報表時,我仍然希望能夠訪問我的主窗體(有時可能需要5到6分鍾的時間)-例如,在上一個報表仍在運行時創建其他報表加載。 僅當我為該新表單創建線程時才有可能-但這(有時)會導致ContextSwitchDeadlock,因為我必須創建一個新線程,這將創建一個新的GUI控件(ReportForm)。

問題2:例如,如果我不創建新線程並使用myNewReportForm.Show()啟動新ReportForm,則必須等到加載報表后才能訪問主窗體(直到報告已完全加載)。 下一個問題是我無法關閉/退出/殺死我的應用程序,因為線程仍在加載報告-我還必須等待,直到加載報告為止。 但是我希望能夠在需要時關閉應用程序(主窗體和所有ReportForms)

基本上看起來像這樣(簡短版本):

/* pass parameters to reportDocument */
ReportForm reportForm = new ReportForm(reportDocument);
reportForm.Show();

在ReportForm中:

crystalReportViewer.ReportSource = this.reportDocument;

你有什么想法?

最佳,Cylence

終於解決了問題。 所以這是我的解決方案:

對於為Crystal Report Viewer創建新線程的每個人:當心!

解決問題-SAP

這是一個常規解決方案:

/* In Main Form */

            /*...*/

     ThreadStart threadStart = delegate() { reportThreadFunction(reportDocument); };
     Thread reportThread = new Thread(threadStart);

     /* Setting the ApartmentState to STA is very important! */
     reportThread.SetApartmentState(ApartmentState.STA);
     reportThread.Start();
}

         /* ... */

private void reportThreadFunction(ReportDocument reportDocument)
{
     ReportThread rt = new ReportThread(reportDocument);
     newReportForm = rt.Run();
     Application.Run(newReportForm);
     newReportForm.Show();
}


/* Class ReportThread */
public class ReportThread
{
    ReportDocument reportDocument;
    CrystalReportViewer crv;
    Template template;

    public ReportThread(ReportDocument reportDocument)
    {
        this.reportDocument = reportDocument;
    }

    public ReportForm Run()
    {
        ReportForm rf = new ReportForm(reportDocument);
        return rf;
    }
}

最佳,Cylence

謝謝! 這為我解決!

為了簡化上面的代碼:

 /* In Main Form */

 /*...*/
 ThreadStart threadStart = delegate({reportThreadFunction(reportDocument);};
 Thread reportThread = new Thread(threadStart);

 /* Setting the ApartmentState to STA is very important! */
 reportThread.SetApartmentState(ApartmentState.STA);
 reportThread.Start();
 }

 private void reportThreadFunction(ReportDocument reportDocument)
 {
     //Create and Start the form
     Application.Run(new ReportThread(reportDocument));
 } 


 /* Class ReportThread */
 /* A form whith a CrystalReportViewer inside*/
 public partial class ReportThread : Form
 {
     public CrystalReportViewer crv; //initiated in partial class off form

     public ReportThread(ReportDocument reportDocument)
     {
        InitializeComponent();
        CrystalReportViewer.ReportSource = reportDocument;
     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM