我有一些需要从我的后台演示者线程更新的UI代码.所以,我执行以下操作:从我的后台线程,我在UI中设置我的属性:
_ui.ConnectionStatus = "A";
Run Code Online (Sandbox Code Playgroud)
然后,我的设置如下:
public string ConnectionStatus
{
set
{
if (Dispatcher.CheckAccess())
ConnectionStatusTxt.Content = value;
else
{
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{ConnectionStatusTxt.Content = value;}));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
The calling thread cannot access this object because a different thread owns it.
我的理解是Dispatcher负责调用不同的线程,所以这个错误让我感到困惑.谢谢!
我的视图模型中有一些代码如下:
miService.GetSomething(par1, par2)
.ObserveOnDispatcher()
.Subscribe(dt =>
{
DoSomething(dt);
});
Run Code Online (Sandbox Code Playgroud)
然后在我的测试中,我"嘲笑"我的服务如下:
miService.Setup(ms => ms.GetSomething(....))
.Returns(Observable.Return(XYZ));
Run Code Online (Sandbox Code Playgroud)
问题是由于ObserveOnDispatcher,订阅委托永远不会被执行.
我见过DispatcherFrame和PushFrame的一些代码,但问题是我不知道"哪里",我可以打电话
frame.Continue = false;
Run Code Online (Sandbox Code Playgroud) 在Windows Phone 7/Silverlight中,以下代码是安全的还是竞争条件?
//Snippet 1
foreach(var item in list)
{
Deployment.Current.Dispatcher.BeginInvoke( () => { foo(item); });
}
Run Code Online (Sandbox Code Playgroud)
当然(?)这个替代品很活泼?
//Snippet 2
Deployment.Current.Dispatcher.BeginInvoke( () =>
{
foreach(var item in list){ foo(item); }
});
list.Clear();
Run Code Online (Sandbox Code Playgroud) silverlight concurrency dispatcher thread-safety windows-phone-7
我想每隔几秒加载一张图片.我创建DispatcherTimer,我想在计时器滴答无所事事,只是等待间隔.我怎样才能做到这一点?
if (window.DialogResult == true) {
st=window.st;
for(int i=0;i<=st;i++){
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,5);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
arr[i]=window.arr[i];
image1.Source = arr[i];
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我现在空了.
void timer_Tick(object sender, EventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud) 我只想弄清楚vb.net中Dispatcher.Invoke的语法.我让它在C#应用程序中工作,但现在我需要VB表单.这是我的C#应用程序的代码
this.Dispatcher.Invoke((Action)(() =>
{
SOME CODE
}));
Run Code Online (Sandbox Code Playgroud)
所有在线资源似乎都是C#,所以任何帮助都会受到赞赏.谢谢.
您好
我正在编写服务器监控应用程序.
类
public class Server
{
public string SERVERNAME;
public string ENVIRONMENT;
public string VERSION;
public string CPU_PERCETAGE;
public string CPU_NAME;
public string CPU_DESCRIPTION;
public string CPU_VOLTAGE;
}
Run Code Online (Sandbox Code Playgroud)
我目前有一个Page wihtin我的主窗口,我在那里Exucute并填充数据:
方法
try
{
{
Thread test = new Thread(() =>
{
datagrid_Disks.Dispatcher.BeginInvoke(
new Action(() =>
{
datagrid_Disks.ItemsSource = Server.GetDisksInfo(textbox_Username.Text,
textbox_password.Password,
textbox_IP.Text,
textbox_Domain.Text);
}));
});
test.Start();
}
catch (UnauthorizedAccessException)
{
Worker.ShowModernBox("Onjuiste gebruiksersnaam" + Environment.NewLine + "of wachtwoord.");
}
catch (ManagementException)
{
Worker.ShowModernBox("Server geeft geen Response." + Environment.NewLine + "Controleer Aub …Run Code Online (Sandbox Code Playgroud) 我想编写过滤器,并httprequest在控制器之前获取客户端并制作一些代码,取决于URL.
请求可以是:HttpRequest,MultipartHttpServletRequest,可POST还是GET.如果此请求的URL开头,我需要向另一个REST API发出请求api.
我遇到了对象的PushFrame方法Dispatcher.它是方法的简化版本:
public void PushFrame(DispatcherFrame frame)
{
// Stuff
_frameDepth++;
while(frame.Continue)
{
// Getting and dispatching messages
}
_frameDepth--;
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
换句话说,它只是打开新的消息处理循环.但我真的无法理解这种方式的好处.出于什么目的PushFrame使用?它的用法有很好的例子吗?至于我,似乎这种方法会导致不明显的错误.
我在WPF中实现了一种脚本语言,该语言允许用户编写多个并发操作的脚本。这些动作是使用async / await来实现的,所有这些动作都在主线程上运行。
我现在将逐步调试器引入该脚本语言。当前,有一个与脚本引擎关联的窗口,以及与步骤调试器不同的窗口。
我试图在执行脚本操作之前在引擎窗口中停止进程,同时不阻止调试器窗口上的交互。MessageBox.Show()为此特别有用,因为它会暂停动画,动画本身可能会在完成时开始新的动作。
Thread.Sleep()当然根本不起作用,因为每个人都在主线程上。
C#中是否有类似于MessageBox.Show()但没有UI的暂停机制?我试过调用Dispatcher.DisableProcessing(),我可以宣誓可以暂停引擎窗口,但是现在出现错误:
“调度程序处理已暂停,但消息仍在处理中”
我认为理想情况下,我想做这样的事情:
//Engine Window
private debugger;
void RunAction(ScriptAction action){
if(action.BreakPoint){
var pauserObject = PauseProcessesOnThisWindow();
debugger.break(action, pauseObject);
}
}
//Debugger Window
void Continue_Click(){
pauseObject.release();
}
Run Code Online (Sandbox Code Playgroud)
如果必须使用MessageBox方法,则可以看到从另一个窗口关闭它很棘手,更不用说在调试器窗口中具有某些控件是一个笨拙的UI,但是仍然需要用户从引擎窗口。
UWP app(mvvm架构)我有一个MainView,它的ViewModel中有一个集合,用于绑定到MainView上的GridView ,每个项目都有一个带有2路数据绑定的TextBox,带有Note类的Description属性.
每个gridviewitem的TextBox的Xaml.
<TextBlock Text="{x:Bind Description,Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)
用于绑定到gridview的ItemSource的Collection属性.
public ObservableCollection<Note> Notes { get; }
Run Code Online (Sandbox Code Playgroud)
这是课堂笔记
public class Note : Observable
{
private string _description;
public string Description
{
get => _description;
set => Set(ref _description, value, nameof(Description));
}
}
Run Code Online (Sandbox Code Playgroud)
在可观测类是双向数据绑定的帮助.
public class Observable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value)) …Run Code Online (Sandbox Code Playgroud) dispatcher ×10
c# ×7
wpf ×6
.net ×1
api ×1
async-await ×1
concurrency ×1
java ×1
multiviews ×1
rest ×1
silverlight ×1
spring ×1
unit-testing ×1
uwp ×1
vb.net ×1