zoukankan      html  css  js  c++  java
  • 使用线程时需要注意的地方

     1 Thread mThread = null;
     2         int mnCnt = 50;
     3 
     4         public FrmTest()
     5         {
     6             InitializeComponent();
     7 
     8             mThread = new Thread(new ThreadStart(
     9                         delegate
    10                         {
    11                             while (true)
    12                             {
    13                                 Thread.Sleep(100);
    14                                 Debug.WriteLine(Environment.TickCount.ToString());
    15 
    16                                 if (mnCnt-- < 0)
    17                                 {
    18                                     break;
    19                                 }
    20                             }
    21                             //方式1:
    22                             if (true)
    23                             {
    24                                 Stop();
    25                             }
    26 
    27                             //方式2:
    28                             if (false)
    29                             {
    30                                 Thread t = new Thread(new ThreadStart(
    31                                     delegate
    32                                     {
    33                                         Stop();
    34                                     }));
    35                                 t.Start();
    36                             }
    37                         }));
    38             mThread.Start();
    39         }
    40 
    41         private void Stop()
    42         {
    43             try
    44             {
    45                 mThread.Abort();
    46             }
    47             catch (Exception ex)
    48             {
    49                 Debug.WriteLine(ex.ToString());
    50 
    51                 //使用方式1时,当执行到这里时,下面代码不会再执行
    52             }
    53 
    54             //其他代码
    55             //...
    56             Debug.WriteLine("停止");
    57         }

    //在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常
    //“Test201102051346.vshost.exe”(托管(v4.0.30319)): 已加载“C:WindowsMicrosoft.NetassemblyGAC_MSILmscorlib.resourcesv4.0_4.0.0.0_zh-Hans_b77a5c561934e089mscorlib.resources.dll”
    //System.Threading.ThreadAbortException: 正在中止线程。
    //在 System.Threading.Thread.AbortInternal()
    //在 System.Threading.Thread.Abort()
    //在 Test201102051346.FrmTest.Stop() 位置 D:UsersWagweiDesktopTest201102051346Test201102051346FrmTest.cs:行号 62
    //在 System.Threading.ThreadAbortException 中第一次偶然出现的“Test201102051346.exe”类型的异常
    //“System.Threading.ThreadAbortException”类型的异常在 Test201102051346.exe 中发生,但未在用户代码中进行处理

  • 相关阅读:
    jQuery选择器---层次选择器总结
    jQuery选择器---基本选择器总结
    jQuery手风琴
    jQuery(ajax)的使用方法
    css 3d 基础知识
    Android Dynamic Action(动态Action)—像访问网页一样地访问Activity
    笨鸟不乖 是这么设计Android项目架构的
    Android Auto Scroll ViewPager (Smooth)
    [原创] 在线音乐API的研究 (Part 2.1)
    [原创] 浅谈开源项目Android-Universal-Image-Loader(Part 3.1)
  • 原文地址:https://www.cnblogs.com/wang_xy/p/12050104.html
Copyright © 2011-2022 走看看