Avatar of Adnan
Adnan
Flag for Norway asked on

I am getting error when i try to click button to start a thread....with SplashControl

Hi

i have a backup form in my application, where i have a restore button to restore the backup file. and i have a SplashControl  wich starts when i click the button with the thread....but i get this exception sayin that: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on....

i have yet not workes so much with threads...so can somone guide me with this issue?
Thread threadBackup;
        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(GetValue("restorequestion") + "\r\n", GetValue("restorebackup"), MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                ((MainForm)this.ParentForm).SetBlockOnApplication();
                 threadBackup = new Thread(new ThreadStart(test));
                 threadBackup.Start();
            }
        }
        private void test()
        {
            //if (MessageBox.Show(GetValue("restorequestion") + "\r\n", GetValue("restorebackup"), MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            //{
                
                int intDBVersion = AmEngine.Instance.GetDbVersion();
                try
                {
                    Backup.Backup bck = new Adra.Backup.Backup();
                    Cursor.Current = Cursors.WaitCursor;
 
                    LoadBackupFile();
                    if (!bck.RestoreEngagement(myOper, myDsBackup, m_intDbVersion))
                    {
                        MessageBox.Show(GetValue("restorefailed"), GetValue("restorebackup"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        //MessageBox.Show(GetValue("restorefinished"), GetValue("restorebackup"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                        DialogResult dialog = MessageBox.Show(GetValue("restorefinished"), GetValue("restorebackup"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (dialog == DialogResult.OK)
                        {
                            Restart();
                        }
                    }
                    //LoadBackupFile();
                    Cursor.Current = Cursors.Default;
                }
                catch (Exception exe)
                {
                    if (m_intDbVersion != intDBVersion)
                    {
                        MessageBox.Show(GetValue("mismatchbetweendbVersionandbackupversion"), GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(GetValue("selectBackupFile"), GetValue("restorebackup"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            //}
            //LoadBackupFile();
            Cursor.Current = Cursors.Default;
        }

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
Adnan

8/22/2022 - Mon
Adnan

ASKER
And here is my Loadbackufile function: every time i get the above exception i get error on loading backup file to....!
 public void LoadBackupFile()
        {
             
            try
            {
                myDsBackup = new DsAmDb();
                XmlTextReader xmlReader = new XmlTextReader(@txtBackupFilePath.Text + "/" + ((string)listBoxBakupFiles.SelectedItem));
                XmlDataDocument xmlDs = new XmlDataDocument(myDsBackup);
 
                xmlDs.Load(xmlReader);
                m_intDbVersion = Convert.ToInt32(xmlDs.DataSet.Tables["DbVersion"].Rows[0].ItemArray[0]);
                myDsBackup = (DsAmDb)xmlDs.DataSet.Copy();
                xmlReader.Close();
 
                if (myOper.Engagement_ID == (int)myDsBackup.Engagement.Rows[0]["Engagement_ID"])
                {
                    //lstBoxEngagement.DataSource = myDsBackup.Engagement;
                    //lstBoxClient.DataSource = myDsBackup.Client;
 
                    btnLoad.Enabled = true;
                    //btnRestoreClient.Enabled = true;
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    //TODO: Translate this one:
                    //MessageBox.Show("*Engasjement matcher ikke!");
                    MessageBox.Show(GetValue("engvsdbinconsistens"), GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    lstBoxEngagement.DataSource = null;
                    lstBoxClient.DataSource = null;
                    btnLoad.Enabled = false;
                    btnRestoreClient.Enabled = false;
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(GetValue("errorreadingbackup"), GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Default;
                return;
            }
        }

Open in new window

CuteBug

You are trying to access a WinForm control from a thread which is different from the thread in which it was created.

You have to use the Control.Invoke Method.

Check this link
http://www.shabdar.org/cross-thread-operation-not-valid.html

Adnan

ASKER
oki i read the link you sent me, but iam still strugling with figuring out how i can make method to invoke the thread....

can you show how it will be in my case....?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CuteBug

Define the SetControlPropertyValue method as it is described in the link.

Then use this method in your LoadBackupFile Method.


delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
 
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
    if (oControl.InvokeRequired)
    {
        SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
        oControl.Invoke(d, new object[] { oControl, propName, propValue });
    }
    else
    {
        Type t = oControl.GetType();
        PropertyInfo[] props = t.GetProperties();
        foreach (PropertyInfo p in props)
        {
            if (p.Name.ToUpper() == propName.ToUpper())
            {
                p.SetValue(oControl, propValue, null);
            }
        }
    }
}
 
 
public void LoadBackupFile()
        {
             
            try
            {
                myDsBackup = new DsAmDb();
                XmlTextReader xmlReader = new XmlTextReader(@txtBackupFilePath.Text + "/" + ((string)listBoxBakupFiles.SelectedItem));
                XmlDataDocument xmlDs = new XmlDataDocument(myDsBackup);
 
                xmlDs.Load(xmlReader);
                m_intDbVersion = Convert.ToInt32(xmlDs.DataSet.Tables["DbVersion"].Rows[0].ItemArray[0]);
                myDsBackup = (DsAmDb)xmlDs.DataSet.Copy();
                xmlReader.Close();
 
                if (myOper.Engagement_ID == (int)myDsBackup.Engagement.Rows[0]["Engagement_ID"])
                {
                    //lstBoxEngagement.DataSource = myDsBackup.Engagement;
                    //lstBoxClient.DataSource = myDsBackup.Client;
 
                    SetControlPropertyValue(btnLoad, "Enabled", true);
                    //btnRestoreClient.Enabled = true;
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    //TODO: Translate this one:
                    //MessageBox.Show("*Engasjement matcher ikke!");
                    MessageBox.Show(GetValue("engvsdbinconsistens"), GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    SetControlPropertyValue(lstBoxEngagement, "DataSource", null);
                    SetControlPropertyValue(lstBoxClient, "DataSource", null);
                    SetControlPropertyValue(btnLoad, "Enabled", false);
                    SetControlPropertyValue(btnRestoreClient, "Enabled", false);
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(GetValue("errorreadingbackup"), GetValue("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = Cursors.Default;
                return;
            }
        }

Open in new window

Adnan

ASKER
hmm oki, i tested as you showed, but i know i get the message saying that cant read tyhe file, it dies on line 31 in LoadBackupFile()........
ASKER CERTIFIED SOLUTION
CuteBug

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Adnan

ASKER
thanks m,an... ;)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.