从网站下载多个文件,如何正确处理ctrl + c

本文关键字:正确处理 ctrl 下载 网站 文件 | 更新日期: 2023-09-27 18:32:28

我创建了一个非常简单的控制台应用程序,该应用程序从Web下载大量文件,并按照单独的映射文件将它们放置在文件夹结构中。该要求不要求需要异步下载文件。

该程序可以工作,但问题是如果有人选择使用 ctrl+c 或 ctrl+break 取消应用程序。

如果这样做,正在进行的文件将随着程序立即退出而损坏。所以我想在退出之前删除损坏的文件。所以我写了下面的处理程序,

static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
    try
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Program interrupted..deleting corrupted file");
        Console.ResetColor();
        if (File.Exists(fileInProgress))
        {
            File.Delete(fileInProgress);
        }
    }
    catch
    {
        Console.WriteLine("Error occured.");
    }
}

fileinprogress是从调用下载文件的函数更新的全局变量。

上述代码的问题在于,如果按ctrl + c,则执行代码,但它永远不会删除正在使用的文件的文件。所以我按照 https://stackoverflow.com/a/937558/714518 并试图等到程序发布文件

static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
    try
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Program interrupted..deleting corrupted file");
        Console.ResetColor();
        if (File.Exists(fileInProgress))
        {
            while (IsFileLocked(fileInProgress))
            {
                System.Threading.Thread.Sleep(1000);
            }
            File.Delete(fileInProgress);
        }
    }
    catch
    {
        Console.WriteLine("Error occured.");
    }
}

现在我不明白这种行为。现在,如果按ctrl + c,程序会等待几秒钟,然后不删除文件,它将继续下载下一个文件。请帮助摆脱这个问题。

实际的应用程序相当大,我只是重现了这种情况。有关完整代码,请参阅 http://pastebin.com/TRBEAvwi。

从网站下载多个文件,如何正确处理ctrl + c

听起来您需要一种方法来指示下载代码停止下载。 通过查看此示例,我认为最佳位置可能是进入您的Console_CancelKeyPress函数。 否则,您的下载代码将永远不会意识到它需要释放文件锁定并停止下载。

例如:

static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
    try
    {
        Interlocked.Increment(ref globalStopFlag);
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Program interrupted..deleting corrupted file");
        Console.ResetColor();
        if (File.Exists(fileInProgress))
        {
            while (IsFileLocked(fileInProgress))
            {
                System.Threading.Thread.Sleep(1000);
            }
            File.Delete(fileInProgress);
        }
    }
    catch
    {
        Console.WriteLine("Error occured.");
    }
}
void SomeDownloadFunction()
{
   using (somefile)
   {
     while (!downloadFinished)
    {
        long doINeedToStop = Interlocked.Read(ref   globalStopFlag)
        if (doINeedToStop != 0)
          return;
        //Download the next set of packets and write them to somefile
    }
   }
}