Gui, Visual Studio 05, Loop + Events + Stuck

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 6
Reputation: 0o0 is an unknown quantity at this point 
Solved Threads: 0
0o0 0o0 is offline Offline
Newbie Poster

Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #1
Mar 28th, 2009
Hello,

I'm new to gui programming althou i've done things before in c++ i've always encountered this error and never really tried to fix it.

Lets for say I have a counter label and a button that says "Start" when one clicks start the label changes values from 0 to 999 (does't matter how fast) . SO i have an Event click in my Form1.h (all generated by VS 05) that calls function Count and the functions counts as long as global varaible is set to false .

the only way this global variable will be false is if the persons clicks the event again (or any other button to stop) and the count should stop where it is if the variable goes false and the varaible is turned back on when the start is pressed.

basicly my point being i wrote the program but the gui doesn't take event in the middle of my loop or in other words i want it to run in the background so i can do my other stuff in meanwhile also
like be able to click more buttons that do other stuff..

Here is my .h class files generated by VS ++
  1.  
  2. #pragma once
  3.  
  4. #include <iostream>
  5. #include <windows.h>
  6.  
  7. bool glbl = false;
  8. namespace Test {
  9.  
  10. using namespace System;
  11. using namespace System::ComponentModel;
  12. using namespace System::Collections;
  13. using namespace System::Windows::Forms;
  14. using namespace System::Data;
  15. using namespace System::Drawing;
  16.  
  17. /// <summary>
  18. /// Summary for Form1
  19. ///
  20. /// WARNING: If you change the name of this class, you will need to change the
  21. /// 'Resource File Name' property for the managed resource compiler tool
  22. /// associated with all .resx files this class depends on. Otherwise,
  23. /// the designers will not be able to interact properly with localized
  24. /// resources associated with this form.
  25. /// </summary>
  26. public ref class Form1 : public System::Windows::Forms::Form
  27. {
  28. public:
  29. Form1(void)
  30. {
  31. InitializeComponent();
  32. //
  33. //TODO: Add the constructor code here
  34. //
  35. }
  36.  
  37.  
  38. void count10(){
  39. for(int i=0;i<1000;i++){
  40. if(glbl == true){
  41. this->lblCount->Text=L"Counting: "+i;
  42. this->Refresh();
  43. }else
  44. break;
  45. }
  46. }
  47.  
  48. protected:
  49. /// <summary>
  50. /// Clean up any resources being used.
  51. /// </summary>
  52. ~Form1()
  53. {
  54. if (components)
  55. {
  56. delete components;
  57. }
  58. }
  59. public: System::Windows::Forms::Button^ btnStart;
  60. public: System::Windows::Forms::Label^ lblCount;
  61.  
  62.  
  63. public:
  64. /// <summary>
  65. /// Required designer variable.
  66. /// </summary>
  67. System::ComponentModel::Container ^components;
  68.  
  69. #pragma region Windows Form Designer generated code
  70. /// <summary>
  71. /// Required method for Designer support - do not modify
  72. /// the contents of this method with the code editor.
  73. /// </summary>
  74. void InitializeComponent(void)
  75. {
  76. this->btnStart = (gcnew System::Windows::Forms::Button());
  77. this->lblCount = (gcnew System::Windows::Forms::Label());
  78. this->SuspendLayout();
  79. //
  80. // btnStart
  81. //
  82. this->btnStart->Location = System::Drawing::Point(12, 42);
  83. this->btnStart->Name = L"btnStart";
  84. this->btnStart->Size = System::Drawing::Size(75, 23);
  85. this->btnStart->TabIndex = 0;
  86. this->btnStart->Text = L"Start";
  87. this->btnStart->UseVisualStyleBackColor = true;
  88. this->btnStart->Click += gcnew System::EventHandler(this, &Form1::btnStart_Click);
  89. //
  90. // lblCount
  91. //
  92. this->lblCount->AutoSize = true;
  93. this->lblCount->Location = System::Drawing::Point(29, 9);
  94. this->lblCount->Name = L"lblCount";
  95. this->lblCount->Size = System::Drawing::Size(35, 13);
  96. this->lblCount->TabIndex = 1;
  97. this->lblCount->Text = L"label1";
  98. //
  99. // Form1
  100. //
  101. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  102. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  103. this->ClientSize = System::Drawing::Size(115, 86);
  104. this->Controls->Add(this->lblCount);
  105. this->Controls->Add(this->btnStart);
  106. this->Name = L"Form1";
  107. this->Text = L"Form1";
  108. this->ResumeLayout(false);
  109. this->PerformLayout();
  110.  
  111. }
  112. #pragma endregion
  113. public: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) {
  114. if(glbl == false){
  115. glbl=true;
  116. this->btnStart->Text="Stop";
  117. count10();
  118. glbl=false;
  119. this->btnStart->Text="Start";
  120. }else{
  121. this->btnStart->Text="Start";
  122. glbl=false;
  123. }
  124. }
  125. };
  126. }


