2016-10-31 83 views
-3

停止基於標誌的線程Finesedequeue在下面的線程方法中,我在停止按鈕中做了一個循環(其目的是在填充名爲_textFromFilesQueue的隊列後停止該線程)在這個主要的問題是,我的形式凍結停止線程在隊列填滿之後停止

這裏是我的代碼:

線程方法:

public static bool Finisedqueue ; 
    public void Read(string inputDirectoryPath) 
    { 

     try 
     { 
      Thread.CurrentThread.IsBackground = true; 
      Finisedqueue = false; 
      Console.WriteLine("thread 1 started"); 
      if (Form1.IsStarted) 
      { 
       Thread.Sleep(3000); 
       var dir = new DirectoryInfo(inputDirectoryPath); 
       FileInfo[] f = dir.GetFiles("*.txt"); 
       if (f.Length > 0) 
       { 
        Console.WriteLine("{0} directory contains {1} Files",inputDirectoryPath,f.Length); 
        int counter = 0; 
        foreach (System.IO.FileInfo fi in f) 
        { 

         counter++; 
         var filePath = Path.Combine(fi.DirectoryName, fi.Name); 
         string textFromFile = File.ReadAllText(filePath); 
         _textFromFilesQueue.Enqueue(textFromFile); 
         Console.WriteLine("The text inside file number {0} is : {1}",counter,textFromFile); 
         fi.Delete(); 
         Console.WriteLine("deleting file number {0} from input folder",counter); 


        } 
        Finisedqueue = true; 
        Console.WriteLine("finished Deleting files"); 
       } 

       else 
       { 
        Console.WriteLine("{0} Has no files inside it ",inputDirectoryPath); 
       } 
      } 

     } 
     catch (Exception excep) 
     { 
      Console.WriteLine("An error occurred: '{0}'", excep); 
      throw(excep); 

     } 
    } 

停止按鈕點擊:

 private void stop_Click(object sender, EventArgs e) 
    { 

     while (!Thread1.Finisedqueue) 
     { 
      Console.WriteLine("Queue from First Thread is still Enqueuing data"); 
     } 
     Thread1Timer.Dispose(); 
     button3.Enabled = true; 
     textBox3.Enabled = true; 



    } 
> the main problem in this is that my form is freezing 

,如果任何機構可以幫助我在此非常感謝您

+0

您的窗體凍結在'while'循環中。 –

+0

我該如何解決這個問題! –

+0

您的StopClick不會停止任何操作,只會掛起,直到完成所有工作。 –

回答

-1

繁忙時等待它是睡覺的好主意,讓你給其他線程執行的機會。

while (!Thread1.Finisedqueue) 
{ 
    Thread.Sleep(100); 
    Console.WriteLine("Queue from First Thread is still Enqueuing data"); 
} 

您還可以檢查出任務類 https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx這是一個異步操作,例如文件IO不錯,是awaitable使用內置async/await功能。

private Queue<string> q = new Queue<string>(); 
private Task readFilesTask; 
public void ReadFiles(string inputDirectory) 
{ 
    var files = Directory.GetFiles(inputDirectory, "*.txt", SearchOption.TopDirectoryOnly); 
    foreach (var file in files) 
    { 
     var textFromFile = File.ReadAllText(file); 
     q.Enqueue(textFromFile); 
     File.Delete(file); 
    } 
} 

private void start_Click(object sender, EventArgs e) 
{ 
    readFilesTask = Task.Run(() => ReadFiles("//path/to/dir")); 
} 

private void stop_Click(object sender, EventArgs e) 
{  
    readFilesTask.Wait(); 
    //readFilesTask.Wait(1000); // if you want kill the task after waiting for 1000 milliseconds 
    //Enable buttons 
} 
+0

這是醜陋的和錯誤的。殺死所有的線程...... –

+0

如果你能做到這一點,你也可以使用Thread.Yield方法來獲得更好的性能(這可以根據你的.NET版本 – VMAtm

+0

而有所不同,你可以給我一個例子,在我的情況下改變它在我的代碼中!謝謝 –