Task.Factory.StartNew没有在.net中执行该方法

时间:2016-11-22 22:20:01

标签: c# .net multithreading ftp task-parallel-library

我有大约5000个文件位于FTP,所以我通过使用FTP下载然后解压缩文件,最后处理并推送到oracle数据库。除了处理和推送到数据库一切正常,我不知道为什么处理没有发生。我可以看到调试器点击该方法,但它没有进入内部方法。如何解决这个问题?

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); 这会产生任何问题吗?

1 个答案:

答案 0 :(得分: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..." );
  }
}
相关问题