. NET tracking thread suspend and program dead loop

Source: Internet
Author: User
Tags assert memory usage

program debugging under. Net is a lot simpler and less of a problem than the headaches of the pointer crossing the line. However, when your program encounters the following problems, it is still very tricky:

1. The process terminates abnormally. Solutions see unhandled exceptions under. Net

2. The program has not been released after a memory leak or memory request. solutions see Using. NET Memory Profiler to track. NET application memory usage-Basic application. If the program is monitored by myself, I will explain it in a later article.

3. Threads are suspended for unknown reasons, such as deadlocks.

4. Program dead Loop.

This article will explain if the program to write the two fault real-time tracking and reporting.

First, we need a separate monitoring thread to monitor the threads that need to be monitored.

I made a monitor class Threadmonitor, before we started monitoring, we set the monitoring thread to the highest priority.

        public ThreadMonitor()
        {
            _MonitorThread = new Thread(new ThreadStart(MonitorTask));
            _MonitorThread.Priority = ThreadPriority.Highest;
            _MonitorThread.IsBackground = true;

        }

Next we provide several common methods for this thread

The Start method allows the caller to initiate the monitoring

The Register method is used to register the threads that need to be monitored into the monitoring list

The Heartbeat method is followed by a description

/**////<summary>
///Start monitor
///</summary>
Public         void Start ()
{
_monitorthread.start ();
       


/**////<summary>
///Monitor Register
///</summary>
<param name= "Monitorpara" >monitor parameter</param>
public void Register (Monitorparameter             Monitorpara)
{
Debug.Assert (Monitorpara!= null);
Debug.Assert (monitorpara.thread!= null);

if (GETTCB (monitorpara.thread)!= null)
{
throw new System.argu Mentexception ("Register repeatedly!");
}

Lock (_registerlock)
{
_tcbtable.add (monitorpa Ra. Thread.managedthreadid, New TCB (Monitorpara));  
}
}

public void Heartbeat (Thread t)
       {
TCB TCB = GETTCB (t);
if (TCB = null)
{
throw new System.ArgumentException ("This thread WA s not registered! ");
}

TCB. Lastheartbeat = DateTime.Now;
TCB. Hittimes = 0;
TCB. Status &= ~threadstatus.hang;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.