2016-11-22 224 views
0

我有大約5000個文件位於FTP中,所以我通過使用FTP下載這些文件,然後解壓縮文件,最後處理並推入到oracle數據庫中。除了處理和推入數據庫的一切都很好,我不知道爲什麼處理不會發生。我可以看到調試器擊中該方法,但它不會進入內部method.How解決這個問題?Task.Factory.StartNew不執行.net中的方法

var list = ftp.GetFileList(remotepath); 

//------------------- 
DateTime dt = DateTime.Now; 
string st = String.Format("{0:yyyyMMdd}", dt);//20161120 
Task[] myTasks = new Task[list.Count]; 
int i = 0; 
foreach (string item in list) 
{ 
    { 
    if (item.StartsWith("GExport_") && (!item.ToUpper().Contains("DUM")) && (item.Contains(st)) && (!item.ToUpper().Contains("BLK"))) 
    { 
     4gpath = item; 
     //Downloadfile() 
     ftp.Get(dtr["REMOTE_FILE_PATH"].ToString() + 4gpath , @localDestnDir + "\\" + dtr["SOURCE_PATH"].ToString()); 
     download_location_hw = dtr["LOCAL_FILE_PATH"].ToString(); 
     // Spin off a background task to process the file we just downloaded 
     myTasks[i++] = Task.Factory.StartNew(() => 
     { 
      //Extractfile()    
      ExtractZipfiles(download_location_hw + "//" + huwawei4gpath, dtr["REMOTE_FILE_PATH"].ToString(), 
       dtr["FTP_SERVER"].ToString(), dtr["FTP_USER_ID"].ToString(), 
       dtr["TECH_CODE"].ToString(), dtr["VENDOR_CODE"].ToString()); 
      //Extract the zip file referred to by download_location_hw 
      // Process the extracted zip file 
      ProcessFile() 
     }); 
     } 
    } 
    } 
    Task.WaitAll(myTasks); 

這裏ProcessFile()方法不是在執行所有

編輯 有錯字在filepath原因要點,謝謝你,但我的問題是,是否有任何同步問題,因爲第一個解壓縮文件,並同時間處理文件,其中文件不可用,它會在處理之前等待解壓縮 -

添加檢查while(!File.Exists("")) { Thread.Sleep(1000); 這樣做,使任何isssues?

+0

你怎麼知道'ProcessFile'沒有被執行?你是否嘗試在該行上放置斷點? – wdosanjos

+0

我把斷點放在那裏,可以看到它正在那裏,但沒有進入裏面,沒有數據被推入到數據庫中。其中文件被成功下載和提取 – peter

+0

@peter可能你正在調試主要堆棧跟蹤線。 – mybirthname

回答

1

如果您在此處嘗試此代碼,您會注意到它的工作原理。它和你的代碼很相似。由於這是有效的,你的問題在其他地方並且與任務無關。

class Program { 
    static void Main(string[] args) { 
    var list = new List<string> { "1", "2" }; 
    Task[] myTasks = new Task[ list.Count ]; 
    int i = 0; 
    foreach(string item in list) { 

     // Spin off a background task to process the file we just downloaded 
     myTasks[ i++ ] = Task.Factory.StartNew(() => 
     { 

      //Extract the zip file referred to by download_location_hw 
      // Process the extracted zip file 
      ProcessFile(); 

      }); 
    } 



    Task.WaitAll(myTasks); 

    Console.WriteLine("in main after processing..."); 
    Console.Read(); 
    } 

    private static void ProcessFile() { 
    Console.Write("Processed..."); 
    } 
} 
+0

是的,你是正確的,有文件路徑導致問題的錯字,謝謝,但我的問題是有沒有任何同步問題,因爲首先解壓縮文件和同一時間進程文件,其中文件不可用,它會等待解壓縮之前處理。因爲現在我geting文件couldnot可用像 – peter

+0

我添加此檢查while(!File.Exists(「」)) { Thread.Sleep(1000); }是否有任何問題 – peter

+0

你可以請編輯的問題,因爲不少人已經投票要求你這樣做。我可以幫助你。 – CodingYoshi