错误码为0xc0000005.此错误可能是cl"中的错误

本文关键字:错误 cl quot 误码 0xc0000005 | 更新日期: 2023-09-27 18:02:22

让我先解释一下我的程序和正在发生的事情。我的程序使用GSM调制解调器从移动卡中读取消息,然后在Marquee中显示消息…这个错误真的是随机的,我在一个朋友的迪斯科舞厅测试了这个程序,有时它可以整夜工作而不会崩溃,有时它会一次又一次地崩溃……所以,有一个类的代码做Marquee(这是它崩溃的地方):

public class MyPanel : System.Windows.Forms.Panel
{
    private LinkedList<string> texto;
    private LinkedList<MensagemParaEcra> msgs;
    private LinkedList<Label> labels;
    private LinkedList<string> msgsRemover = new LinkedList<string>();
    private List<Label> labelsRmv = new List<Label>();
    private List<Label> labelARemover = new List<Label>();
    private Label firstLb;
    private Label lastLb;
    private Label leftLb;
    private Label lastLb3;
    private Color color;
    private Color backg;
    private Font textFont;
    private Label lastLb2;
    private Screen screen;
    private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    public MyPanel(Color corLabel, Color back, Font text)
    {
        this.color = corLabel;
        this.backg = back;
        this.textFont = text;
        this.Width = 4000;
        texto = new LinkedList<string>();
        msgs = new LinkedList<MensagemParaEcra>();
        labels = new LinkedList<Label>();
        MouseMove += MyPanel_MouseMove;
        this.BackColor = backg;
        this.Size = new Size(4000, 69);
        this.Refresh();
        Screen[] screens = Screen.AllScreens;
        screen = Screen.FromControl(BarraSms.getInstance());
    }
    public void addMensagem(MensagemParaEcra msg) // add a message to be displayed in the monitor
    {
        Label lbl = new Label();
        lbl.Text = (msg.getTexto() + " -");
        lbl.ForeColor = color;
        lbl.Font = textFont;
        lbl.BackColor = backg;
        lbl.Visible = true;
        lbl.AutoSize = true;
        if (labels.Count > 0)
        {
            lbl.Location = new Point(this.lastLb3.Right, 0);
        }
        else
        {
            lbl.Location = new Point(screen.Bounds.Width + 10, 0);
            firstLb = lbl;
        }
        reorderAdd(lastLb3, lbl);
        labels.AddLast(lbl);
        this.Controls.Add(lbl);
        this.Refresh();
        this.lastLb3 = lbl;
    }
    private void reorderAdd(Label ultima, Label lbl) //reorder the list order when the user add a new message
    {
        var it = labels.OrderBy(x => x.Location.X);
        int count;
        for (int i = 0; i < labels.Count; i++)
        {
            if (it.ElementAt(i) == ultima)
            {
                count = i;
                lbl.Location = new Point(ultima.Right, 0);
                try
                {
                    if (it.ElementAt(i + 1) != null)
                        it.ElementAt(i + 1).Location = new Point(lbl.Right, 0);
                }catch{
                }
                for (int a = count+2; a < labels.Count; a++)
                {
                    if(it.ElementAt(a) != null)
                    it.ElementAt(a).Location = new Point(it.ElementAt(a-1).Right, 0);
                }
                return;
            }
        }
    }

    public void startT()
    {
        startThread();
    }
    public void removerMsg(string msg) //remove the message
    {
        string remover = msg + " -";  
        Label lb = getMsg(remover);
        labels.Remove(lb);
        this.Controls.Remove(lb); 
        this.Refresh();
        if(labels.Count >1)
        reorder();
    }
    private Label getMsg(string msg) //method to find a message in the list
    {
        var node = labels.First;
        while (node != null)
        {            
            var nextNode = node.Next;
            if (node.Value.Text.Equals(msg))
            {
                return node.Value;
            }
            node = nextNode;
        }
        return null;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.ResumeLayout(false);
    }
    public void setVelocidade(int ve) { //used to set the interval of the timmer
        myTimer.Interval = ve;
    }
    private void startThread()
    {
        myTimer.Tick += new EventHandler(timerEvent);
        myTimer.Interval = 20;
        myTimer.Start();
    }
    private void MyPanel_MouseMove(object sender, MouseEventArgs e) //used to move the unborded window
    {
        BarraSms.getInstance().mouseMove(e);
    }
    [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute()] //I found this when I was searching for the error but it didn't help me
    private void timerEvent(object sender, EventArgs e)
    {
           var it4 = labels.OrderBy(x => x.Location.X);
           // var it = it4.GetEnumerator();
            //while (it.MoveNext())
            foreach(Label lb in it4)
            {
              //  Label lb = it.Current; //sometimes it crashes here
                if (firstLb == null)
                    firstLb = lb;
                if (lb.Location.X + lb.Width < 0) {
                    lb.Location = new Point(it4.Last().Right, 0);
                }
                else
                {
                    lb.Location = new Point(lb.Location.X - 4, lb.Location.Y);
                    leftLb = lb;
                }
                lastLb = lb;
            }
            this.Refresh(); //other times it crashes here
            if(labels.Count >2 )
            reorder();
    }
    private void reorder() { //used to reorder my list by the location on the screen
        var it2 = labels.OrderBy(x => x.Location.X);
        Label orderLb = it2.ElementAt(0);
        for (int i = 1; i < labels.Count; i++)
        {
            it2.ElementAt(i).Location = new Point(orderLb.Right, 0);
            orderLb = it2.ElementAt(i);
        }
    }
    public void applyDestaque(string msg) { //its just to highlight a message
        Label lb = getMsg(msg + " -");
        labels.Find(lb).Value.BackColor = Color.Yellow;
        labels.Find(lb).Value.ForeColor = Color.Black;
    }
    public void removeDestaque(string msg) { // remove the highlight
        Label lb = getMsg(msg + " -");
        labels.Find(lb).Value.BackColor = backg;
        labels.Find(lb).Value.ForeColor = color;
    }
    internal void addMensagemDestaque(MensagemParaEcra mensagemParaEcra)  // add a highlighted message
    {
        Label lbl = new Label();
        lbl.Text = (mensagemParaEcra.getTexto() + " -");
        lbl.ForeColor = Color.Black;
        lbl.Font = textFont;
        lbl.BackColor = Color.Yellow;
        lbl.Visible = true;
        lbl.AutoSize = true;
        if (labels.Count > 0)
        {
            lbl.Location = new Point(this.lastLb3.Right, 0);
        }
        else
        {
            lbl.Location = new Point(screen.Bounds.Width + 10, 0);
            firstLb = lbl;
        }
        reorderAdd(lastLb3, lbl);
        labels.AddLast(lbl);
        this.Controls.Add(lbl);
        this.Refresh();
        this.lastLb3 = lbl; 
    }
}

好了,就这些……

我一遍又一遍地搜索,但我无法修复它或找到任何解决方案…编辑:到目前为止,我所做的更改已在我的代码中更新,我注释了线程内的行,并添加了您告诉我要做的foreach循环

完整错误:

运行时遇到致命错误。错误的地址在0x5f44a62c,线程0x2040。错误码为0xc0000005。此错误可能是CLR中的错误,也可能是不安全或不可验证的错误部分用户代码。此错误的常见来源包括userCOM-interop或PInvoke的封送错误,这可能会损坏堆栈。

编辑:添加赏金给谁可以帮助我解决这个错误…

错误码为0xc0000005.此错误可能是cl"中的错误

从它的文档看来,你正在使用的属性需要一个[SecurityCritical]才能工作。来自文档:

The CLR delivers the corrupted process state exception to applicable exception clauses only in methods that have both the HandleProcessCorruptedStateExceptionsAttribute and SecurityCriticalAttribute attributes .

你可以尝试添加[SecurityCritical]属性,并在方法中捕获任何异常并记录它们,看看它是否给你一些更多的信息