I have 2 buttons on my form. The First button contains a loop and second button a MessageBox.
If I press the first button, this loop will take sometime.
Now is my question this. Let´s say that the loop will take about > 5 seconds and I press button2 after 1 second, the MessageBox wont show immediately because the loop is working. The MessageBox will show when the loop is ready.

My question here is if it is possible when I press button2 to "Force" the messageBox to
appear immediately with any code or any other approach to it ?

The First Button have this code:

int count = 0;
for(int i = 0; i < 3000000000; i++)
{
     count = (count + 1);

The second Button has this code:

MessageBox::Show("Loop is working !");

I have an application on my computer that have a "Cancel" button while a process is
working.
When putting the Cursor over this button, you can actually press this button down.
You can see this visually when the button is pressed.

If I do the same with the buttoncontrols that you attach to the Form normally, this is not happening. If no process is going on, you can see a shadow over the button when the cursor is moved over but not if a process is going on.
I might wonder if this is possible to do in anyway, to "force"/ press down another button while a process is going on somewhere else.

What you're trying to do is concurrent processing. An easy way to do it is with .NET 2.0's BackgroundWorker class to start a separate thread for the time intensive loop.

Thank you. I have red about the Backgroundworker, the example for C++ that I am using.
It seems to be very much happening in the code. I am not really sure what is nessecary for me to use and what everything are for.
Though I have attached the Backgroundworkercontrol to my Form and put:
using namespace System::Threading;

I will try to explain excactly what I am doing. (I will cancel a loop like below).

I have 2 buttons on my Form.
I have declared a CancelAction like this:

private: bool CancelAction;

Button1 has this code that is doing a loop and while CancelAction is false wich is the defaultvalue, count will increase like the code is doing below.

int count = 0; 
for(int i = 0; i < 3000000000; i++) 
{ 
   if(CancelAction == false)
   {
      for(int i2 = 0; i2 < 2; i2++)
      {
	   count = (count + 1);
      }
   }
}

This is my second button that will set the CancelAction to true wich will cause my loop above to stop so the increase of count will stop.
This is working now. Usually you have to press the button several times to make it work.
But now I am trying to "force" it with the backgroundworkercontrol then so it will work when you press the button the first time.

CancelAction = true;

What you're trying to do is concurrent processing. An easy way to do it is with .NET 2.0's BackgroundWorker class to start a separate thread for the time intensive loop.

Here's an example of the code you would use for two buttons where the first initiates the BackgroundWorker and the second stops it using a bool flag:

bool _cancelAction;

Void Form1_Load(Object^ sender, EventArgs^ e)
{
  _cancelAction = false;
}

Void button1_Click(Object^ sender, EventArgs^ e)
{
  backgroundWorker1->RunWorkerAsync();
}

Void button2_Click(Object^ sender, EventArgs^ e)
{
  _cancelAction = true;
}

Void backgroundWorker1_DoWork(
  Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
  Int64 count = 0;

  for (Int64 i = 0; i < 3000000000; i++) {
    if (_cancelAction == true)
      break;

    for(int i2 = 0; i2 < 2; i2++)
      count = (count + 1);
  }
}

Void backgroundWorker1_RunWorkerCompleted(
  Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
{
  MessageBox::Show("Worker completed");
}

I have managed to apply this code to my program with the 2 buttons.
It do works, the cancelbutton cancel the process in button1 but when pressing the cancelbutton I will receive an Errormessage that says:

"DragDrop registration did not succeed"

and when looking at the Details of the message, it says like below.
What could this meen ?

System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.

at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)

Are you using drag and drop functionality in your form? If not, go ahead and set the AllowDrop property to false for all of your controls.

Yes, In my button1 I have a lot of code and the last line is:

this->form21instance.Show();

So I am opening a new Form here. In the opening form I have a checkedListBox that I set the AllowDrop property to false. So I am not getting this Error message anymore.
Before I changed the AllowDrop to false, Form2 did not opened at all and I only got the Errormessage.

The problem now though is that when Form2 opens, it stucks. The Form2 is not showing it´s buttons and controls. So what is happening is that my whole program is "stuck" so I have to close it in TaskManager.
Even if I will not press the Cancelbutton at all, Form2 will get stuck and there by the whole application.
I don´t know if it is possible to know what this could depend on ?
Thanks...


Are you using drag and drop functionality in your form? If not, go ahead and set the AllowDrop property to false for all of your controls.

When your program hangs, it's usually because you have an errant loop that isn't stopping. Start troubleshooting the problem by searching for loops that run when you show Form2. Ed can't offer much advice beyond that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.