-1

我有一个在新线程中调用的方法“ImportExcel”:

[STAThread]
    private void btnImportExcel_Click(object sender, EventArgs e)
    {
        // Start by setting up a new thread using the delegate ThreadStart
        // We tell it the entry function (the function to call first in the thread)
        // Think of it as main() for the new thread.
        ThreadStart theprogress = new ThreadStart(ImportExcel);

        // Now the thread which we create using the delegate
        Thread startprogress = new Thread(theprogress);
        startprogress.SetApartmentState(ApartmentState.STA);

        // We can give it a name (optional)
        startprogress.Name = "Book Detail Scrapper";

        // Start the execution
        startprogress.Start();            
    }

现在在 ImportExcel() 函数中,有一个 try catch 块。在 catch 块中,如果发生特定异常,我希望再次调用 ImportExcel() 函数。我怎么做?

4

1 回答 1

2

可能您可以只添加一个间接级别来处理此类问题:

private void TryMultimpleImportExcel()
{
    Boolean canTryAgain = true;

    while( canTryAgain)
    {
        try
        {
            ImportExcel();
            canTryAgain = false;
        }
        catch(NotCriticalTryAgainException exc)
        {
            Logger.Log(exc);
        }
        catch(Exception critExc)
        {
            canTryAgain = false;
        }
    }
}


    // ...
    ThreadStart theprogress = new ThreadStart(TryMultimpleImportExcel);
    // ..
    startprogress.Start();    

还:

如果您想让您的用户停止可能无休止的处理,您可能需要使用 CancellationToken,如此处所述 -如何使用 CancellationToken 属性?. 谢谢@MikeOfSST。

于 2014-08-18T09:54:53.483 回答