相关疑难解决方法(0)

C#中的委托语法问题

我构建了一个Testbox来了解Windows窗体应用程序中的线程.Silverlight和Java提供了Dispatcher,它在更新GUI元素时非常有用.

代码示例:声明类代表

public delegate void d_SingleString(string newText);
Run Code Online (Sandbox Code Playgroud)

创建线程

        _thread_active = true;
        Thread myThread = new Thread(delegate() { BackGroundThread(); });
        myThread.Start();
Run Code Online (Sandbox Code Playgroud)

线程功能

    private void BackGroundThread()
    {
        while (_thread_active)
        {
            MyCounter++;
            UpdateTestBox(MyCounter.ToString());
            Thread.Sleep(1000);
        }
    }
Run Code Online (Sandbox Code Playgroud)

委派TextBox更新

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

有没有办法在BeginInvoke方法中声明Delate宣言?!

就像是

BeginInvoke(*DELEGATE DECLARATION HERE*, new object[] { newText });
Run Code Online (Sandbox Code Playgroud)

非常感谢,rAyt

c# multithreading delegates

6
推荐指数
1
解决办法
1615
查看次数

标签 统计

c# ×1

delegates ×1

multithreading ×1