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. .NET (Core and Framework)
  4. Accessing control from a different thread

Accessing control from a different thread

Scheduled Pinned Locked Moved .NET (Core and Framework)
winformshelptutorial
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.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on runButton

    extProcess = gcnew System::Diagnostics::Process;
    extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
    extProcess->StartInfo->RedirectStandardOutput = true;
    extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
    extProcess->StartInfo->FileName = EXTPROC_EXE;
    extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
    extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
    try
    {
    extProcessThread->RunWorkerAsync();

    The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window

    void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
    {
    extProcess->Start();
    extProcess->BeginOutputReadLine();
    }
    void Form1::StdoutHandler (Object ^ sender,
    System::Diagnostics::DataReceivedEventArgs ^output)
    {
    //Get text
    textBox->AppendText (output->Data + "\n");
    }

    The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...

    P S L 3 Replies Last reply
    0
    • P piul

      I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on runButton

      extProcess = gcnew System::Diagnostics::Process;
      extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
      extProcess->StartInfo->RedirectStandardOutput = true;
      extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
      extProcess->StartInfo->FileName = EXTPROC_EXE;
      extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
      extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
      try
      {
      extProcessThread->RunWorkerAsync();

      The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window

      void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
      {
      extProcess->Start();
      extProcess->BeginOutputReadLine();
      }
      void Form1::StdoutHandler (Object ^ sender,
      System::Diagnostics::DataReceivedEventArgs ^output)
      {
      //Get text
      textBox->AppendText (output->Data + "\n");
      }

      The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...

      P Offline
      P Offline
      piul
      wrote on last edited by
      #2

      Maybe using http://msdn.microsoft.com/en-us/library/ms171728.aspx

      1 Reply Last reply
      0
      • P piul

        I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on runButton

        extProcess = gcnew System::Diagnostics::Process;
        extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
        extProcess->StartInfo->RedirectStandardOutput = true;
        extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
        extProcess->StartInfo->FileName = EXTPROC_EXE;
        extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
        extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
        try
        {
        extProcessThread->RunWorkerAsync();

        The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window

        void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
        {
        extProcess->Start();
        extProcess->BeginOutputReadLine();
        }
        void Form1::StdoutHandler (Object ^ sender,
        System::Diagnostics::DataReceivedEventArgs ^output)
        {
        //Get text
        textBox->AppendText (output->Data + "\n");
        }

        The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...

        S Offline
        S Offline
        Simon_Whale
        wrote on last edited by
        #3

        You need to have a look at the invoke methods in the article shown by the other poster MSDN: Look at the example at the bottom of the page[^]

        As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.

        1 Reply Last reply
        0
        • P piul

          I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on runButton

          extProcess = gcnew System::Diagnostics::Process;
          extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
          extProcess->StartInfo->RedirectStandardOutput = true;
          extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
          extProcess->StartInfo->FileName = EXTPROC_EXE;
          extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
          extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
          try
          {
          extProcessThread->RunWorkerAsync();

          The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window

          void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
          {
          extProcess->Start();
          extProcess->BeginOutputReadLine();
          }
          void Form1::StdoutHandler (Object ^ sender,
          System::Diagnostics::DataReceivedEventArgs ^output)
          {
          //Get text
          textBox->AppendText (output->Data + "\n");
          }

          The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          This[^] explains it, however all examples are C# or VB.NET; same principles apply to C++/CLI. Amd a BackgroundWorker's ProgressChanged and RunWorkerCompleted handlers don't have the cross-thread problem as they run on the thread that created the BGW, which normally is the main thread. That is one of the big advantages of BGW. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          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