c#长时间循环时内存泄露问题

quilt1120p quilt1120p 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想实现的功能是 ,按一下键程序执行循环,直到按停止键为止
但是如果长时间不按停止按钮,程序会越来越慢,直到电脑内存耗尽。问题应该在while循环中、求高人帮忙解决这问题,不胜感激!。


这是开始键的程序

private void StartCalcButton_Click(object sender, EventArgs e)
{

string printbox = string.Empty;
string challengetext=string.Empty;
StopFlag = false;
if (ScLboxDataValue>EcLboxDataValue)
{
this.PrintBox.Items.Clear();
this.PrintBox.Items.Add(" Start Challenge is larger than End Challenge");
this.PrintBox.SelectedIndex = this.PrintBox.Items.Count - 1;
return;
}
ValueCompare compare = new ValueCompare();
Encryption5Byte a = new Encryption5Byte();
Encryption10Byte b = new Encryption10Byte();
while ((compare.ChallengeChangeValueCompareAction(this) != ">") && (StopFlag == false))
{
StopCalcButton.Enabled = true;
StartCalcButton.Enabled = false;
ClearButton.Enabled = false;
ResetButton.Enabled = false;
parameter.Enabled = false;
KeyLengthSelect.Enabled = false;
ClearButton.Focus();
Challenge[0] = Convert.ToByte((byte)counter1);
Challenge[1] = Convert.ToByte((byte)(counter1 >> 8));
Challenge[2] = Convert.ToByte((byte)(counter1 >> 16));
Challenge[3] = Convert.ToByte((byte)(counter1 >> 24));
Challenge[4] = Convert.ToByte((byte)(counter1 >> 32));
if (_5Byteskey.Checked == true)
{
a.GetSignatureBufAction1(Challenge, KeyPage4boxDataBuf, Signature);
}
else
{
b.GetSignatureBufAction2(Challenge, Signature, KeyPage4boxDataBuf, KeyPage5boxDataBuf);
}
for (int j = 4; j >= 0; j--)
{
if (Challenge[j] < 16)
challengetext += "0" + Convert.ToString(Challenge[j], 16) + " ";
else
challengetext += Convert.ToString(Challenge[j], 16) + " ";

}
for (int i = 2; i >= 0; i--)
{
if (Signature[i] < 16)
printbox += " " + "0" + Convert.ToString(Signature[i], 16);
else
printbox += " " + Convert.ToString(Signature[i], 16);
}
//ListBox表示
printbox += " " + challengetext;
this.PrintBox.Items.Add(printbox.ToUpper());
this.PrintBox.SelectedIndex = this.PrintBox.Items.Count - 1;
printbox = null;
challengetext = null;
Application.DoEvents();
counter1++;
GC.Collect();
// GC.WaitForPendingFinalizers();
//Dispose();
//GC.SuppressFinalize(this);
}
StartCalcButton.Enabled = true;
ClearButton.Enabled = true;
ResetButton.Enabled = true;
StopCalcButton.Enabled = false;
if (StopFlag == true)
{
this.PrintBox.Items.Add(" Calculation stoped,the counter will not be reset");
this.PrintBox.SelectedIndex = this.PrintBox.Items.Count - 1;

}
if (StopFlag == false)
{
parameter.Enabled = true;
KeyLengthSelect.Enabled = true;
counter1 = ScLboxDataValue;
this.PrintBox.Items.Add(" Calculation complete");
this.PrintBox.SelectedIndex = this.PrintBox.Items.Count - 1;
}
return;
}


这是停止键程序

private void StopCalcButton_Click(object sender, EventArgs e)
{
StopFlag = true;
}

...全文

相关推荐

C# 测试内存泄露.zip

小巧实用的内存测试的小工具,内涵C#源码

tdtclls

Python跑循环时内存泄露的解决方法

主要介绍了Python跑循环时内存泄露的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38699492

C# 定时器保活机制引起的内存泄露问题解决

主要介绍了C# 定时器保活机制引起的内存泄露问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38705014

检测内存泄露,让自己清楚哪里出问题了

检测内存泄露,让自己清楚哪里出问题了。 以免内存泄露了都不清楚

xjlshowup

js内存泄露的几种情况详细探讨

内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束。在C++中,因为是手动管理内存,内存泄露是经常出现的事情。而现在流行的C#和Java等语言采用了自动垃圾回收方法管理内存,正常使用的情况下几乎不会发生内存泄露。浏览器中也是采用自动垃圾回收方法管理内存,但由于浏览器垃圾回收方法有bug,会产生内存泄露。1、当页面中元素被移除或替换时,若元素绑定的事件仍没被移除,在IE中不会作出恰当处理,此时要先手工移除事件,不然会存在内存泄露。 代码如下: <div id=”myDiv”> &lt;input type=”button” value=”Click me” id=”myBtn”&gt;

weixin_38623707
评论 14
收藏 已收藏
分享
返回顶部
quilt1120p
C# 111,129 成员 642,538 内容