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. Cross threaded operations

Cross threaded operations

Scheduled Pinned Locked Moved C#
help
4 Posts 3 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
    MAW30
    wrote on last edited by
    #1

    I have recently started to multithread my program and everything seems to work fine with the exception of a form I use to track the progress of data being transfered. The Progress form is on the main thread, I send data to it as my program is processing it. The problem is that is does not appear on the form, I have used various methods to Invoke the thread but it just goes into a loop and never stops or just goes into oblivian. Some of the methods are as follows or similar:

        public void UpdateStartTime(String text)
        {
            if (textBoxStartTime.InvokeRequired)
            {
                UpdateStartTimeCallback updateStartTimeCallback = new UpdateStartTimeCallback(UpdateStartTime);
                Invoke(updateStartTimeCallback, new object\[\] { Text });
            }
            else
            {
                textBoxStartTime.Text = text; 
            }
        }
    

    and

        public void UpdateStartTime(TextBox textBox, String text)
        {
            if (textBoxStartTime.InvokeRequired)
            {
                textBoxStartTime.Invoke(new Action(UpdateStartTime), new object\[\] { textBox, text }); 
            }
            else
            {
                textBoxStartTime.Text = text; 
            }
        }
    

    I also tried a delegate but it just does not see the other thread, I have verified the thread numbers are different and they're being used, it just doesn't make the connection. Any ideas or suggestions will be greatly appreciated, thanks in advance. Michael

    J 1 Reply Last reply
    0
    • M MAW30

      I have recently started to multithread my program and everything seems to work fine with the exception of a form I use to track the progress of data being transfered. The Progress form is on the main thread, I send data to it as my program is processing it. The problem is that is does not appear on the form, I have used various methods to Invoke the thread but it just goes into a loop and never stops or just goes into oblivian. Some of the methods are as follows or similar:

          public void UpdateStartTime(String text)
          {
              if (textBoxStartTime.InvokeRequired)
              {
                  UpdateStartTimeCallback updateStartTimeCallback = new UpdateStartTimeCallback(UpdateStartTime);
                  Invoke(updateStartTimeCallback, new object\[\] { Text });
              }
              else
              {
                  textBoxStartTime.Text = text; 
              }
          }
      

      and

          public void UpdateStartTime(TextBox textBox, String text)
          {
              if (textBoxStartTime.InvokeRequired)
              {
                  textBoxStartTime.Invoke(new Action(UpdateStartTime), new object\[\] { textBox, text }); 
              }
              else
              {
                  textBoxStartTime.Text = text; 
              }
          }
      

      I also tried a delegate but it just does not see the other thread, I have verified the thread numbers are different and they're being used, it just doesn't make the connection. Any ideas or suggestions will be greatly appreciated, thanks in advance. Michael

      J Offline
      J Offline
      Jibesh
      wrote on last edited by
      #2

      try like this. define a simple delegate in your class and call it like below. delegate definition

      public delegate void StatusUpdateHandler(string status)

      use the delegate in your application like this

      public void UpdateStartTime(string text)
      {
      if (this.InvokeRequired)
      {
      BeginInvoke(new StatusUpdateHandler(UpdateStartTime),new object[]{text} ); // this will call the UpdateStartTime method again, but second time you are routing your execution to main thread, InvokeRequired flag will be false and else part gets executed.
      }
      else
      {
      textBoxStartTime.Text = text;
      }

      }

      Jibesh V P

      M 1 Reply Last reply
      0
      • J Jibesh

        try like this. define a simple delegate in your class and call it like below. delegate definition

        public delegate void StatusUpdateHandler(string status)

        use the delegate in your application like this

        public void UpdateStartTime(string text)
        {
        if (this.InvokeRequired)
        {
        BeginInvoke(new StatusUpdateHandler(UpdateStartTime),new object[]{text} ); // this will call the UpdateStartTime method again, but second time you are routing your execution to main thread, InvokeRequired flag will be false and else part gets executed.
        }
        else
        {
        textBoxStartTime.Text = text;
        }

        }

        Jibesh V P

        M Offline
        M Offline
        MAW30
        wrote on last edited by
        #3

        I tried the above exactly as shown, however it does not return to the method. Any other ideas.

        D 1 Reply Last reply
        0
        • M MAW30

          I tried the above exactly as shown, however it does not return to the method. Any other ideas.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          That's the standard pattern. If it's not working, there's something you haven't told us and/or are not showing us. The code listed in the first reply is the way to do it, so long as the controls were created on the UI thread and your long-running code is on a different thread, be it directly launched with the Thread class, a Task or in a BackgroundWorker.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          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