<legend id='bboq6'><style id='bboq6'><dir id='bboq6'><q id='bboq6'></q></dir></style></legend>

      <bdo id='bboq6'></bdo><ul id='bboq6'></ul>

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

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

    <tfoot id='bboq6'></tfoot>
    1. C#中的委托语法问题

      时间:2023-11-10
    2. <legend id='tItuX'><style id='tItuX'><dir id='tItuX'><q id='tItuX'></q></dir></style></legend>

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

              <tbody id='tItuX'></tbody>

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

                本文介绍了C#中的委托语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我构建了一个测试箱来了解有关 Windows 窗体应用程序中的线程的知识.Silverlight 和 Java 正在提供 Dispatcher,这在更新时确实很有帮助GUI 元素.

                I built a Testbox to learn something about threading in windows form applications. Silverlight and Java are providing the Dispatcher, which really helps when updating GUI Elements.

                代码示例:声明类委托

                public delegate void d_SingleString(string newText);
                

                创建线程

                        _thread_active = true;
                        Thread myThread = new Thread(delegate() { BackGroundThread(); });
                        myThread.Start();
                

                线程函数

                    private void BackGroundThread()
                    {
                        while (_thread_active)
                        {
                            MyCounter++;
                            UpdateTestBox(MyCounter.ToString());
                            Thread.Sleep(1000);
                        }
                    }
                

                委派文本框更新

                    public void UpdateTestBox(string newText)
                    {
                        if (InvokeRequired)
                        {
                            BeginInvoke(new d_SingleString(UpdateTestBox), new object[] { newText });
                            return;
                        }
                        tb_output.Text = newText;
                    }
                

                有没有办法在 BeginInvoke 方法中声明延迟声明?!

                Is there a way to declare the Declaration of the Delate IN the BeginInvoke Method?!

                类似

                BeginInvoke(*DELEGATE DECLARATION HERE*, new object[] { newText });
                

                非常感谢,雷特

                推荐答案

                在很多这样的情况下,最简单的方法是使用捕获的变量"在线程之间传递状态;这意味着您可以保持逻辑本地化:

                In many cases like this, the simplest approach is to use a "captured variable" to pass state between the threads; this means you can keep the logic localised:

                public void UpdateTestBox(string newText)
                {
                    BeginInvoke((MethodInvoker) delegate {
                        tb_output.Text = newText;
                    });        
                }
                

                如果我们期望在工作线程上调用它(很少点检查InvokeRequired),上述内容特别有用 - 请注意,这对于任何一个 UI 都是安全的或工作线程,并允许我们在线程之间传递尽可能多或尽可能少的状态.

                The above is particularly useful if we expect it to be called on the worker thread (so little point checking InvokeRequired) - note that this is safe from either the UI or worker thread, and allows us to pass as much or as little state between the threads.

                这篇关于C#中的委托语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:循环内的匿名 C# 委托 下一篇:代表上的可选参数无法正常工作

                相关文章

              2. <tfoot id='Az7Pe'></tfoot>

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

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