当函数以Dispatcher.BeginInvoke启动完成时,我需要使用回调函数来执行一些后处理任务.但是我在Dispatcher.BeginInvoke中找不到任何参数来接受回调.可以给Dispatcher.BeginInvoke提供一个回调函数吗?
首先介绍一下:我有一个WPF应用程序,它是传统Win32应用程序的GUI前端.遗留应用程序在单独的线程中作为DLL运行.用户在UI中选择的命令在该"遗留线程"上调用.
如果"遗留线程"结束,GUI前端不能再做任何有用的事情了,所以我需要关闭WPF应用程序.因此,在线程的方法结束时,我打电话Application.Current.Shutdown().
由于我不在主线程上,我需要调用此命令.但是,我注意到Dispatcher也必须BeginInvokeShutdown()关闭调度程序.所以我的问题是:调用之间有什么区别
Application.Current.Shutdown();
Run Code Online (Sandbox Code Playgroud)
并打电话
Application.Current.Dispatcher.BeginInvokeShutdown();
Run Code Online (Sandbox Code Playgroud) 使用.NET 3.5
嗨,大家好,我正在为一个项目制作一个WPF应用程序,我只是看了一下Dispatcher和多线程的一些见解.我的计划的一个例子:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(
() =>_aCollection.Add(new Model(aList[i], aSize[i]))));
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(
() => _Data.Add(new DataPoint<double, double>(Id, aList[i]))));
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(
() => _historical[0].Add(aList[i])));
Run Code Online (Sandbox Code Playgroud)
据我所知,当另一个线程访问除创建它之外的对象时,WPF不喜欢.然而,我认为必须有一个更好的方法,而不是让这么多的调度员调用,有人可能请至少把我推向正确的方向(如果有更好的解决方案).
干杯,Sparky
我目前正在使用一个可观察的集合来存储我的ListView数据对象.将新对象添加到集合中工作正常,listView正确更新.但是,当我尝试更改集合中对象的某个属性时,listView将无法正确更新.例如,我有一个可观察的集合DataCollection.我试试
_DataCollections.ElementAt(count).Status = "Active";
Run Code Online (Sandbox Code Playgroud)
由于按下按钮,我在长时间操作之前执行此更改.listView不会反映更改.所以我补充一下myListView.Items.Refresh(); 这是有效的,但是在button_click方法完成之前,listView不会刷新,这在那时是不行的.例如:
button1_Click(...)
{
_DataCollections.ElementAt(count).Status = "Active";
myListView.Items.Refresh();
ExecuteLongOperation();
_DataCollections.ElementAt(count).Status = "Finished";
myListView.Items.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
状态将永远不会转到"活动",它将在方法完成后直接转到"已完成".我也尝试使用这样的调度程序:
button1_Click(...)
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
(NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status = "Active"; myListView.Items.Refresh(); });
ExecuteLongOperation();
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
(NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status = "Finished"; myListView.Items.Refresh(); });
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎也不正常.任何提示或想法将不胜感激.
我有这样的问题:有一些方法
private List<int> GetStatusList()
{
return (List<int>)GetValue(getSpecifiedDebtStatusesProperty);
}
Run Code Online (Sandbox Code Playgroud)
在主线程中调用它 - 我使用
`delegate List<int> ReturnStatusHandler();` ...
this.Dispatcher.Invoke(new ReturnStatusHandler(GetStatusList));
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做,使用lambda表达式而不是自定义委托和方法?
我正在研究WPF应用程序.我有一个名为"Status_label"的标签MainWindow.xaml.我想从不同的类(signIn.cs)更改其内容.通常我能做到这一点
var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWin.status_lable.Content = "Irantha signed in";
Run Code Online (Sandbox Code Playgroud)
但我的问题是,当我试图通过signIn.cs类中的不同线程访问它时,它会给出一个错误:
The calling thread cannot access this object because a different thread owns it.
Run Code Online (Sandbox Code Playgroud)
我可以通过使用Dispatcher.Invoke(new Action(() =>{..........或其他方式解决这个问题吗?
编辑: 我将从不同的类调用此标签更改操作 - 以及单独的线程
MainWindow.xaml
<Label HorizontalAlignment="Left" Margin="14,312,0,0" Name="status_lable" Width="361"/>
Run Code Online (Sandbox Code Playgroud)
SignIn.cs
internal void getStudentAttendence()
{
Thread captureFingerPrints = new Thread(startCapturing);
captureFingerPrints.Start();
}
void mySeparateThreadMethod()
{
var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWin.status_lable.Dispatcher.Invoke(new Action(()=> mainWin.status_lable.Content ="Irantha signed in"));
}
Run Code Online (Sandbox Code Playgroud)
line …
当用户点击按钮时,我想停止在main()中启动的调度程序.
private void button_start_Click(object sender, RoutedEventArgs e)
{
...
Camera.EventFrame -= onFrameEventFocusCam; //unsubscribe event
while (!Dispatcher.HasShutdownStarted)
{
// test if Dispatcher started shutdown --> ok, it does but never finishs...!
}
...
}
private void onFrameEventFocusCam(object sender, EventArgs e)
{
...
Dispatcher.Invoke(new Action(() =>
{
// Convert bitmap to WPF-Image
var bmp = new Bitmap(bitmap);
var hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image_fokus.Source = wpfBitmap;
image_fokus.Stretch = System.Windows.Media.Stretch.UniformToFill;
DeleteObject(hBitmap);
bitmap.Dispose();
}));
GC.Collect();
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我在以下位置停止时收到以下错误消息onFrameEventFocusCam …
这仅在x64版本中发生
WindowsBase.dll中发生类型为"System.ArithmeticException"的未处理异常附加信息:算术运算中出现溢出或下溢.
在线崩溃
gridcontainer.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
Run Code Online (Sandbox Code Playgroud)
如果我删除该行,它会在没有可用源的情况下开始更新布局时立即崩溃
00000101 test ecx,ecx
00000103 je 0000000000000124
00000105 lea rdx,[rbp+18h]
00000109 mov rcx,qword ptr [rbp+70h]
0000010d call 0000000000216100
00000112 cmp byte ptr [rdi],0
Run Code Online (Sandbox Code Playgroud)
你知道怎么预防吗?
例外:
System.ArithmeticException: Overflow or underflow in the arithmetic operation.
at System.Windows.UIElement.RoundLayoutSize(Size size, Double dpiScaleX, Double dpiScaleY)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, …Run Code Online (Sandbox Code Playgroud) 我正在和Akka一起工作,我们仍然要相互了解.
我的方案是:我为主管(父)演员选择了一个非默认调度员,他的角色是管理(监督)并创建子演员来完成工作.
问题:儿童演员是否继承了父母的演员?
我知道您可以为配置中指定的父actor的子actor 明确指定一个不同的调度程序.
akka.actor.deployment {
/my-parent-actor {
dispatcher = dispatcher-for-parent
}
"/my-parent-actor/*" {
dispatcher = dispatcher-for-children
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果您指定父actor调度程序,而没有明确指定子actor的调度程序,是否继承父项actor的子项.
我需要编写一个围绕第三方api的包装器,它会使用消息泵,因此根据是否在UI线程(例如在wpf应用程序中)实例化包装器(例如控制台应用程序),需要对其进行非常不同的处理).
如果它不在UI线程上运行,那么我需要一个调度程序并实现我自己的消息泵.
为此,我需要知道包装器是否在wpf应用程序中实例化.仅确定实例化是否发生在UI线程上是不够的(即使在wpf应用程序中,实例化包装器的线程可能不是UI线程).
有什么办法我可以弄清楚我是在一个带有消息泵或控制台应用程序的wpf或windows窗体环境中我必须实现自己的消息泵吗?
谢谢
dispatcher ×10
c# ×7
wpf ×7
.net ×1
akka ×1
c#-4.0 ×1
callback ×1
expression ×1
inheritance ×1
lambda ×1
listview ×1
mainwindow ×1
parent-child ×1
scala ×1
shutdown ×1
ui-thread ×1