本文介绍了单击按钮时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好
我有一个包含以下代码的窗口形式

Hello Everyone
I have a window form containing the following code

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 



我已经在Google上搜索了很多,但找不到任何东西.



I have googled a lot but not able to find any thing

推荐答案

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);
        }
    }

因此,问题可能出在您的后台工作程序代码中,或者未在UI线程上调用您的按钮处理程序.
一些问题:

在尝试停止该工作程序之前,为什么要明确致电垃圾回收器?忽略为什么要调用GC",一旦工人死了并且释放了资源,肯定可以更好地完成任何GC吗?

工人实际上在做什么?

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?


这篇关于单击按钮时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:55