【问题标题】:OutOfMemoryException using timer elapsed event c#OutOfMemoryException 使用计时器经过的事件 c#
【发布时间】: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处的会话是否已经过去。

我希望这是有道理的。如果有其他实现或方法,请告诉我。

谢谢。

【问题讨论】:

标签: c# struct out-of-memory


【解决方案1】:

是的,您可以让Timer(或任何其他类型)成为struct 的成员。顺便说一句,您应该强烈考虑将其更改为class(您将其设为struct 有什么特别的原因吗?)或从您的属性中删除setter;可变结构,就像你在这里看到的一样,通常被认为是纯邪恶的。

这里并没有太多可以真正说明您的环境以及可能导致您内存不足的任何内容。您能否进一步说明您是如何使用struct

问题编辑后编辑

您不应该像现在一样重复附加到计时器的事件。您的 while 循环将继续附加计时器处理程序的副本,这可能是您内存不足的地方。

【讨论】:

  • 非常感谢。但是,我现在不应该得到一个参数超出范围异常?我不知道我是否可以在这里问另一个问题,但我也许
  • @Jonathan:您可能应该提出一个新问题。如果它回答了您的问题,请不要忘记将此答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多