主窗体form1是Run()出来的,另一个窗体form2是在新线程中使用showdialog方法显示出来的,现在需要把form2的父窗体设置为form1,改如何实现?

解决方案 »

  1.   

    你怎么能在非工作线程showdialog呢?
      

  2.   

    我不太明白,我把代码贴出来吧。。
    ScanfAppFrm是想被设为子窗体的,frm是想被设为父窗体的,frm是Run()出来的,
    显示ScanfAppFrm时用了个这
      ThreadPool.QueueUserWorkItem(new WaitCallback(showScanfAppDialog), ScanfAppFrm);  //显示扫描申请窗体 private void showScanfAppDialog(object form)
            {
                ScanfAppFrm = (扫描申请)form;
                //ScanfAppFrm.MdiParent = frm;
                ScanfAppFrm.ShowDialog();                      
            }
    现在想把frm设置为ScanfAppFrm的父窗体,这个应该咋改呢?
      

  3.   

    请回到主线程创建并调用ShowDialog,也就是用Invoke或者BeginInvoke进行所有UI相关操作
      

  4.   

    Invoke或者BeginInvoke应该由主窗体调用还是非主窗体?
      

  5.   

    在主线程中调用showdialog后主线程不就阻塞在这块了么,我在showdialog后还需要和服务器沟通,若主线程调用showdialog那就不能及时收到服务器的消息了,除非把showdialog的窗体关闭,才能收到消息。。
      

  6.   


    Thread th = new Thread(new ThreadStart(ExportCost));
                th.SetApartmentState(ApartmentState.STA);
                th.Start();
    private void ExportCost()
            {
                SetExportBusinessNum();
                if (!string.IsNullOrEmpty(ExportBusinessNum))
                {
                    Query_Customs_CustomsDeclaration query_Customs_CustomsDeclaration = GetQuery_Customs_CustomsDeclaration();
                    query_Customs_CustomsDeclaration.BusinessNum = ExportBusinessNum;                DataTable dt = Customs_CustomsDeclarationManager.QDZYExportCostExcel(query_Customs_CustomsDeclaration);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "excel文件(*.xls)|*.xls";
                        sfd.FileName = "财务数据";
                        DataTable dtCopy = dt.Copy();
                        if (dt.Columns.Contains("是否审核")) dt.Columns.Remove("是否审核");
                        if (dt.Columns.Contains("是否导出")) dt.Columns.Remove("是否导出");
                        if (dt.Columns.Contains("Id")) dt.Columns.Remove("Id");
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            if (CommonMethodHelper.ExportExcel_NoScientificCalc(dt, sfd.FileName))
                            {
                                ShowInformationMsgBox(sfd.FileName + "导出成功!");
                                Customs_CustomsDeclarationManager.UpdateIsExport(query_Customs_CustomsDeclaration);
                                InvokeSearchCostData();
                            }
                        }
                    }
                    else
                    {
                        ShowInformationMsgBox("没有要导出数据!");
                    }
                }
                else
                {
                    ShowInformationMsgBox("请选择您要导出的数据!");
                }
            }