[C++, windows form]How do I make the main thread wait for the called thread to finish?










0















I created 2 buttons, one for start a new thread, the other to end it. The actual calculation inside the new thread involved new and delete so I don't abort the thread directly, but using a flag to end it. It may take some time to end the delete and result-saving, so I want the main thread to wait for the new thread to end. But however I tried, I find the new thread doesn't run(though its ThreadState is running) until the command lines for the stop-button are conducted. System::Threading::Thread works quite different from thread to me. Is it how it should be?



#include "stdafx.h"

ref class Form1 : System::Windows::Forms::Form

public:
//define a thread name and a flag to terminate the thread
System::Threading::Thread^ th1;
static bool ITP1=0;

Form1(void)
InitializeComponent();

System::Windows::Forms::Button^ ButtonStart;
System::Windows::Forms::Button^ ButtonStop;
System::Windows::Forms::Label^ Label1;

void InitializeComponent(void)

this->SuspendLayout();

this->ButtonStart = gcnew System::Windows::Forms::Button();
this->ButtonStart->Location = System::Drawing::Point(20, 20);
this->ButtonStart->Click += gcnew System::EventHandler(this, &Form1::ButtonStart_Click);
this->Controls->Add(this->ButtonStart);

this->ButtonStop = gcnew System::Windows::Forms::Button();
this->ButtonStop->Location = System::Drawing::Point(120, 20);
this->ButtonStop->Click += gcnew System::EventHandler(this, &Form1::ButtonStop_Click);
this->Controls->Add(this->ButtonStop);

this->Label1 = gcnew System::Windows::Forms::Label();
this->Label1->Location = System::Drawing::Point(20, 80);
this->Controls->Add(this->Label1);

this->ResumeLayout(false);


void ThreadStart()

for (int idx=0;idx<999999999;++idx)

if (ITP1) break;

this->Label1->Text = "finished";
ITP1=0;


System::Void ButtonStart_Click(System::Object^ sender, System::EventArgs^ e)

th1 = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&Form1::ThreadStart));
th1->Start();
this->Label1->Text = "running";


System::Void ButtonStop_Click(System::Object^ sender, System::EventArgs^ e)

if (th1->ThreadState==System::Threading::ThreadState::Running)

//use the flag to stop the thread
ITP1=1;
//the wait method using while+sleep doesn't work
while (th1->ThreadState==System::Threading::ThreadState::Running) System::Threading::Thread::Sleep(1000);
//replacing the wait method above with "th1->Join()" doesn't work either


;

int main()

Form1^ A1 = gcnew Form1();
A1->ShowDialog();
return 0;










share|improve this question
























  • How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

    – Sami Kuhmonen
    Nov 14 '18 at 5:24











  • Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

    – reko34
    Nov 14 '18 at 5:30











  • Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

    – Sami Kuhmonen
    Nov 14 '18 at 5:31











  • Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

    – reko34
    Nov 14 '18 at 5:35











  • "Join me and together we shall rule the universe." -Darth Threader

    – user4581301
    Nov 14 '18 at 5:37















0















I created 2 buttons, one for start a new thread, the other to end it. The actual calculation inside the new thread involved new and delete so I don't abort the thread directly, but using a flag to end it. It may take some time to end the delete and result-saving, so I want the main thread to wait for the new thread to end. But however I tried, I find the new thread doesn't run(though its ThreadState is running) until the command lines for the stop-button are conducted. System::Threading::Thread works quite different from thread to me. Is it how it should be?



#include "stdafx.h"

ref class Form1 : System::Windows::Forms::Form

public:
//define a thread name and a flag to terminate the thread
System::Threading::Thread^ th1;
static bool ITP1=0;

Form1(void)
InitializeComponent();

System::Windows::Forms::Button^ ButtonStart;
System::Windows::Forms::Button^ ButtonStop;
System::Windows::Forms::Label^ Label1;

void InitializeComponent(void)

this->SuspendLayout();

