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. Passing info from a thread

Passing info from a thread

Scheduled Pinned Locked Moved C#
question
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.
  • B Offline
    B Offline
    brsecu
    wrote on last edited by
    #1

    I'm running a process in a thread and want to put the results as they happen in a text box on my form. Is that possible? I cant seem to make it work.

    J 1 Reply Last reply
    0
    • B brsecu

      I'm running a process in a thread and want to put the results as they happen in a text box on my form. Is that possible? I cant seem to make it work.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      It's possible, you just have to transfer the results to the UI thread before updating the text box. 2 common ways of doing this:

      // on the background thread
      textBox.Invoke((MethodInvoker)delegate()
      {
          textBox.Text = "here are the results...";
      });

      The other common way and recommended way is to use a BackgroundWorker[^] component. It lets you run work on a background thread and call worker.ReportProgress, which fires an event on the proper thread, allowing you to update your UI in there.

      Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango

      E 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        It's possible, you just have to transfer the results to the UI thread before updating the text box. 2 common ways of doing this:

        // on the background thread
        textBox.Invoke((MethodInvoker)delegate()
        {
            textBox.Text = "here are the results...";
        });

        The other common way and recommended way is to use a BackgroundWorker[^] component. It lets you run work on a background thread and call worker.ReportProgress, which fires an event on the proper thread, allowing you to update your UI in there.

        Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        I don't believe the cast to MethodInvoker is necessary.

        Need a C# Consultant? I'm available.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

        J 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          I don't believe the cast to MethodInvoker is necessary.

          Need a C# Consultant? I'm available.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          It is because the parameter type is Delegate. The compiler cannot infer which delegate type you mean when passing in an anonymous method; you'll get a "cannot convert anonymous method to type System.Delegate because it is not a delegate type" compiler error.

          Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango

          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