【发布时间】:2011-07-30 03:43:42
【问题描述】:
我在使用时间时遇到内存不足异常。它是否与在结构中使用计时器有关 - 是否可以在结构中使用计时器,如下所示:
public struct SessionStruct
{
private bool _isElapsed;
public bool isElapsed
{
get{return _isElapsed;}
set { _isElapsed = value; }
}
public string sessionID
{
get;
set;
}
public DateTime time
{
get;
set;
}
public Timer timer
{ get; set; }
};
这是经过的事件处理程序:
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
req = "<Request><ID>1234567</ID><type>10</type><attribute>" + session1.sessionID + "|</attribute></Request>";}
[编辑]
这是实例化和设置值的代码 - 这只是其中的一部分......
public Request(string request, Database db)
{
//实例化 s = 新的 SessionStruct(); s.timer = 新计时器(60000); s.timer.Enabled = true; //db = 新数据库(); 尝试 { Debug.WriteLine("开始一个新的请求"); doc = new XmlDocument(); doc.LoadXml(请求); 字符串属性 = ""; //字符串会话ID = ""; ArrayList attributeArray = new ArrayList(); ArrayList queryArray = new ArrayList();
int iteration = 0;
bool hasSpecial = false ;
nodelist = doc.SelectNodes("/Request");
foreach (XmlNode xn in nodelist)
{
try
{
type = xn["type"].InnerText;
id = xn["ID"].InnerText;
attribute = xn["attribute"].InnerText;
Debug.WriteLine("Processing Request of type: " + type + "\nWith Id number: " + id + "\nWith attribute string: " + attribute);
for (int i = attribute.IndexOf('|'); i != -1; )
{
attributeArray.Add(attribute.Substring(0, i));
if (attribute.Substring(0, i).Contains('\''))
hasSpecial = true;
attribute = attribute.Substring(i + 1);
i = attribute.IndexOf('|');
}
//设置变量 if (type == "1" && attributeArray.Count != 2) { s.sessionID = attributeArray[0].ToString(); s.time = DateTime.Now; s.timer.Start(); isNotLogin = true; 属性数组.RemoveAt(0); } 否则如果(类型!=“1”) { s.sessionID = attributeArray[0].ToString(); s.time = DateTime.Now; s.timer.Start(); isNotLogin = true; 属性数组.RemoveAt(0); }
我在这里返回结构
public SessionStruct getSessionStruct()
{
return s;
}
它返回到另一个类,如:
sessStruct2 = iRequest.getSessionStruct();
int x;
for(x = 0; x< session.Count; x++)
{
sessStruct = (SessionStruct)session[x];
if (sessStruct.sessionID == sessStruct2.sessionID)
{
Debug.WriteLine("Resetting Session Timer");
session.RemoveAt(x);
sessStruct2.timer.Stop(); //resetting timer
sessStruct2.timer.Start();
Debug.WriteLine("Session Timer Reset SUCCESSFUL");
session.Add((object)sessStruct2);
Debug.WriteLine("Added Session "+sessStruct2.sessionID+" Successfully to sessions table");
guiServer.log("Added Session " + sessStruct2.sessionID + " Successfully to sessions table");
break;
}
}
if(x==session.Count)
{
session.Add(iRequest.getSessionStruct());
Debug.WriteLine("Added the session");
}
我创建了一个线程来检查计时器是否像这样过去
while (true)
{
if (session.Count > 0)
{
session1 = (SessionStruct)session[0];
session1.timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed)
}
}
这是堆栈跟踪:
first chance exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
System.Transactions 严重:0:http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionVNurseService.exeSystem.OutOfMemoryException,mscorlib,版本=4.0.0.0,文化= 中性,PublicKeyToken=b77a5c561934e089 引发了“System.OutOfMemoryException”类型的异常。在 System.MulticastDelegate.CombineImpl(代表跟随) 在 System.Timers.Timer.add_Elapsed(ElapsedEventHandler 值) 在 VNurseService.Server.Session.checkSessionList() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()System.OutOfMemoryException:引发了“System.OutOfMemoryException”类型的异常。 在 System.MulticastDelegate.CombineImpl(代表跟随) 在 System.Timers.Timer.add_Elapsed(ElapsedEventHandler 值) 在 VNurseService.Server.Session.checkSessionList() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() mscorlib.dll 中发生了“System.OutOfMemoryException”类型的未处理异常
我知道这很令人困惑,但我们正在创建伪会话,并且会话 ID 来自请求,当解析请求时,它会插入会话 ID 和时间,然后将计时器启动到结构中。然后将该结构返回到“主”类,然后对照会话数组列表中的其他会话进行检查。如果它在数组列表中,它将删除当前结构并在数组列表的末尾添加新结构。然后新线程检查arraylist,看看arraylist索引0处的会话是否已经过去。
我希望这是有道理的。如果有其他实现或方法,请告诉我。
谢谢。
【问题讨论】:
-
您的代码看起来不错,使用作为结构属性的计时器应该没有问题。您没有向我们展示您如何连接您的事件或创建计时器的实例。您也没有指定发生异常的确切代码行或完整的异常详细信息,包括堆栈跟踪和错误消息。
-
可变结构是EVIL!
-
您没有显示结构是如何初始化的以及如何创建计时器的。
-
@SLaks:愿意提供链接吗? (不,我没有谷歌:p)
标签: c# struct out-of-memory