this->ButtonStart = gcnew System::Windows::Forms::Button();
this->ButtonStart->Location = System::Drawing::Point(20, 20);
this->ButtonStart->Click += gcnew System::EventHandler(this, &Form1::ButtonStart_Click);
this->Controls->Add(this->ButtonStart);

this->ButtonStop = gcnew System::Windows::Forms::Button();
this->ButtonStop->Location = System::Drawing::Point(120, 20);
this->ButtonStop->Click += gcnew System::EventHandler(this, &Form1::ButtonStop_Click);
this->Controls->Add(this->ButtonStop);

this->Label1 = gcnew System::Windows::Forms::Label();
this->Label1->Location = System::Drawing::Point(20, 80);
this->Controls->Add(this->Label1);

this->ResumeLayout(false);


void ThreadStart()

for (int idx=0;idx<999999999;++idx)

if (ITP1) break;

this->Label1->Text = "finished";
ITP1=0;


System::Void ButtonStart_Click(System::Object^ sender, System::EventArgs^ e)

th1 = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&Form1::ThreadStart));
th1->Start();
this->Label1->Text = "running";


System::Void ButtonStop_Click(System::Object^ sender, System::EventArgs^ e)

if (th1->ThreadState==System::Threading::ThreadState::Running)

//use the flag to stop the thread
ITP1=1;
//the wait method using while+sleep doesn't work
while (th1->ThreadState==System::Threading::ThreadState::Running) System::Threading::Thread::Sleep(1000);
//replacing the wait method above with "th1->Join()" doesn't work either


;

int main()

Form1^ A1 = gcnew Form1();
A1->ShowDialog();
return 0;










share|improve this question
























  • How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

    – Sami Kuhmonen
    Nov 14 '18 at 5:24











  • Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

    – reko34
    Nov 14 '18 at 5:30











  • Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

    – Sami Kuhmonen
    Nov 14 '18 at 5:31











  • Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

    – reko34
    Nov 14 '18 at 5:35











  • "Join me and together we shall rule the universe." -Darth Threader

    – user4581301
    Nov 14 '18 at 5:37













0












0








0








I created 2 buttons, one for start a new thread, the other to end it. The actual calculation inside the new thread involved new and delete so I don't abort the thread directly, but using a flag to end it. It may take some time to end the delete and result-saving, so I want the main thread to wait for the new thread to end. But however I tried, I find the new thread doesn't run(though its ThreadState is running) until the command lines for the stop-button are conducted. System::Threading::Thread works quite different from thread to me. Is it how it should be?



#include "stdafx.h"

ref class Form1 : System::Windows::Forms::Form

public:
//define a thread name and a flag to terminate the thread
System::Threading::Thread^ th1;
static bool ITP1=0;

Form1(void)
InitializeComponent();

System::Windows::Forms::Button^ ButtonStart;
System::Windows::Forms::Button^ ButtonStop;
System::Windows::Forms::Label^ Label1;

void InitializeComponent(void)

this->SuspendLayout();

this->ButtonStart = gcnew System::Windows::Forms::Button();
this->ButtonStart->Location = System::Drawing::Point(20, 20);
this->ButtonStart->Click += gcnew System::EventHandler(this, &Form1::ButtonStart_Click);
this->Controls->Add(this->ButtonStart);

this->ButtonStop = gcnew System::Windows::Forms::Button();
this->ButtonStop->Location = System::Drawing::Point(120, 20);
this->ButtonStop->Click += gcnew System::EventHandler(this, &Form1::ButtonStop_Click);
this->Controls->Add(this->ButtonStop);

this->Label1 = gcnew System::Windows::Forms::Label();
this->Label1->Location = System::Drawing::Point(20, 80);
this->Controls->Add(this->Label1);

this->ResumeLayout(false);


void ThreadStart()

for (int idx=0;idx<999999999;++idx)

if (ITP1) break;

this->Label1->Text = "finished";
ITP1=0;


System::Void ButtonStart_Click(System::Object^ sender, System::EventArgs^ e)

th1 = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&Form1::ThreadStart));
th1->Start();
this->Label1->Text = "running";


System::Void ButtonStop_Click(System::Object^ sender, System::EventArgs^ e)

if (th1->ThreadState==System::Threading::ThreadState::Running)

//use the flag to stop the thread
ITP1=1;
//the wait method using while+sleep doesn't work
while (th1->ThreadState==System::Threading::ThreadState::Running) System::Threading::Thread::Sleep(1000);
//replacing the wait method above with "th1->Join()" doesn't work either


;

int main()

Form1^ A1 = gcnew Form1();
A1->ShowDialog();
return 0;










share|improve this question
















I created 2 buttons, one for start a new thread, the other to end it. The actual calculation inside the new thread involved new and delete so I don't abort the thread directly, but using a flag to end it. It may take some time to end the delete and result-saving, so I want the main thread to wait for the new thread to end. But however I tried, I find the new thread doesn't run(though its ThreadState is running) until the command lines for the stop-button are conducted. System::Threading::Thread works quite different from thread to me. Is it how it should be?



#include "stdafx.h"

ref class Form1 : System::Windows::Forms::Form

public:
//define a thread name and a flag to terminate the thread
System::Threading::Thread^ th1;
static bool ITP1=0;

Form1(void)
InitializeComponent();

System::Windows::Forms::Button^ ButtonStart;
System::Windows::Forms::Button^ ButtonStop;
System::Windows::Forms::Label^ Label1;

void InitializeComponent(void)

this->SuspendLayout();

this->ButtonStart = gcnew System::Windows::Forms::Button();
this->ButtonStart->Location = System::Drawing::Point(20, 20);
this->ButtonStart->Click += gcnew System::EventHandler(this, &Form1::ButtonStart_Click);
this->Controls->Add(this->ButtonStart);

this->ButtonStop = gcnew System::Windows::Forms::Button();
this->ButtonStop->Location = System::Drawing::Point(120, 20);
this->ButtonStop->Click += gcnew System::EventHandler(this, &Form1::ButtonStop_Click);
this->Controls->Add(this->ButtonStop);

this->Label1 = gcnew System::Windows::Forms::Label();
this->Label1->Location = System::Drawing::Point(20, 80);
this->Controls->Add(this->Label1);

this->ResumeLayout(false);


void ThreadStart()

for (int idx=0;idx<999999999;++idx)

if (ITP1) break;

this->Label1->Text = "finished";
ITP1=0;


System::Void ButtonStart_Click(System::Object^ sender, System::EventArgs^ e)

th1 = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&Form1::ThreadStart));
th1->Start();
this->Label1->Text = "running";


System::Void ButtonStop_Click(System::Object^ sender, System::EventArgs^ e)

if (th1->ThreadState==System::Threading::ThreadState::Running)

//use the flag to stop the thread
ITP1=1;
//the wait method using while+sleep doesn't work
while (th1->ThreadState==System::Threading::ThreadState::Running) System::Threading::Thread::Sleep(1000);
//replacing the wait method above with "th1->Join()" doesn't work either


;

int main()

Form1^ A1 = gcnew Form1();
A1->ShowDialog();
return 0;







multithreading c++-cli






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 5:35









Swordfish

9,37811436




9,37811436










asked Nov 14 '18 at 5:17









reko34reko34

1




1












  • How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

    – Sami Kuhmonen
    Nov 14 '18 at 5:24











  • Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

    – reko34
    Nov 14 '18 at 5:30











  • Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

    – Sami Kuhmonen
    Nov 14 '18 at 5:31











  • Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

    – reko34
    Nov 14 '18 at 5:35











  • "Join me and together we shall rule the universe." -Darth Threader

    – user4581301
    Nov 14 '18 at 5:37

















  • How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

    – Sami Kuhmonen
    Nov 14 '18 at 5:24











  • Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

    – reko34
    Nov 14 '18 at 5:30











  • Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

    – Sami Kuhmonen
    Nov 14 '18 at 5:31











  • Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

    – reko34
    Nov 14 '18 at 5:35











  • "Join me and together we shall rule the universe." -Darth Threader

    – user4581301
    Nov 14 '18 at 5:37
















How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

– Sami Kuhmonen
Nov 14 '18 at 5:24





How did you determine it doesn’t run? Did you use a debugger to see what’s happening?

– Sami Kuhmonen
Nov 14 '18 at 5:24













Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

– reko34
Nov 14 '18 at 5:30





Yes. I add a break under System::Void ButtonStop_Click. th1->ThreadState is always Running though no actions are performed. So the while loop is dead.

– reko34
Nov 14 '18 at 5:30













Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

– Sami Kuhmonen
Nov 14 '18 at 5:31





Did you put a breakpoint inside the thread? That’s the only way to know if it’s running since it doesn’t do anything.

– Sami Kuhmonen
Nov 14 '18 at 5:31













Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

– reko34
Nov 14 '18 at 5:35





Debug doesn't work correctly with threading for my VC++ express. The command lines under the button is how far it can go. But I printed the ThreadState to the label. So without debug I also know if it's running or stopped.

– reko34
Nov 14 '18 at 5:35













"Join me and together we shall rule the universe." -Darth Threader

– user4581301
Nov 14 '18 at 5:37





"Join me and together we shall rule the universe." -Darth Threader

– user4581301
Nov 14 '18 at 5:37












2 Answers
2






active

oldest

votes


















1














You have to join() the called thread in the main thread. Then the main thread will wait until the called thread is finished.



See the documentation for Join to know how it is to be called.






share|improve this answer

























  • I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

    – reko34
    Nov 14 '18 at 6:31











  • Is ButtonStop_Click run as part of the main thread?

    – P.W
    Nov 14 '18 at 7:11











  • All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

    – reko34
    Nov 14 '18 at 7:25











  • It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

    – P.W
    Nov 14 '18 at 7:38











  • No. th1->Join() is called inside the form class, which is in the main thread.

    – reko34
    Nov 14 '18 at 8:02


















0














Finally I found the cause. It's just the "this->" pointer in the new thread. Removing it makes everything OK.
I suppose it's because the Form allows operation on only one element at the same time. I ask the button-click to wait for the new thread, and the new thread tries to edit the other form element. They wait for each other to end and cause a dead loop.






share|improve this answer























  • Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

    – reko34
    Nov 14 '18 at 8:27










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293603%2fc-windows-formhow-do-i-make-the-main-thread-wait-for-the-called-thread-to-f%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have to join() the called thread in the main thread. Then the main thread will wait until the called thread is finished.



See the documentation for Join to know how it is to be called.






share|improve this answer

























  • I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

    – reko34
    Nov 14 '18 at 6:31











  • Is ButtonStop_Click run as part of the main thread?

    – P.W
    Nov 14 '18 at 7:11











  • All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

    – reko34
    Nov 14 '18 at 7:25











  • It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

    – P.W
    Nov 14 '18 at 7:38











  • No. th1->Join() is called inside the form class, which is in the main thread.

    – reko34
    Nov 14 '18 at 8:02















1














You have to join() the called thread in the main thread. Then the main thread will wait until the called thread is finished.



See the documentation for Join to know how it is to be called.






share|improve this answer

























  • I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

    – reko34
    Nov 14 '18 at 6:31











  • Is ButtonStop_Click run as part of the main thread?

    – P.W
    Nov 14 '18 at 7:11











  • All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

    – reko34
    Nov 14 '18 at 7:25











  • It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

    – P.W
    Nov 14 '18 at 7:38











  • No. th1->Join() is called inside the form class, which is in the main thread.

    – reko34
    Nov 14 '18 at 8:02













1












1








1







You have to join() the called thread in the main thread. Then the main thread will wait until the called thread is finished.



See the documentation for Join to know how it is to be called.






share|improve this answer















You have to join() the called thread in the main thread. Then the main thread will wait until the called thread is finished.



See the documentation for Join to know how it is to be called.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 5:29

























