. Net 2 Tip: capture the comparison between CSE and Thread. Timer and Thread. Sleep

Source: Internet
Author: User
  • How to capture AccessViolationException in. Net
    In. net4.0, some SEH Exceptions in the system are not captured by default. These Exceptions are called failed upted State Exceptions (CSE)
    For example, when calling unmanaged code, such errors often occur, such as "memory unreadable/writable ".
    The following is an article on ms msdn that details the CSE exception:
    Http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035

    It is not recommended for MS to capture such exceptions because such exceptions are not resolved and applications may cause more serious errors. we recommend that you restart the application. the causes of such exceptions often need to be resolved. instead of simply capturing. so. NET4.0. NET ). however, sometimes we need to capture such exceptions, or call a third-party library. we cannot completely solve this problem .. NET4.0 provides the following methods for capturing:

    // This program runs as part of an automatic test system so you need
    // To prevent the normal Unhandled Exception behavior (Watson dialog ).
    // Instead, print out any exceptions and exit with an error code.
    [HandleProcessCorruptedStateExceptions]
    Public static int Main ()
    {
    Try
    {
    // Catch any exceptions tions leaking out of the program CallMainProgramLoop ();
    }
    Catch (Exception e)
    // We cocould be catching anything here
    {
    // The exception we caught cocould have been a program error
    // Or something much more serious. Regardless, we know that
    // Something is not right. We'll just output the exception
    // And exit with an error. We won't try to do any work when
    // The program or process is in an unknown state!

    System. Console. WriteLine (e. Message );
    Return 1;
    }

    Return 0;

    }

  • Use Timer or Thread. Sleep
    When you need to execute a method for a period of time, do you want to commit in method 1 and method 2?
    Method 1.
    Thread thread = new Thread () => {
    Thread. Sleep (millisecond );
    Action ();
    });
    Thread. IsBackground = true;
    Thread. Start ();

    Method 2:
    Timer timer = new Timer (o => action (), null, millisecond,-1 );

    If you only need to execute a method for a period of time, timer is recommended.
    The following ms mvp seriously criticized the use of Thread. Sleep.
    Http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

    This person thinks that if you use thread. sleep in the code, this code is poorly designed.The Thread. Sleep Application Scenario only simulates the execution time of an operation during testing and debugging.
    1 ). thread consumption is very high. It takes about 200,000 cpu cycles to create a Thread and destroy it in 100,000 cycles. the timer method is the thread of the thread pool inside the process. threadPool is also used. queueUserWorkItem is better than a new thread.
    2). thread. sleep cannot solve the problem, but it will make the problem more difficult to reproduce.
    2). After creating a thread, for example, closing the form, the thread needs to force exit when exiting. This is also inconvenient. On the contrary, timer is used.

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.