And here is my .CPP file
  1. // Test.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include "Form1.h"
  5.  
  6. using namespace Test;
  7.  
  8. [STAThreadAttribute]
  9. int main(array<System::String ^> ^args)
  10. {
  11. // Enabling Windows XP visual effects before any controls are created
  12. Application::EnableVisualStyles();
  13. Application::SetCompatibleTextRenderingDefault(false);
  14.  
  15. Test::Form1 f1;
  16. f1.ShowDialog();
  17.  
  18. // Create the main window and run it
  19. //Application::Run(gcnew Form1());
  20.  
  21.  
  22. return 0;
  23. }

Thanks for help. I think i'm missing the basic concept of events.
Axar
Last edited by 0o0; Mar 28th, 2009 at 4:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #2
Mar 28th, 2009
basicly my point being i wrote the program but the gui doesn't take event in the middle of my loop or in other words i want it to run in the background so i can do my other stuff in meanwhile also
You need multiple threads to accomplish that. Create a worker thread that does the counting so that the main thread can continue doing whatever needs to be done.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: 0o0 is an unknown quantity at this point 
Solved Threads: 0
0o0 0o0 is offline Offline
Newbie Poster

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #3
Mar 29th, 2009
Im not quiet sure how to do do that. Can i get some hints on how to accomplish a worker thread?

one more thing, can I also do lets say 10 things in the background with worker thread?
thanks once again.
Last edited by 0o0; Mar 29th, 2009 at 12:23 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: 0o0 is an unknown quantity at this point 
Solved Threads: 0
0o0 0o0 is offline Offline
Newbie Poster

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #4
Mar 29th, 2009
So, i created a worker that im guessing suppose to work

Now the problem is that when i'm doing my "WORK" (count function) i'm suppose to change the label values however, the program doesn't do anything there and exists worker when i try to change the value of my label. i checked with break point and all my form values are Error : Cannot obtain value written in them.

