• C#的Thread类


    概念

    创建和控制线程,设置其优先级并获取其状态。

    示例

    using System;
    using System.Threading;
    
    // Simple threading scenario:  Start a static method running
    // on a second thread.
    public class ThreadExample {
        // The ThreadProc method is called when the thread starts.
        // It loops ten times, writing to the console and yielding 
        // the rest of its time slice each time, and then ends.
        public static void ThreadProc() {
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }
    
        public static void Main() {
            Console.WriteLine("Main thread: Start a second thread.");
            // The constructor for the Thread class requires a ThreadStart 
            // delegate that represents the method to be executed on the 
            // thread.  C# simplifies the creation of this delegate.
            Thread t = new Thread(new ThreadStart(ThreadProc));
    
            // Start ThreadProc.  Note that on a uniprocessor, the new 
            // thread does not get any processor time until the main thread 
            // is preempted or yields.  Uncomment the Thread.Sleep that 
            // follows t.Start() to see the difference.
            t.Start();
            //Thread.Sleep(0);
    
            for (int i = 0; i < 4; i++) {
                Console.WriteLine("Main thread: Do some work.");
                Thread.Sleep(0);
            }
    
            Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
            t.Join();
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }

    注解

    进程启动时,公共语言运行时将自动创建单个前台线程以执行应用程序代码。 除了此主前台线程,进程还可以创建一个或多个线程来执行与进程关联的程序代码的一部分。 这些线程可以在前台或后台执行。 此外,还可以使用 ThreadPool 类来执行由公共语言运行时管理的工作线程上的代码。

  • 相关阅读:
    Laya页面嵌套和Scene.destory导致的Bug
    Laya的滚动容器Panel+HBox
    Laya的对象唯一标识
    Android自带的TTS功能
    一步一步学android之控件篇——ListView基本使用
    android surfaceView 的简单使用 画图,拖动效果
    Android 数据分析系列一:sharedPreferences
    Android Service总结
    android中碰撞屏幕边界反弹问题
    Android开发:setAlpha()方法和常用RGB颜色表----颜色, r g b分量数值(int), 16进制表示 一一对应
  • 原文地址:https://www.cnblogs.com/Tpf386/p/12109307.html
Copyright © 2020-2023  润新知