answered Nov 14 '18 at 5:24









P.WP.W

12.9k3945




12.9k3945












  • I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

    – reko34
    Nov 14 '18 at 6:31











  • Is ButtonStop_Click run as part of the main thread?

    – P.W
    Nov 14 '18 at 7:11











  • All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

    – reko34
    Nov 14 '18 at 7:25











  • It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

    – P.W
    Nov 14 '18 at 7:38











  • No. th1->Join() is called inside the form class, which is in the main thread.

    – reko34
    Nov 14 '18 at 8:02

















  • I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

    – reko34
    Nov 14 '18 at 6:31











  • Is ButtonStop_Click run as part of the main thread?

    – P.W
    Nov 14 '18 at 7:11











  • All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

    – reko34
    Nov 14 '18 at 7:25











  • It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

    – P.W
    Nov 14 '18 at 7:38











  • No. th1->Join() is called inside the form class, which is in the main thread.

    – reko34
    Nov 14 '18 at 8:02
















I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

– reko34
Nov 14 '18 at 6:31





I've read this documentation before I ask the question here. In fact, I used Join() in the main thread(the form is called by the main function) while the documentation seems to use it in the new threads.

– reko34
Nov 14 '18 at 6:31













Is ButtonStop_Click run as part of the main thread?

– P.W
Nov 14 '18 at 7:11





Is ButtonStop_Click run as part of the main thread?

– P.W
Nov 14 '18 at 7:11













All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

– reko34
Nov 14 '18 at 7:25





All my scripts are above. You can see I only created one thread, and used ButtonStop_Click to stop it. So ButtonStop_Click can only be called from the main thread.

– reko34
Nov 14 '18 at 7:25













It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

– P.W
Nov 14 '18 at 7:38





It looks as if you are using th1->Join() in the created thread. The join should be called in the main thread, soon after starting the created thread.

– P.W
Nov 14 '18 at 7:38













No. th1->Join() is called inside the form class, which is in the main thread.

– reko34
Nov 14 '18 at 8:02





No. th1->Join() is called inside the form class, which is in the main thread.

– reko34
Nov 14 '18 at 8:02













0














Finally I found the cause. It's just the "this->" pointer in the new thread. Removing it makes everything OK.
I suppose it's because the Form allows operation on only one element at the same time. I ask the button-click to wait for the new thread, and the new thread tries to edit the other form element. They wait for each other to end and cause a dead loop.






share|improve this answer























  • Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

    – reko34
    Nov 14 '18 at 8:27















0














Finally I found the cause. It's just the "this->" pointer in the new thread. Removing it makes everything OK.
I suppose it's because the Form allows operation on only one element at the same time. I ask the button-click to wait for the new thread, and the new thread tries to edit the other form element. They wait for each other to end and cause a dead loop.






share|improve this answer























  • Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

    – reko34
    Nov 14 '18 at 8:27













0












0








0







Finally I found the cause. It's just the "this->" pointer in the new thread. Removing it makes everything OK.
I suppose it's because the Form allows operation on only one element at the same time. I ask the button-click to wait for the new thread, and the new thread tries to edit the other form element. They wait for each other to end and cause a dead loop.






share|improve this answer













Finally I found the cause. It's just the "this->" pointer in the new thread. Removing it makes everything OK.
I suppose it's because the Form allows operation on only one element at the same time. I ask the button-click to wait for the new thread, and the new thread tries to edit the other form element. They wait for each other to end and cause a dead loop.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 8:07









reko34reko34

1




1












  • Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

    – reko34
    Nov 14 '18 at 8:27

















  • Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

    – reko34
    Nov 14 '18 at 8:27
















Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

– reko34
Nov 14 '18 at 8:27





Thanks for everyone. I have to find other solutions. I need the created thread to return the result to UI and update the progress bar so I cannot simply remove the "this" pointer.

– reko34
Nov 14 '18 at 8:27

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293603%2fc-windows-formhow-do-i-make-the-main-thread-wait-for-the-called-thread-to-f%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線