C# WPF中需要的Control.invokererequired发生了什么?
我正在编写一个小的WPF应用程序,它会产生一些线程来对网站页面进行压力测试。我想在wpf的文本框中输出一些信息,说明我从主wpf线程手动启动的线程的状态。我希望WPF能够通过以下方式将我的线程同步回GUI线程:C# WPF中需要的Control.invokererequired发生了什么?,c#,.net,wpf,multithreading,C#,.net,Wpf,Multithreading,我正在编写一个小的WPF应用程序,它会产生一些线程来对网站页面进行压力测试。我想在wpf的文本框中输出一些信息,说明我从主wpf线程手动启动的线程的状态。我希望WPF能够通过以下方式将我的线程同步回GUI线程: if(this.InvokeRequired) this.Invoke(ProcessState); 然而,WPF中没有类似的内容。。。在WPF中,从非gui线程进行输出的选项有哪些 编辑: 现在的问题似乎是,我无法检查控件的Dispatcher对象的方法CheckA
if(this.InvokeRequired)
this.Invoke(ProcessState);
然而,WPF中没有类似的内容。。。在WPF中,从非gui线程进行输出的选项有哪些
编辑:
现在的问题似乎是,我无法检查控件的Dispatcher
对象的方法CheckAccess()
。我正在运行.net的3.5版本和VS2008SP1
以下是我的Dispatcher类(上面是F12)的完整元数据:
}在WPF中,它被移出了控件/UIElement层次结构,移到了dispatcher中 等价物是和 您应该能够做到:
if (someControl.Dispatcher.CheckAccess())
UpdateUI();
else
someControl.Dispatcher.Invoke(new Action(UpdateUI));
您的元数据包括:
//
// Summary:
// Determines whether the calling thread is the thread associated with this
// System.Windows.Threading.Dispatcher.
//
// Returns:
// true if the calling thread is the thread associated with this System.Windows.Threading.Dispatcher;
// otherwise, false.
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CheckAccess();
这是您要使用的方法。。。它没有在Intellisense中显示,但它在那里,并且它确实正常工作。对于我来说,它在每个页面、用户控件和框架上都能正常工作
Implements IDisposable 'Code is implemented automatically
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler Me.Dispatcher.ShutdownStarted, AddressOf Me_HasShutDownStarted
End Sub
Private Sub Me_HasShutDownStarted(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.disposedValue = False Then Call Me.Dispose()
End Sub
如果您只是将WinForm Invoke转换为WPF Dispatch,则只需进行一些小的更改即可将WinForm代码转换为WPF代码。这对名为“_connection”的控件有效,并被重写为在WPF应用程序中工作
// ---- Original WinForm code ----
delegate void SetIndicatorCallback(bool bVal);
private void SetIndicator(bool bVal)
{
if (this._connection.InvokeRequired)
{
SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
this.Invoke(d, new object[] { bVal });
}
else
{
if (bVal)
{
// Change the control's color, or whatever action is required.
}
}
}
// ---- WPF ----
// Still using a delegate
delegate void SetIndicatorCallback(bool bVal);
private void SetIndicator(bool bVal)
{
if (this._connection.Dispatcher.CheckAccess())
{
if (bVal)
{
// Change the control' color, or whatever action is needed.
}
}
else
{
SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
this.Dispatcher.BeginInvoke(d, new object[] { bVal });
}
}
@里德,我怀疑bc的事实是我在3.5版本下,控件的Dispatcher类上没有CheckAccess(),有什么想法吗?@爱我的乍得:它应该存在:(这是3.5版本)确保使用System.Windows.Threading添加
代码>如果您也想使用Dispatcher。@Reed,我没有将该程序集作为.net的一部分列在“添加引用”下。它是否作为.net框架的一部分隐藏在某处?@Chad:它是一个标准的WPF程序集。如果你在3.5(甚至是3.0)和WPF项目中,它应该是可用的…@查德:这不会在Intellisense中显示(出于愚蠢的原因,它被伪装为不可浏览),但它在那里,甚至在你的元数据中。。。
// ---- Original WinForm code ----
delegate void SetIndicatorCallback(bool bVal);
private void SetIndicator(bool bVal)
{
if (this._connection.InvokeRequired)
{
SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
this.Invoke(d, new object[] { bVal });
}
else
{
if (bVal)
{
// Change the control's color, or whatever action is required.
}
}
}
// ---- WPF ----
// Still using a delegate
delegate void SetIndicatorCallback(bool bVal);
private void SetIndicator(bool bVal)
{
if (this._connection.Dispatcher.CheckAccess())
{
if (bVal)
{
// Change the control' color, or whatever action is needed.
}
}
else
{
SetIndicatorCallback d = new SetIndicatorCallback(SetIndicator);
this.Dispatcher.BeginInvoke(d, new object[] { bVal });
}
}