问题标签 [cross-thread]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 从线程而不是 C# 中的原始线程更新 ListView
我已经知道要更新控件,我可以使用 Invoke Action
但这仅适用于单一数据的控制属性。现在我有一个线程需要更改列表视图项的列表视图集合,清除它然后使用新项目、新文本和图标图像进行更新。它们之间有什么不同,整数或布尔值与列表、数组或整数、布尔值甚至组件、控件的集合相比。我可以简单地添加吗
我可能需要我的列表视图的解决方案,但如果您有答案,请清楚地解释它为什么以及如何工作,因为我需要彻底了解它。你也可以在尝试这种做法时注意到我的风险。
编辑:问题不在于如何从其他线程更新控制。IT 关于何时以及为什么需要这样做而不是那样做。此外,我需要知道更新单个数据和更新整个数据集合之间的区别。
.net - 如何将文本设置为在另一个线程中创建的元素
在开始之前,我知道这个问题已经有了很多答案,但让我解释一下发生了什么。
我基本上想将一些文本附加到 RichTextBox 元素,它对我来说就像一个记录器来通知用户来自文件处理的每个操作,但是文本通过 for 循环附加到 RichTextBox,如果我在同一类“Form1.vb”UI 冻结,直到循环完成。
我决定在一个单独的线程中运行循环以避免 UI 冻结,这就是我的问题开始的地方。
Form1.vb
我无法从另一个线程访问 txtLogger 元素,所以我尝试了 MSDN 示例 https://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx?cs-save-lang =1&cs-lang=vb#code-snippet-2
它向我展示了如何使用委托访问进行线程安全调用的元素。
所以我编辑的代码是
Form1.vb
代码确实是这样工作的,文本附加到 RichTextBox,但所有代码都在同一个类 Form1.vb 中
在我原来的项目中,for循环是在另一个类中执行的,在这里我将它命名为“Class1.vb”。
那是代码示例
类1.vb
c# - 在创作者更新后获取 OnCollectionChanged 上的线程访问异常
在我的程序中,我有一个抽象类ObservableKeyedCollection<TKey, TItem>
,它继承自KeyedCollection<TKey, TItem>
并实现INotifyCollectionChanged
.
这个抽象类的实现绑定到一个ListBox
. 在此ListBox
,我通过双击编辑项目,并在接受后,从该ObservableKeyedCollection<TKey, TItem>
实现中删除已编辑项目的旧实例,并添加已修改的新实例。
在 Windows 10 创意者更新(1703,内部版本号 15063.250)之前,这一切都运行良好。自更新以来,ObservableKeyedCollection<TKey, TItem>
开始抛出InvalidOperationException
s 并显示以下消息:
调用线程无法访问此对象,因为不同的线程拥有它。
我没有在代码的这个区域使用任何异步操作。
整个堆栈跟踪会太长,但这里是开头的顶部OnCollectionChanged
:
在 System.Windows.Threading.Dispatcher.VerifyAccess() 在 System.Windows.Threading.DispatcherObject.VerifyAccess() 在 System.Windows.DependencyObject.GetValue(DependencyProperty dp) 在 System.Windows.Controls.Primitives.Selector.GetIsSelected(DependencyObject元素) 在 System.Windows.Controls.Primitives.Selector.SelectionChanger.CreateDeltaSelectionChange(List'1 unselectedItems, List'1 selectedItems) 在 System.Windows.Windows.Controls.Primitives.Selector.ItemSetIsSelected(ItemInfo info, Boolean value) .Controls.Primitives.Selector.SelectionChanger.End() 在 System.Windows.Controls.Primitives.Selector.RemoveFromSelection(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) 在 System.Windows.Controls .ItemsControl.OnItemCollectionChanged2(Object sender, NotifyCollectionChangedEventArgs e) 在 System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) 在 System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Controls.ItemCollection。 OnViewCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) at System.Windows.WeakEventManager.ListenerList'1.DeliverEvent(Object sender, EventArgs e, Type managerType) at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list) at System .Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32adjustedOldIndex, Int32adjustedNewIndex) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChangedargs ) 在 System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 在 TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 e:\Phil\Programozás\Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection.cs,行号:68 at TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) at [...]]]]]CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32adjustedOldIndex, Int32adjustedNewIndex) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.Colle OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at e:\Phil\Programozás\Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection.cs,行号:68 at TetheredSun.ObservableKeyedColle .RemoveItem(Int32 索引)在 [...]CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32adjustedOldIndex, Int32adjustedNewIndex) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.Colle OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) at TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) at e:\Phil\Programozás\Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection.cs,行号:68 at TetheredSun.ObservableKeyedColle .RemoveItem(Int32 索引)在 [...]ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32adjustedOldIndex, Int32adjustedNewIndex) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 在 Tether2 .OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 e:\Phil\Programozás\Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) 在 [...]ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs args, Int32adjustedOldIndex, Int32adjustedNewIndex) 在 System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 在 Tether2 .OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 e:\Phil\Programozás\Modulok\TetheredSun.1.0\TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) 在 [...]ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 在 TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 e:\Phil\Programozás\Modulok\TetheredSun1。 TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) at [...]ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 在 System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 在 TetheredSun.ObservableKeyedCollection'2.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 在 e:\Phil\Programozás\Modulok\TetheredSun1。 TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) at [...]0\TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) at [...]0\TetheredSun\ObservableKeyedCollection.cs,行号:68 在 TetheredSun.ObservableKeyedCollection`2.RemoveItem(Int32 index) at [...]
编辑1:
这是在创作者更新之前正常工作的违规代码部分(覆盖KeyedCollection<TKey, TItem>.RemoveItem(int index)
):
只有当我OnCollectionChanged
使用NotifyCollectionChangedAction.Remove
操作调用时,才会出现问题。将其替换为NotifyCollectionChangedAction.Reset
似乎可以避免异常:
我试图用Dispatcher
这里看到的问题来解决这个问题:https ://stackoverflow.com/a/22026686/2659699但是虽然我的调度程序不为空,但它的CheckAccess()
计算结果为真,并且我在NotifyCollectionChangedEventHandler.Invoke()
.
非常感谢您的想法和帮助。
c# - 如何在 C# 的循环中运行相同的后台工作程序并将其停止
我执行了一个循环 10 次的遗传算法,每次调用“FitnessFunction”函数时,我使用后台工作人员在拓扑上移动一个对象,一旦它到达特定点,然后我取消该后台工作人员并返回“遗传算法”功能...
不幸的是,我收到以下错误:
我试过的:
每次我调用“FitnessFunction”函数时运行后台工作程序,该函数负责在特定条件下停止它。
c# - 跨线程操作
尝试从主 MMI 线程以外的其他线程调用文本框时,我碰巧遇到了这个跨线程错误。我已经明白为什么会这样了。我想听听你对我解决这个问题的方式的看法。我使用它是因为我讨厌在代码中添加委托声明。
这是正确的方法吗?有没有更好更短的方法?
c# - 动态创建 toolStripMenuItem 时出错
我遇到了以下问题:我有一个配置文件和一个对此文件的侦听器。更改后,会触发一个方法 ( createMenuAndItems
),该方法会修改toolStripMenuItem
.
该方法createMenuAndItems
在加载表单和修改配置文件时调用。每当数组
有多个项目,当我修改配置文件时显示错误。在加载表单时工作正常。
Visual Studio 中显示的错误:
System.InvalidOperationException 未处理
HResult=-2146233079
消息=:跨线程操作无效:控件“topMenu”从创建它的线程以外的线程访问
实际消息是
Message=Operación no válida a través de subprocesos: Set tuvo acceso al control 'topMenu' desde un subproceso distinto aquel en que lo creó。
源 = System.Windows.Forms
StackTrace:
试过CheckForIllegalCrossThreadCalls = False;
但没有运气
奇怪的是,如果数组strArrays
只有一个值,它就像一个魅力。但是当添加更多项目时开始显示此错误
这是代码。
文件上的侦听器:
这是创建和修改toolStripMenuItem
谢谢!
winforms - WinForms 中的 async/await 调试:system.invalidoperationexception 跨线程操作无效
在 Visual Studio 2017 中通过 Ctrl+F5(不调试启动)启动/运行您的应用程序并使用 async/await 进行 winforms 的控件事件处理时。例如,按钮单击事件处理,您可以访问这些控件属性以进行读/写操作,但是当您按 F5 启动应用程序时,您会收到运行时错误消息:
要解决此问题,您必须使用众所周知的
代码构建。
问题:是否有任何/更优雅的方法来避免使用 .InvokeRequired而不使用以下代码示例片段中提供的条件编译:
更新
问题:以下代码示例statsProgress
代码构造是否是最佳/推荐的解决方案?
更新 2
这是最终的解决方案:
c# - 如何在循环中使用多个后台工作人员,在 C# 中为每个后台工作人员每次迭代开始新工作
我需要使用 4 个后台工作人员在特定坐标上并行移动 4 个对象,即一起启动它们并一起停止它们。
我写了一个循环来循环 50 次,每次我需要启动工人并且在他们完成他们的工作之后,就像Do_Work()
方法中那样停止它并在下一次迭代中再次启动它们,我编写了以下方法来调用工人:
它们是 4Do_Work()
个后台工作人员的 4 种方法,以下是其中之一:
问题是工作人员只运行一次,然后我停止执行并出现以下错误:
此 BackgroundWorker 当前很忙,无法同时运行多个任务。
注意:我每次都尝试创建新的后台工作人员,但它不能正常工作,并且当我声明 50x4 后台工作人员时,我的执行没有响应!!
后台工作人员注册如下:
c# - C# 串口不在文本框中显示输出
大家好,我对 C# 和 Arduino 很陌生。当我将文本发送到我的 Arduino 时,它不会将其发送回应用程序中的文本框。我可以将它发送到我的 Arduino,但是当我发送的消息必须在应用程序的文本框中时出现错误。
这是我收到错误的部分:
这是我的代码:
我的ino文件
我已经尝试了2个多小时,我无法在任何地方找到答案。
c# - 从 WPF 中的任何线程修改 Datagrid.Columns
(我使用 .Net 4.5 和 Visual Studio 2017。在帖子末尾添加了最小示例链接,如果您想自己查看这些内容,可以运行/崩溃而无需做任何事情)
我有一个包含 WPF 中的 DataGrid 的 UserControl。它绑定到包含 DataTable 和自定义列列表的 Viewmodel,以便在我目前无法删除它们时隐藏其中的一些。
DataGrid.Columns 是只读的,所以在 UserControl.DataContextChanged(我在下面解释原因)中,我得到了新的 DataContext,清除了我的 grid.Columns 集合,并用我在加载时读取的 DataGridColums 的自定义列表提供给它。
最初,我正在做一个同步加载。一切运行良好(我正在加载很多东西,包括这些项目的列表)。但是我已经把加载放到了一个后台工作程序中(运行得几乎没有问题),后来,使用 Task.Run() 将后台工作程序替换为异步加载。
我窗口中的所有其他项目列表都加载得很好,代码运行良好。但是当我试图用其他的替换 mygrid.Columns 时,我有一个 InvalidOperationException 说调用线程无法访问这个对象,因为另一个线程拥有它。
我尝试了很多东西,很多来自 myGrid.Dispatcher 的 BeginInvoke、App 的当前 Dispatcher 等等,以及我找到的每一段异步代码,但我不知道如何向这个 ObservableCollection 添加一个简单的项目。
我见过自定义 asyncObservableCollection 但不能使用它们,DataGrid.Columns 是只读的。
我对我认为 UserControl_DataContextChanged 属于 UI 线程这一事实感到困惑,因此它应该能够安全地更改用户控件。
我在这里上传了一个“最小示例”=> https://files.fm/u/k2srba6m 问题出在 ItemViewmodel.FilterColumns 中,就像我的原始代码一样(希望这个解决方案也适用于原始代码)
任何帮助将不胜感激(对不起我的英语)