-1

I am new to c++ windows form programming. I have designed a windows form which has 5 buttons and 5 graphs. When any buttom is pressed 100 random values are ploted in the respective graph. Everything is working perfectly except when the values are being plotted no other button works. I have created a for loop which loops 100 times and plots the randomly generated values in the respecive graph. But while one graph is being plotted no ther button work. I actully wanna create a start and stop button so when the start buttom is pressed the plotting starts and when stop is pressed the plotting stops. Thanks in advance.

the code for the button is as follows

private: System::Void btn1_Click(System::Object^  sender, System::EventArgs^  e) {

             for (z = 0; z < 100; z++)
             {
                 y = rand() % 8 + 1;
                 x = rangeMax;
                 //plot data
                 gp1->PlotXY(x, y, 0);

                 //display lable
                 dis1->Text = Convert::ToString(y);
                 dis1->Update();
                 ctr++;
                 Sleep(1);
                 if (ctr == 1)
                 {
                     rangeMin = rangeMin + 0.2;
                     rangeMax = rangeMax + 0.2;
                     gp1->SetRange(rangeMin, rangeMax, 0, 10);
                     gp1->XGridNumber::set(100);
                     gp1->YGridNumber::set(10);
                     ctr = 0;
                 }
                 gp1->Update();

             }
hp26
  • 19
  • 2
  • 1
    You really need to provide more information as in source-code and alike for us to possibly help. – Till Nov 15 '14 at 11:49
  • i have added the code.. when i click the button ,with the above mentioned code, no other buttons work till the loop mentioned above finishes. – hp26 Nov 15 '14 at 13:28
  • This is the expected behavior. I mean when you do processing in your GUI thread it will block until the processing completes. – drescherjm Nov 15 '14 at 13:37
  • You may want this:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.110%29.aspx and http://stackoverflow.com/questions/4428817/accessing-ui-control-from-backgroundworker-thread-c-sharp – drescherjm Nov 15 '14 at 13:40
  • thank you every one for your valuable reply. – hp26 Nov 15 '14 at 13:49

2 Answers2

0

When you click the button, your code will enter this piece of code and won't exit until it's done. So it can't process the clicks on the other buttons. Solving the problem might be quite tricky, so I won't go into much detail here, I'll try to give help you finding a way to solve the problem by yourself.

There are 2 ways to do it(there are more, but I'll talk about these 2):

  • Multithreading;
  • Queues;

Multithreading: The idea is to create the possibility for your code to be in multiple places at the same time. Instead of doing everything inside your button function, this function will only launch a new thread that will do the hard work. This probably will create problems because you'll have to worry about synchronization, deathlocks and messy stuff.

Queues: Probably easier to implement that multithreading. The idea behind it is to push the information that you need to calculate and draw points to some variable (a stack is appropriate). This will be a fast operation, and your program will be able to exit the button function quickly. Afterwards, in your main loop, you will have to implement something that deals with the information that you saved to your stack variable. Just be carefull to not make the same mistake and try to process it all at the same time. I suggest you to process point by point if and only if a second has passed since you processed the last point. This way you'll get the same behaviour you have now, but without being stuck.

SlySherZ
  • 1,631
  • 1
  • 16
  • 25
0

This is because you have only one thread to handle these buttons. If your thread starts to run this loop you got here then this thread cannot do anything else untill it is finished.

You got a few options here. Optimize this code and see if you realy need all of it(only you can tell). The other option is to implement multithreading to handle those functions. But this will require alot of work to get it working properly. Remember that only one thread can handle UI's so you could make a worker thread to calculate values and such. Check this out http://www.codeproject.com/Articles/540912/Cplusplus-Threads-Make-your-multitasking-life-e