简体   繁体   English

C#中的线程监视器类

[英]Thread Monitor class in c#

In my c# application multiple clients will access the same server, to process one client ata a time below code is written.In the code i used Moniter class and also the queue class.will this code affect the performance.if i use Monitor class, then shall i remove queue class from the code. 在我的C#应用​​程序中,多个客户端将访问同一台服务器,以一次在下面的代码编写时处理一个客户端。在代码中,我使用了Moniter类以及队列类。该代码会影响性能。如果我使用Monitor类,那我应该从代码中删除队列类。

Sometimes my remote server machine where my application running as service is totally down.is the below code is the reasond behind, coz all the clients go in a queue, when i check the netstatus -an command using command prompt, for 8 clients it shows 50 connections are holding in Time-wait... 有时我的应用程序作为服务运行的远程服务器计算机完全关闭。下面的代码是后面的原因,因为当我使用命令提示符检查netstatus -an命令时,对于所有8个客户端,所有客户端进入队列50个连接正在等待中...

Below is my code where client acces the server ... 以下是我的客户端访问服务器的代码...

if (Id == "")
{
    System.Threading.Monitor.Enter(this);
    try
    {
        if (Request.AcceptTypes == null)
        {
            queue.Enqueue(Request.QueryString["sessionid"].Value);

            string que = "";

            que = queue.Dequeue();
            TypeController.session_id = que;
            langStr = SessionDatabase.Language;
            filter = new AllThingzFilter(SessionDatabase, parameters, langStr);
            TypeController.session_id = "";

            filter.Execute();
            Request.Clear();

            return filter.XML;
        }
        else
        {
            TypeController.session_id = "";
            filter = new AllThingzFilter(SessionDatabase, parameters, langStr);

            filter.Execute();
        }
    }
    finally
    {
        System.Threading.Monitor.Exit(this);
    }
}

Locking this is pretty wrong, it won't work at all if every thread uses a different instance of whatever class this code lives in. It isn't clear from the snippet if that's the case but fix that first. 锁定是完全错误的,如果每个线程使用此代码所驻留的类的不同实例,则根本无法使用。从代码段中尚不清楚是否是这种情况,但请先解决。 Create a separate object just to store the lock and make it static or give it the same scope as the shared object you are trying to protect (also not clear). 创建一个单独的对象只是为了存储该锁并使它成为静态,或者赋予它与您要保护的共享对象相同的作用域(也不清楚)。

You might still have trouble since this sounds like a deadlock rather than a race. 您可能仍然遇到麻烦,因为这听起来像是僵局,而不是比赛。 Deadlocks are pretty easy to troubleshoot with the debugger since the code got stuck and is not executing at all. 死锁很容易用调试器进行故障排除,因为代码被卡住了并且根本没有执行。 Debug + Break All, then Debug + Windows + Threads. 调试+全部破坏,然后调试+ Windows +线程。 Locate the worker threads in the thread list. 在线程列表中找到工作线程。 Double click one to select it and use Debug + Call Stack to see where it got stuck. 双击一个将其选中,然后使用“调试+调用堆栈”查看其卡住的位置。 Repeat for other threads. 重复其他线程。 Look back through the stack trace to see where one of them acquired a lock and compare to other threads to see what lock they are blocking on. 回顾堆栈跟踪,以查看其中一个在何处获取了锁,并与其他线程进行比较以查看它们在锁上锁定了什么。

That could still be tricky if the deadlock is intricate and involves multiple interleaved locks. 如果死锁错综复杂并且涉及多个交错锁,那仍然可能很棘手。 In which case logging might help. 在这种情况下,日志记录可能会有所帮助。 Really hard to diagnose mandelbugs might require a rewrite that cuts back on the amount of threading. 诊断Mandelbug真的很困难,可能需要重写以减少线程数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM