Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help For "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."

Help For "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."

Scheduled Pinned Locked Moved C#
comhelp
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohsen Sabzaliyan
    wrote on last edited by
    #1

    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

    S R M H D 6 Replies Last reply
    0
    • M 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

      S Offline
      S Offline
      Super Lloyd
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M 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

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        See this[^] post. /ravi

        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

        1 Reply Last reply
        0
        • M 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

          M Offline
          M Offline
          Mbah Dhaim
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M 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

            H Offline
            H Offline
            hamed vojdani
            wrote on last edited by
            #5

            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());
                }
            }
            
            D 1 Reply Last reply
            0
            • H hamed vojdani

              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());
                  }
              }
              
              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              • M 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

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                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)

                1 Reply Last reply
                0
                • M 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

                  D Offline
                  D Offline
                  Derek R White
                  wrote on last edited by
                  #8

                  http://javaflowswithin.blogspot.com/2010/03/c-threadsafecontrol.html

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups