• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Thread Problem... AGIAN!

#pragma once

namespace example
     using namespace System;
     using namespace System::ComponentModel;
     using namespace System::Collections;
     using namespace System::Windows::Forms;
     using namespace System::Data;
     using namespace System::Drawing;
     mytest * obj1;
     static void __cdecl testthread(void * dummy)
          obj1 = new mytest();
     public __gc class Form1 : public System::Windows::Forms::Form

               _beginthread(testthread, 0, NULL);

          void Dispose(Boolean disposing)
               if (disposing && components)
     private: System::Windows::Forms::ListBox *  listBox1;
     private: System::Windows::Forms::ListBox *  listBox2;

          System::ComponentModel::Container * components;
          void InitializeComponent(void)...

I created a thread using _beginthread , but the problem is that I want the function testthread to be able to modify
the components in my windows form, (such as listbox or combobox)
But by creating that testthread function outside the class i am not able to access the component of Form1.. (very obvious :P)
I tried creating the thread inside the Form1(VOID) function, but it would never compile..
I think there is someway to create a thread inside the Form1 class so that the function is going to be able to update on the windows form component while the windows form can take user inputs.

Can anyone please help out on this?
1 Solution
_beginthread is C++ Run-Time Library function, and you are trying to apply it from managed application. You need to use managed Thread class instead of unmanaded _beginthread function.
Running managed thread is relatively simple, but it has one restriction: it's impossible to update windows form directly from the worker thread. To do this you need to use Control::BeginInvoke or Control::Invoke functions. The following article describes this:


I hope you don't have problems translating of C# code to managed C++. Slightly different syntax:

Thread m_WorkerThread;     // C#
Thread* m_WorkerThread;   // C++
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now