2016-06-29 7 views
0

私はクラスWorkerを呼び出し、クラスは別の(子)フォームInformationFetchingを開く親フォームAddEmployeeを持っています。私はInformationFetchingの親フォームとしてAddEmployeeを設定したいが、クロススレッドのエラーが出ている。親フォームをC#でクラスから開いたフォームに設定する方法は?

//Parent Form 
public partial class AddNewAccount : DevExpress.XtraEditors.XtraForm 
{ 
    Thread thrd; 
    private void brnContinue_Click(object sender, EventArgs e) 
    { 
     Worker workerObject = new Worker(this); 
     //ThreadStart ts = new ThreadStart(workerObject.DoWork); 
     //thrd = new Thread(ts); 

     Thread thrd = new Thread(() => 
     { 
      //workerObject.DoWork(); // CrossThreadException 
      Invoke(new MethodInvoker(workerObject.DoWork)); // OK 
     }); 

     //some code here 
     addNewAccount();  
    } 

    private void addNewAccount() 
    { 
     //parameter define here and pass in verifyDetails 
     thrd.Start(); 
     validatorClass objClass = new validatorClass(); 
     Bool isSuccess = false; 
     isSuccess = objClass.verifyDetails(parameters); //it will verify all the details. May take upto a 30 seconds 
    } 
} 

//worker class 
public class Worker 
{ 
    Form parentForm 
    InformationFetching frmChild; 
    Bool isTestCall = false;1 
    public Worker(Form prntForm) 
    { 
     // TODO: Complete member initialization 
     parentForm = prntForm; 
    } 
    public void DoWork() 
    { 
     //child form 
     if(isTestCall) 
      InformationFetching frmChild = new InformationFetching(0) 
     else 
      InformationFetching frmChild = new InformationFetching(1) //1 is when small verification 
     //getting error of cross-thread 
     frmChild.MdiParent = parentForm; 
     frmChild.StartPosition = FormStartPosition.CenterParent; 
     frmChild.ShowDialog(); 
    } 
} 

//validator class 
public class validatorClass 
{ 
    // verify all the details one by one and fetch in InformationFetching form 
    internal void verifyDetails() 
    { 
     //phase-1 verification 
     AccountSyncform().showInfo("Information"); 

     //phase-2 verification 
     AccountSyncform().showInfo("Information"); 

     //phase-3 verification 
     AccountSyncform().showInfo("Information"); 

     //phase-4 verification 
     AccountSyncform().showInfo("Information"); 

     //phase-5 verification 
     AccountSyncform().showInfo("Information"); 
    } 

    public InformationFetching AccountSyncform() 
    { 
     InformationFetching mForm = null; 
     try 
     { 
      foreach (System.Windows.Forms.Form f in System.Windows.Forms.Application.OpenForms) 
       if (f.Name == "InformationFetching") 
       { mForm = (InformationFetching)f; break; } 
     } 
     catch (Exception ex) 
     { 
      MainTainErrorLog.addGeneralErrorToLog(ex.Message); 
     } 
     return mForm; 
    } 
} 

ここで私が紛失しているものをお聞かせください。

+0

[起動](http://stackoverflow.com/questions/14703698/invokedelegate)それ。 – DonBoitnott

+0

を参照してくださいhttps://msdn.microsoft.com/en-us/library/ms171728.aspx –

答えて

1

のコメントによると、私はこのようなことをお勧め:

private void brnContinue_Click(object sender, EventArgs e) 
{ 
    Worker workerObject = new Worker(this); 
    Thread thrd = new Thread(() => 
    { 
    Invoke(new MethodInvoker(delegate 
    { 
     addNewAccount(); // Do something heavy for 30s 
     workerObject.DoWork(); 
    })); 
    }); 
    thrd.Start(); 
} 

private void addNewAccount() 
{ 
    validatorClass objClass = new validatorClass(); 
    bool isSuccess = false; 
    isSuccess = objClass.verifyDetails(parameters); 
} 
+0

私たちは[チャットでこのディスカッションを続けましょう](http://chat.stackoverflow.com/rooms/116555/discussion-between- nanji-mange-and-x)である。 –

関連する問題