【问题标题】:Download multiple file from website, how to handle ctrl+c correctly从网站下载多个文件,如何正确处理 ctrl+c
【发布时间】:2013-09-22 06:18:10
【问题描述】:

我创建了一个非常简单的控制台应用程序,它从 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..

【问题讨论】:

    标签: c# .net visual-studio-2010 console-application


    【解决方案1】:

    听起来您需要一种方法来指示您的下载代码以停止下载。通过查看此示例,我认为最佳位置可能是您的 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
        }
       }
    }
    

    【讨论】:

    • 您的代码提供了一种停止下一次下载的方法。看起来文件被锁定,直到下载完成。在这种情况下,我不需要删除文件,下载当前文件后会中止下载。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多