私はクラス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;
}
}
ここで私が紛失しているものをお聞かせください。
[起動](http://stackoverflow.com/questions/14703698/invokedelegate)それ。 – DonBoitnott
を参照してくださいhttps://msdn.microsoft.com/en-us/library/ms171728.aspx –