我从这行代码中收到此异常“由于内存不足异常,函数求值被禁用”。
this.pbErrorSign.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
我实际上添加了背景图像和许多其他图像,如警告图像和图片框,而不是按钮,以制作有吸引力的 GUI。程序前段时间运行良好,现在它给了我这个....请帮助
以下代码来自设计师。
this.pbErrorSign.BackColor = System.Drawing.Color.Transparent;
this.pbErrorSign.BackgroundImage = global::SAMS.Properties.Resources.ErrorSign3;
this.pbErrorSign.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.pbErrorSign.Location = new System.Drawing.Point(69, 121);
this.pbErrorSign.Name = "pbErrorSign";
this.pbErrorSign.Size = new System.Drawing.Size(30, 30);
this.pbErrorSign.TabIndex = 1;
this.pbErrorSign.TabStop = false;
以下是名为errorDialogForm的窗体代码
public partial class ErrorDialogForm : Form
{
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Capture = true;
}
public ErrorDialogForm()
{
InitializeComponent();
}
public string LabelText
{
get
{
return this.lblError.Text;
}
set
{
this.lblError.Text = value;
}
}
private void pbOkButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void pbOkButton_MouseEnter(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.purpleOkButton));
}
private void pbOkButton_MouseLeave(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.blueOkButton));
}
private void ErrorDialogForm_Enter(object sender, EventArgs e)
{
this.Close();
}
private void ErrorDialogForm_Deactivate(object sender, EventArgs e)
{
this.Close();
}
private void ErrorDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
this.Parent = null;
e.Cancel = true;
}
}
最佳答案
The function evaluation was disabled because of an out of memory exception
这是一个调试器通知,它只是告诉您它不会向您显示任何内容,因为程序因 OOM 而崩溃。发生这种情况时,调试器也很可能会崩溃。 真正的问题是您遇到的导致调试器停止程序的 OOM 异常。
this.pbOkButton.BackgroundImage = Properties.Resources.purpleOkButton;
这就是导致崩溃的语句。当您四处移动鼠标时,您可以在非常频繁触发的事件中执行此操作。不太明显的是,此语句创建了一个新 位图对象。旧的没有被处理掉。这使得您的程序的内存使用率迅速攀升,垃圾收集器可以对此做任何事情的可能性很小,因为您没有分配任何其他对象。 OOM kaboom 是不可避免的。
正确的解决方法是只创建这些位图一次:
private Image purpleOk;
private Image blueOk;
public ErrorDialogForm()
{
InitializeComponent();
purpleOk = Properties.Resources.purpleOkButton;
blueOk = Properties.Resources.blueOkButton;
pbOkButton.BackgroundImage = blueOk;
}
private void pbOkButton_MouseEnter(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = purpleOk;
}
private void pbOkButton_MouseLeave(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = blueOk;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
purpleOk.Dispose();
blueOk.Dispose();
base.OnFormClosed(e);
}
关于c# - 由于内存不足异常,函数评估被禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312671/