Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone
I have a window form containing the following code
C#
private void button2_Click(object sender, EventArgs e)
         {
            //backgroundWorker1.WorkerSupportsCancellation = true;
            try
            {

                DialogResult result = MessageBox.Show("Are you sure you want to stop broadcast?", "Confirm", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    System.GC.Collect();
                    if (this.obj.backgroundWorker1.IsBusy)
                    {
                        this.obj.backgroundWorker1.CancelAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                //this.obj.maintainlog("Button Stop : " + ex.Message);
                this.obj.maintainlog("Button Stop : " + ex.StackTrace);
            }
        }


Now while I am clicking the button I am getting the following Error
Attempted to read or write protected memory. This is often an indication that other memory has been corrupted
And stack trace is :

at System.Windows.Forms.SafeNativeMethods.MessageBox(HandleRef hWnd, String text, String caption, Int32 type)
   at System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp)
   at System.Windows.Forms.MessageBox.Show(String text, String caption, MessageBoxButtons buttons)
   at FORMBROADCASTER.Form1.button2_Click(Object sender, EventArgs e) in D:\CM Trade Application\FORMBROADCASTER\Form1.cs:line 52 


I have googled a lot but not able to find any thing
Posted
Updated 2-Aug-11 19:45pm
v3
Comments
BillWoodruff 3-Aug-11 3:18am    
Which version of the .NET FrameWork does your project target ? And, is the code that calls 'MessageBox.Show executing on the main ui thread ? Have you tried using a custom form, using ShowModal, instead of MessageBox.Show ?

1 solution

When I try exactly that in my code, it doesn't cause a problem.
C#
BackgroundWorker bw;
private void button1_Click(object sender, EventArgs e)
    {
    bw = new BackgroundWorker();
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerAsync();
    }
void bw_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    int i = 0;
    while (true)
        {
        if (worker.CancellationPending == true)
            {
            e.Cancel = true;
            break;
            }
        else
            {
            // Perform a time consuming operation and report progress.
            Console.WriteLine(i++);
            System.Threading.Thread.Sleep(500);
            }
        }
    }
private void button2_Click(object sender, EventArgs e)
    {
    try
        {
        DialogResult result = MessageBox.Show("Are you sure you want to stop broadcast?", "Confirm", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
            {
            System.GC.Collect();
            if (bw.IsBusy)
                {
                bw.CancelAsync();
                }
            }
        }
    catch (Exception ex)
        {
        Console.WriteLine("Button Stop : " + ex.StackTrace);
        }
    }
So, either the problem is in your background worker code, or your button handler is not being called on the UI thread.
Some questions:

Why are you explicitly calling the Garbage Collector before you try to stop the worker? Ignoring "why are you calling the GC at all", surely any GC would be better done once the worker is dead and it's resources released?

What is the worker actually doing?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900