C# 让调度员成为工作的榜样
我正试图从以下示例中创建一个工作程序:。我想我差不多做完了,但我不知道该由谁来打电话 调度员:我应该用什么来代替明天呢?我想我应该把UI线程,但我怎么能做到呢C# 让调度员成为工作的榜样,c#,C#,我正试图从以下示例中创建一个工作程序:。我想我差不多做完了,但我不知道该由谁来打电话 调度员:我应该用什么来代替明天呢?我想我应该把UI线程,但我怎么能做到呢 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.W
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace TestDelegate
{
public partial class Form1 : Form
{
private delegate void NoArgDelegate();
private delegate void OneArgDelegate(String arg);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Change the status image and start the rotation animation.
button1.Enabled = false;
button1.Text = "Contacting Server";
// Start fetching the weather forecast asynchronously.
NoArgDelegate fetcher = new NoArgDelegate(
this.FetchWeatherFromServer);
fetcher.BeginInvoke(null, null);
}
private void FetchWeatherFromServer()
{
Thread.Sleep(4000);
// Schedule the update function in the UI thread.
tomorrow.Dispatcher.BeginInvoke(
System.Threading.ThreadPriority.Normal,
new OneArgDelegate(UpdateUserInterface),
weather);
}
private void UpdateUserInterface(String weather)
{
//Update UI text
button1.Enabled = true;
button1.Text = "Fetch Forecast";
}
}
}
this.Dispatcher.Invoke(…)
?this.Dispatcher.Invoke(…)怎么样?问题在于.NET framework 3及更高版本中引入的引用Dispatcher
位于System.Windows.Threading
命名空间中,而不在System.Threading
中
System.Windows
命名空间在WPF(Windows演示基础)项目中可用。问题在于引用.NET framework 3及更高版本中引入的Dispatcher
位于System.Windows.Threading
命名空间中,而不在System.Threading
中
System.Windows
命名空间在WPF(Windows Presentation Foundation)项目中可用。在Windows窗体中,您只需使用this.BeginInvoke(…)
它是
控件
类的一种方法,窗体
派生自控件
,在Windows窗体中,您可以使用此方法。BeginInvoke(…)
这是一个
控制
类的方法,表单
派生自控制
我已经尝试过了,我得到了以下错误:“错误1'TestDelegate.Form1'不包含'Dispatcher'的定义,并且找不到接受第一个'TestDelegate.Form1'类型参数的扩展方法'Dispatcher'啊,我明白了。愚蠢的我。您使用的是WPF结构,但您的窗口是一个表单
,即WinForms。在这种情况下,没有调度程序-而是执行Invoke()
。我已经尝试过了,并且得到了以下错误:“错误1'TestDelegate.Form1'不包含'Dispatcher'的定义,并且没有扩展方法'Dispatcher'接受'TestDelegate.Form1'类型的第一个参数”啊,我明白了。愚蠢的我。您使用的是WPF结构,但您的窗口是一个表单
,即WinForms。在这种情况下,没有调度程序-而是执行Invoke()
。相同的问题:错误1命名空间“System.Threading”中不存在类型或命名空间名称“Dispatcher”(是否缺少程序集引用?)。我找到了解决方案:我正在开发Windows窗体应用程序,但是,在Windows演示文稿基础上,只有调度器。同样的问题:错误1:命名空间“系统”线程中不存在类型或命名空间名称“分派器”(您是否缺少一个程序集引用?)我找到了解决方案:我正在开发一个Windows窗体应用程序,但在Windows演示文稿基础上的调度程序。