How to close an application when a button is clicked even when the application is running another pr

  • Thread starter Anderson Takemitsu Kubota
  • Start date
A

Anderson Takemitsu Kubota

Hi!

I need to run a process that should take over 1 minute, for instance.
I desire to put a button that the user can cancel the process.
For this, I was using the Application.DoEvents in the first process, and several times I verify the value of a m_bCancel variable that informs when the user requests the application to cancel.

This is the code I've being implementing on the cancel button is something like this:

private void btnCancel_Click(object sender, System.EventArgs e)
{
if(m_bRunning)
{
AddStatus("Process is being aborted...");
m_MainProcess.SetCancel(true);
int nCount = 0;
while(nCount < 5)
{
Application.DoEvents();
if(m_bRunning)
Thread.Sleep(1000);
else
break;
nCount++;
}
}
m_bCancel = true;
FinishProcess();
this.Close();
this.Dispose();
Application.Exit();
}

The problem is that the application remains on memory even after the Application.Exit() statement is executed.
Is it possible to do what I desire in a single thread?

Thank you.

Anderson T. Kubota
 
A

Alex Yakhnin [eMVP]

What are you doing in your worker thread MainProcess when
making a call to SetCancel?

-Alex
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com
"Check out our Compact Framework controls..."
-----Original Message-----
Hi!

I need to run a process that should take over 1 minute, for instance.
I desire to put a button that the user can cancel the process.
For this, I was using the Application.DoEvents in the
first process, and several times I verify the value of a
m_bCancel variable that informs when the user requests the
application to cancel.
This is the code I've being implementing on the cancel button is something like this:

private void btnCancel_Click(object sender, System.EventArgs e)
{
if(m_bRunning)
{
AddStatus("Process is being aborted...");
m_MainProcess.SetCancel(true);
int nCount = 0;
while(nCount < 5)
{
Application.DoEvents();
if(m_bRunning)
Thread.Sleep(1000);
else
break;
nCount++;
}
}
m_bCancel = true;
FinishProcess();
this.Close();
this.Dispose();
Application.Exit();
}

The problem is that the application remains on memory
even after the Application.Exit() statement is executed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top