Help For "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."
-
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
-
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
I read it! I hope you feel better now! Just as a comment I will use 'using' at the top of the file, so as to avoid long.class.name, will use shorter variable name with upper case at the top of each word. That will increase readability which, in turn, will make the code easier to: understand / debug / maintain! also I will use the other using to dispose of disposable resouce as is: using(SqlConnection cnA = new SqlConnection("ConnectionString...")) { ...... } this is a lighter syntax than try {} finally {} which makes sure that disposable resource are disposed even in case of exception or misplaced return statement!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
-
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
Read from one of my favorite articles by Sacha Barber about threading Beginners Guide To Threading In .NET Part 1 of n[^] Beginners Guide To Threading In .NET Part 2 of n[^] Beginners Guide To Threading In .NET Part 3 of n[^] Beginners Guide To Threading In .NET Part 4 of n[^] Beginners Guide To Threading In .NET Part 5 of n[^] hope it helps
dhaim ing ngarso sung tulodho, ing madyo mangun karso, tut wuri handayani. "Ki Hajar Dewantoro" in the front line gave a lead, in the middle line build goodwill, in the behind give power support
-
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
hi add this line to main function to disable CrossThread checks: Control.CheckForIllegalCrossThreadCalls = false; like this:
static class Program { /// <summary> /// The main entry point for the application. /// </summary> \[STAThread\] static void Main() { Control.CheckForIllegalCrossThreadCalls = false; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
-
hi add this line to main function to disable CrossThread checks: Control.CheckForIllegalCrossThreadCalls = false; like this:
static class Program { /// <summary> /// The main entry point for the application. /// </summary> \[STAThread\] static void Main() { Control.CheckForIllegalCrossThreadCalls = false; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
And wait for the app to die at some point? These are 'illegal' cross thread calls and the warning is thown for a reason! Removing the check just stops that, but doesn't solve the underlying problem. It's easy to fix so that the control is invoked when necessary.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan
Check out Luc's article here[^]
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
hi, MyFrinds. Please Help me! Read my Source Code : { ... System.Threading.Thread lengthyprocessthreadanbar = new System.Threading.Thread(new System.Threading.ThreadStart(RunLengthyProcessanbar)); lengthyprocessthreadanbar.IsBackground = true; lengthyprocessthreadanbar.Start(); _pleasewaitdlg = new Anbar_PleaseWait(); _pleasewaitdlg.ShowDialog(); ... } void RunLengthyProcessanbar() { SetupForm(); _pleasewaitdlg.shouldclosenow = true; } private void SetupForm() { SqlConnection cnA = new SqlConnection("ConnectionString..."); SqlCommand cmA=new SqlCommand (); cmA.Connection = cnA; cmA.CommandText = "Command Text ..."; int RowActive = 0; if (cnA.State != ConnectionState.Open) cnA.Open(); SqlDataReader readA = cmA.ExecuteReader(); while (readA.Read()) { dgvAnbar.Rows.Add(1); dgvAnbar.Rows[RowActive].Cells[0].Value = readA[0].ToString(); } cnA.Close(); } www.ggsoft.ir mohsen5459@gmail.com Thanks.
Welcome Mohsen Sabzaliyan