-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

おそらく、このような問題を処理するために、もう 1 レベルの間接化を追加することができます。

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 に答える