3

我正在使用外部库加载一个大而复杂的文件。对这个库的调用相当复杂,所以我将它们包装在几个静态辅助方法中,这些方法很好地为我处理缓存等。然后这些方法在后台使用Tasks.

在加载过程中,库在某些情况下会抛出异常,说明文件块格式错误,因此无法解析。这些异常被认为是“安全的”,如果它们被吞下,库将跳过坏块并愉快地继续解析文件的其余部分。

发生这种情况时,我需要向用户显示一个对话框,询问是否应该中止文件导入。这可以正常工作,如下所示:

public static class MyBigFileLoadMethods {
    // private fields for locking, caching, etc.

    public static Load(string filePath, bool cache = true) {
        // validation etc.

        try {
            var data = LoadMethodInDll(filePath);  
        } catch (BadBlockException) {
            if (MessageBox.Show("boom.  continue anyway?") == DialogResult.Yes) {
                // call appropriate dll methods to ignore exception and continue loading
            } else {
                throw;
            }
        }
    }
}

从设计为在后台运行的方法调用MessageBox.Show()感觉非常错误,但我还没有想出一个更好的方法,它不涉及太多的编组和调用,代码变得非常难以阅读。有没有更清洁的方法来做这件事,或者有更好的方法来设计我的加载过程?

4

1 回答 1

3

库执行此操作的适当方法是通过某种回调。最简单的实现是一个委托返回一个布尔值,指示是否应该继续处理。一种更丰富但更复杂的方式是策略接口,其中包含各种方法来实现指示是否继续、中止、重试等。

然后,您的 UI 代码提供回调,以适当的方式向用户显示消息。您加载库的代码将如下所示:

public static class MyBigFileLoadMethods {
    // private fields for locking, caching, etc.

    public static void Load(string filePath, Func<Exception, bool> continueOnException = null, bool cache = true) {
        // validation etc.

        try {
            var data = LoadMethodInDll(filePath);  
        } catch (BadBlockException e) {
            if (continueOnException != null && continueOnException(e))  {
                // call appropriate dll methods to ignore exception and continue loading
            } else {
                throw;
            }
        }
    }
}

然后在您的 UI 代码中,您将希望编组回 UI 线程。它看起来像这样:

MyBigFileLoadMethods.Load("C:\path\to\data", ShowError);

private bool ShowError(Exception e)
{
    if (this.InvokeRequired)
    {
        return (bool)this.Invoke(new Func<Exception, bool>(ShowError), e);
    }

    return MessageBox.Show(string.Format("boom: {0}. continue anyway?", e.Message)) == DialogResult.Yes;
}
于 2013-09-02T16:23:06.463 回答