0

I have a parent form AddEmployee which call a class Worker and class opens another(child) form InformationFetching. I want to set AddEmployee as parent form of InformationFetching but I am getting error of Cross-Thread.

 //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;
    }
}

Can anybody please suggest me what I am missing here?

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63

1 Answers1

1

According to comments, I would suggest like this:

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);
}
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116555/discussion-between-nanji-mange-and-x). – Nanji Mange Jul 06 '16 at 09:47