???????1.
---------------------------
?/// <summary>??????? /// 用來判斷是否停止線程的變量??????? /// </summary>??????? private? volatile bool m_stopThread=false;??????? private Thread m_threadStartSendMusic;
??????? /// <summary>??????? /// 開始發送??????? /// </summary>??????? public void SendMusicInThread(int startcount,int sendcount,string sbip)??????? {??????????? //若有在發送數據 停止發送??????????? m_stopThread = true;??????????? Thread.Sleep(106);????????? ?????????? ??????????? m_StartCount = startcount;??????????? m_SendCount = sendcount;??????????? m_sbip = sbip;????????????//新開一個線程發送數據 ??????????? m_threadStartSendMusic = new Thread(SendMusic);??????????? m_threadStartSendMusic.IsBackground = true;??????????? m_threadStartSendMusic.Start();??????? }
??????? /// <summary>??????? /// 發送音樂??????? /// </summary>??????? private? void SendMusic()??????? {??????????? m_stopThread = false;????????? ??????????? int endCount = m_StartCount + m_SendCount;??????????? for (int i = m_StartCount; i < endCount; i++)??????????? {??????????????? if (m_stopThread)??????????????? {??????????????????? break;??????????????? }
???????????? //do something ?????????????}
????? }
----------------------------------------------------------------------------------------------
2.
??????? /// <summary>??????? /// 用來判斷是否停止線程的變量??????? /// </summary>??????? private? volatile bool m_stopThread=false;??????? private Thread m_threadStartSendMusic;
??????? /// <summary>??????? /// 開始發送??????? /// </summary>??????? public void SendMusicInThread(int startcount,int sendcount,string sbip)??????? {??????????? //若有在發送數據 停止發送??????????? m_stopThread = true;??????????? Thread.Sleep(106);??????????? m_stopThread = false;?????????????????????? m_StartCount = startcount;??????????? m_SendCount = sendcount;??????????? m_sbip = sbip;????????????//新開一個線程發送數據 ??????????? m_threadStartSendMusic = new Thread(SendMusic);??????????? m_threadStartSendMusic.IsBackground = true;??????????? m_threadStartSendMusic.Start();??????? }
??????? /// <summary>??????? /// 發送音樂??????? /// </summary>??????? private? void SendMusic()??????? {??????????? int endCount = m_StartCount + m_SendCount;??????????? for (int i = m_StartCount; i < endCount; i++)??????????? {??????????????? if (m_stopThread)??????????????? {??????????????????? break;??????????????? }
???????????? //do something ?????????????}
????? }
-------------------------------------------------------------------------------------------------
?
當我多次執行SendMusicInThread(。。。)方法時 為何1不能停止當前線程 然后再開線程m_threadStartSendMusic
2卻能把上一個線程停掉 然后再開一個線程
就?m_stopThread 放的位置不同
而m_stopThread 我設置的是volatile 應該每次執行去判斷m_stopThread的時候都是最新的值才對 為什么呢
?
?
- 2 回答
- 0 關注
- 406 瀏覽
添加回答
舉報
0/150
提交
取消