Check Control :: Invoke to drop the method into a safe stream to modify the control. To show the form of your example:
public delegate void SwapControlVisibleDelegate(Control^ target); public ref class Form1 : public System::Windows::Forms::Form { protected : virtual void OnShown(EventArgs^ e) override { __super::OnShown(e); some_function(); } void some_function() { System::Threading::Thread^ t1; System::Threading::ThreadStart^ ts = gcnew ystem::Threading::ThreadStart(this, &Form1::ThreadProc); t1 = gcnew System::Threading::Thread(ts); t1->Start(); } void ThreadProc() { Threading::Thread::Sleep(2000); for each(Control^ c in this->Controls) { SwapVisible(c); } } void SwapVisible(Control^ c) { if(c->InvokeRequired)
Here's how to invoke a method control in a safe thread to make changes. Right now I read your comment for the question. Take a look at the BackgroundWorker component , it is ideal for running an asynchronous task with cancellation support, and it also implements events to receive notifications about the progress and completion of a task.
Jairo source share