Can I add an attribute to a function to prevent reentry?

前端 未结 9 2334
梦谈多话
梦谈多话 2021-02-15 12:26

At the moment, I have some functions which look like this:

private bool inFunction1 = false;
public void function1()
{
    if (inFunction1) return;
    inFunctio         


        
相关标签:
9条回答
  • 2021-02-15 13:12

    You may want to consider avoiding re-entrancy by modifying your design so that it will never call function1() before its previous invocation completes. To me it seems that a layer is missing from above function1().

    0 讨论(0)
  • 2021-02-15 13:14

    I dont think that will be possible.

    The closest will be the the 'Synchronized' attribute, but that will block all subsequent calls.

    0 讨论(0)
  • 2021-02-15 13:20

    You may find that you could use PostSharp to accomplish this - along with the suggestions from Anthony about using try/finally. It's likely to be messy though. Also consider whether you want the re-entrancy to be on a per-thread or per-instance basis. (Could multiple threads call into the method to start with, or not?)

    There's nothing like this in the framework itself.

    0 讨论(0)
提交回复
热议问题