task.factory.startnew不执行.NET中的方法



我有大约5000个文件位于FTP中,因此我正在使用FTP下载这些文件,然后解压缩文件,最后处理并推入Oracle Database.except Processing并将其推入数据库一切顺利,我不知道为什么处理没有发生。

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);这会导致任何ISSSSESSE?

如果在此处尝试此代码,您会注意到它有效。它与您的代码非常相似。由于这有效,因此您的问题在其他地方,并且与任务无关。

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..." );
  }
}

最新更新