thanks once again.

  1.  
  2. #pragma once
  3.  
  4. #include <iostream>
  5. #include <windows.h>
  6.  
  7. bool glbl = false;
  8. namespace Test {
  9.  
  10. using namespace System;
  11. using namespace System::ComponentModel;
  12. using namespace System::Collections;
  13. using namespace System::Windows::Forms;
  14. using namespace System::Data;
  15. using namespace System::Drawing;
  16.  
  17. /// <summary>
  18. /// Summary for Form1
  19. ///
  20. /// 'Resource File Name' property for the managed resource compiler tool
  21. /// associated with all .resx files this class depends on. Otherwise,
  22. /// the designers will not be able to interact properly with localized
  23. /// resources associated with this form.
  24. /// </summary>
  25.  
  26. public ref class Form1 : public System::Windows::Forms::Form
  27. {
  28.  
  29. public:
  30. Form1(void)
  31. {
  32. InitializeComponent();
  33. //
  34. //TODO: Add the constructor code here
  35. //
  36. }
  37.  
  38. protected:
  39. /// <summary>
  40. /// Clean up any resources being used.
  41. /// </summary>
  42. ~Form1()
  43. {
  44. if (components)
  45. {
  46. delete components;
  47. }
  48. }
  49.  
  50. private: System::Windows::Forms::Button^ btnStart;
  51. private: System::Windows::Forms::Label^ lblCount;
  52. private: System::ComponentModel::BackgroundWorker^ backgroundWorker1;
  53.  
  54. private:
  55. /// <summary>
  56. /// Required designer variable.
  57. /// </summary>
  58. System::ComponentModel::Container ^components;
  59.  
  60. #pragma region Windows Form Designer generated code
  61. /// <summary>
  62. /// Required method for Designer support - do not modify
  63. /// the contents of this method with the code editor.
  64. /// </summary>
  65.  
  66.  
  67.  
  68. void InitializeComponent(void)
  69. {
  70. //
  71. this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
  72. // Create backgroundWorker1
  73. //
  74. this->backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &Form1::Worker);
  75.  
  76. this->btnStart = (gcnew System::Windows::Forms::Button());
  77. this->lblCount = (gcnew System::Windows::Forms::Label());
  78. this->SuspendLayout();
  79. //
  80. // btnStart
  81. //
  82. this->btnStart->Location = System::Drawing::Point(46, 61);
  83. this->btnStart->Name = L"btnStart";
  84. this->btnStart->Size = System::Drawing::Size(75, 23);
  85. this->btnStart->TabIndex = 0;
  86. this->btnStart->Text = L"Start";
  87. this->btnStart->UseVisualStyleBackColor = true;
  88. this->btnStart->Click += gcnew System::EventHandler(this, &Form1::btnStart_Click);
  89. //
  90. // lblCount
  91. //
  92. this->lblCount->AutoSize = true;
  93. this->lblCount->Location = System::Drawing::Point(43, 24);
  94. this->lblCount->Name = L"lblCount";
  95. this->lblCount->Size = System::Drawing::Size(35, 13);
  96. this->lblCount->TabIndex = 1;
  97. this->lblCount->Text = L"label1";
  98. //
  99. // Form1
  100. //
  101. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  102. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  103. this->ClientSize = System::Drawing::Size(177, 118);
  104. this->Controls->Add(this->lblCount);
  105. this->Controls->Add(this->btnStart);
  106. this->Name = L"Form1";
  107. this->Text = L"Form1";
  108. this->ResumeLayout(false);
  109. this->PerformLayout();
  110.  
  111. }
  112. #pragma endregion
  113. private: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) {
  114. //Add Worker Thread Handler
  115. this->backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &Form1::Worker);
  116.  
  117. if(glbl == false){
  118. glbl=true;
  119. this->btnStart->Text="Stop";
  120. this->backgroundWorker1->RunWorkerAsync();
  121. }else{
  122. this->btnStart->Text="Start";
  123. glbl=false;
  124. }
  125. }
  126. public:
  127.  
  128. private:
  129. System::Void Worker(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
  130. {
  131. count();
  132.  
  133. }
  134. void count(){
  135. for(int i=0;i<1000;i++){
  136. if(glbl == true){
  137. this->lblCount->Text=L"Counting: "+i;
  138. }else
  139. break;
  140. }
  141.  
  142. this->btnStart->Text="Start";
  143. glbl=false;
  144. }
  145. };
  146. }
Last edited by 0o0; Mar 29th, 2009 at 6:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: 0o0 is an unknown quantity at this point 
Solved Threads: 0
0o0 0o0 is offline Offline
Newbie Poster

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #5
Apr 4th, 2009
bump!

I can't figure out how to change gui from background worker!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 145
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 35
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #6
Apr 4th, 2009
Originally Posted by 0o0 View Post
bump!

I can't figure out how to change gui from background worker!
No, you won't be able to update a GUI from a worker by calling it directly. You need to use message passing. i.e. call SendMessage() or PostMessage() to send a user defined message to the GUI thread. You'll need to catch the message in your GUI thread and do your GUI stuff.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: 0o0 is an unknown quantity at this point 
Solved Threads: 0
0o0 0o0 is offline Offline
Newbie Poster

Re: Gui, Visual Studio 05, Loop + Events + Stuck

 
0
  #7
Apr 4th, 2009
I was looking at background workers and came across this auricle
http://www.codeproject.com/KB/cpp/Ba...r_Threads.aspx

the Author has two programs
Synchronous Example and Asynchronous
in Synchronous method he does in C#
  1.  
  2. m_fmProgress.lblDescription.Invoke((MethodInvoker) delegate()
  3. {
  4. m_fmProgress.lblDescription.Text =
  5. "Processing file " + i.ToString() +
  6. " of " + iCount.ToString();
  7. m_fmProgress.progressBar1.Value =
  8. Convert.ToInt32( i * ( 100.0 / iCount ) );
  9. });

im wondering if there is a way to do this in C++?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 627 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC