<tfoot id='JO1d4'></tfoot>
  • <legend id='JO1d4'><style id='JO1d4'><dir id='JO1d4'><q id='JO1d4'></q></dir></style></legend>

    • <bdo id='JO1d4'></bdo><ul id='JO1d4'></ul>
    <i id='JO1d4'><tr id='JO1d4'><dt id='JO1d4'><q id='JO1d4'><span id='JO1d4'><b id='JO1d4'><form id='JO1d4'><ins id='JO1d4'></ins><ul id='JO1d4'></ul><sub id='JO1d4'></sub></form><legend id='JO1d4'></legend><bdo id='JO1d4'><pre id='JO1d4'><center id='JO1d4'></center></pre></bdo></b><th id='JO1d4'></th></span></q></dt></tr></i><div id='JO1d4'><tfoot id='JO1d4'></tfoot><dl id='JO1d4'><fieldset id='JO1d4'></fieldset></dl></div>

        <small id='JO1d4'></small><noframes id='JO1d4'>

      1. 避免 C# 中的重复事件订阅

        时间:2023-09-16
        <tfoot id='kj7hB'></tfoot>
        1. <small id='kj7hB'></small><noframes id='kj7hB'>

        2. <i id='kj7hB'><tr id='kj7hB'><dt id='kj7hB'><q id='kj7hB'><span id='kj7hB'><b id='kj7hB'><form id='kj7hB'><ins id='kj7hB'></ins><ul id='kj7hB'></ul><sub id='kj7hB'></sub></form><legend id='kj7hB'></legend><bdo id='kj7hB'><pre id='kj7hB'><center id='kj7hB'></center></pre></bdo></b><th id='kj7hB'></th></span></q></dt></tr></i><div id='kj7hB'><tfoot id='kj7hB'></tfoot><dl id='kj7hB'><fieldset id='kj7hB'></fieldset></dl></div>

            <legend id='kj7hB'><style id='kj7hB'><dir id='kj7hB'><q id='kj7hB'></q></dir></style></legend>
                <bdo id='kj7hB'></bdo><ul id='kj7hB'></ul>

                    <tbody id='kj7hB'></tbody>
                  本文介绍了避免 C# 中的重复事件订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您如何建议避免重复事件订阅的最佳方法?如果这行代码在两个地方执行,则事件将运行两次.我试图避免订阅两次的第 3 方事件.

                  How would you suggest the best way of avoiding duplicate event subscriptions? if this line of code executes in two places, the event will get ran twice. I'm trying to avoid 3rd party events from subscribing twice.

                  theOBject.TheEvent += RunMyCode;
                  

                  在我的委托设置器中,我可以有效地运行它...

                  In my delegate setter, I can effectively run this ...

                  theOBject.TheEvent -= RunMyCode;
                  theOBject.TheEvent += RunMyCode;
                  

                  但这是最好的方法吗?

                  推荐答案

                  我认为,最有效的方法是让你的事件成为一个属性并为其添加并发锁,就像在这个 示例:

                  I think, the most efficient way, is to make your event a property and add concurrency locks to it as in this Example:

                  private EventHandler _theEvent;
                  private object _eventLock = new object();
                  public event EventHandler TheEvent
                  {
                      add
                      {
                          lock (_eventLock) 
                          { 
                              _theEvent -= value; 
                              _theEvent += value; 
                          }
                      }
                      remove
                      {
                          lock (_eventLock) 
                          { 
                             _theEvent -= value; 
                          }
                      }
                  }
                  

                  这篇关于避免 C# 中的重复事件订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C#:调用按钮事件处理程序方法而不实际单击按钮 下一篇:在什么情况下脱离事件是必要的?

                  相关文章

                  <tfoot id='pBJcj'></tfoot>

                  <i id='pBJcj'><tr id='pBJcj'><dt id='pBJcj'><q id='pBJcj'><span id='pBJcj'><b id='pBJcj'><form id='pBJcj'><ins id='pBJcj'></ins><ul id='pBJcj'></ul><sub id='pBJcj'></sub></form><legend id='pBJcj'></legend><bdo id='pBJcj'><pre id='pBJcj'><center id='pBJcj'></center></pre></bdo></b><th id='pBJcj'></th></span></q></dt></tr></i><div id='pBJcj'><tfoot id='pBJcj'></tfoot><dl id='pBJcj'><fieldset id='pBJcj'></fieldset></dl></div>
                    <bdo id='pBJcj'></bdo><ul id='pBJcj'></ul>
                  <legend id='pBJcj'><style id='pBJcj'><dir id='pBJcj'><q id='pBJcj'></q></dir></style></legend>

                    1. <small id='pBJcj'></small><noframes id='pBJcj'>