2016-11-07 5 views
0

私は次のコードを持っている:メソッドの実行中にメソッドを再利用するには?

CreateMultiLayerWakeupSteps() 
{ 
    var wakeupStep = new AutodetectWakeupStep 
    { 
     //Creating the object... 
    } 

    //Later in the process I have this operation: 

    wakeupStep.SuccessNextStep = UserInputs(wakeupStep); 
} 

メソッドの実装は次のようなものになりますUserInputs:私は再びCreateMultiLayerWakeupStepメソッドを呼び出すしたいと思いますUserInputs方法で

private static AutodetectCommandStep UserInputs(AutodetectWakeupStep wakeupStep) 
    { 
     AutodetectCommandStep autodetectCommandStep 
     { 
      //Creating the object... 
     } 

     //Some more operations autodetectCommandStep here... 

     return autodetectCommandStep; 
    } 

を新しいステップを作成するために、次の例外がスローされます。StackOverflowException

実行中にメソッドを再利用するソリューションはありますか?実装するのは難しいですか?私はスレッド非同期に精通していません。

よろしくお願いいたします。

+3

再帰を学習する必要があります。この関数が実行されている間、関数をcalしようとしています。 – tym32167

+0

何をしようとしているのかは無限ループに終わります。理論的に無限であるのはもちろん、それがメモリの壁に突き当たるからです。完全に期待される。だからあなたのロジック/デザインはどこかに欠陥があるはずです。 – Fildor

+0

@ tym32167私が知っていることから、再帰はそれ自身を呼び出すメソッドであり、これは私の場合ではありません –

答えて

3

ここではマルチスレッドについては何もありません。あなたは再帰をしようとしていますが、再帰がいつ終了するかは指定していません。そのため、StackOverflowExceptionが届きます。

したがって、たとえば、あなたがメソッドを残すように、このExecuteAgainがあなたの状況に応じて、falseになりますときには決めるべきAutodetectCommandStep.ExecuteAgain

void CreateMultiLayerWakeupSteps() 
{ 
    var wakeupStep = new AutodetectWakeupStep 
    { 
     //Creating the object... 
    } 

    //Later in the process I have this operation: 

    wakeupStep.SuccessNextStep = UserInputs(wakeupStep); 

    if(wakeupStep.SuccessNextStep.ExecuteAgain) 
     CreateMultiLayerWakeupSteps(); 
} 

でプロパティを持っている必要があります。常にtrueの場合、同じ例外がスローされます。

また、CreateMultiLayerWakeupSteps(AutodetectWakeupStep wakeUpStep)の外側にAutodetectWakeupStepオブジェクトを作成することをお勧めします。あなたのコードは次のようになります。

void CreateMultiLayerWakeupSteps(AutodetectWakeupStep wakeUpStep) 
{ 

     AutodetectWakeupStep step = UserInputs(wakeupStep); 

     if(step.ExecuteAgain) 
      CreateMultiLayerWakeupSteps(step); 
} 
関連する問題