<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # volatile(C# 參考) **volatile** 關鍵字指示一個字段可以由多個同時執行的線程修改。聲明為 **volatile** 的字段不受編譯器優化(假定由單個線程訪問)的限制。這樣可以確保該字段在任何時間呈現的都是最新的值。 **volatile** 修飾符通常用于由多個線程訪問但不使用 [lock](https://msdn.microsoft.com/zh-CN/library/c5kehkcz.aspx) 語句對訪問進行序列化的字段。 **volatile** 關鍵字可應用于以下類型的字段: * 引用類型。 * 指針類型(在不安全的上下文中)。請注意,雖然指針本身可以是可變的,但是它指向的對象不能是可變的。換句話說,您無法聲明“指向可變對象的指針”。 * 類型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。 * 具有以下基類型之一的枚舉類型:byte、sbyte、short、ushort、int 或 uint。 * 已知為引用類型的泛型類型參數。 * [IntPtr](https://msdn.microsoft.com/zh-CN/library/system.intptr.aspx) 和 [UIntPtr](https://msdn.microsoft.com/zh-CN/library/system.uintptr.aspx)。 可變關鍵字僅可應用于類或結構字段。不能將局部變量聲明為 **volatile**。 下面的示例說明如何將公共字段變量聲明為 **volatile**。 ``` class VolatileTest { public volatile int i; public void Test(int _i) { i = _i; } } ``` 下面的示例演示如何創建輔助線程,并用它與主線程并行執行處理。有關多線程處理的背景信息,請參見[Managed Threading](https://msdn.microsoft.com/zh-CN/library/3e8s7xdd.aspx)和[線程處理(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/ms173178.aspx)。 ``` using System; using System.Threading; public class Worker { // This method is called when the thread is started. public void DoWork() { while (!_shouldStop) { Console.WriteLine("Worker thread: working..."); } Console.WriteLine("Worker thread: terminating gracefully."); } public void RequestStop() { _shouldStop = true; } // Keyword volatile is used as a hint to the compiler that this data // member is accessed by multiple threads. private volatile bool _shouldStop; } public class WorkerThreadExample { static void Main() { // Create the worker thread object. This does not start the thread. Worker workerObject = new Worker(); Thread workerThread = new Thread(workerObject.DoWork); // Start the worker thread. workerThread.Start(); Console.WriteLine("Main thread: starting worker thread..."); // Loop until the worker thread activates. while (!workerThread.IsAlive) ; // Put the main thread to sleep for 1 millisecond to // allow the worker thread to do some work. Thread.Sleep(1); // Request that the worker thread stop itself. workerObject.RequestStop(); // Use the Thread.Join method to block the current thread // until the object's thread terminates. workerThread.Join(); Console.WriteLine("Main thread: worker thread has terminated."); } // Sample output: // Main thread: starting worker thread... // Worker thread: working... // Worker thread: working... // Worker thread: working... // Worker thread: working... // Worker thread: working... // Worker thread: working... // Worker thread: terminating gracefully. // Main thread: worker thread has terminated. } ``` ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-CN/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [C# 參考](https://msdn.microsoft.com/zh-CN/library/618ayhy6.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-CN/library/67ef8sbd.aspx) [C# 關鍵字](https://msdn.microsoft.com/zh-CN/library/x53a06bb.aspx) [修飾符(C# 參考)](https://msdn.microsoft.com/zh-CN/library/6tcf2h8w.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看