- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
开发了一个运行良好的 Azure 服务总线消息接收器控制台应用程序 console app 。
控制台应用程序代码如下:
using System.IO;
using Microsoft.ServiceBus.Messaging;
class Program
{
static void Main(string[] args)
{
const string connectionString = "Endpoint=sb://sbusnsXXXX.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=bkjk3Qo5QFoILlnay44ptlukJqncoRUaAfR+KtZp6Vo=";
const string queueName = "bewtstest1";
var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);
try
{
queueClient.OnMessage(message => {
string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
Console.WriteLine(body);
message.Complete();
});
Console.ReadLine();
}
catch (Exception ex)
{
queueClient.OnMessage(message => {
Console.WriteLine(ex.ToString());
message.Abandon();
});
Console.ReadLine();
}
}
}
尝试转换为 WinForms 应用程序,以便我可以将服务总线消息显示为列表框中的字符串。
我使用控制台应用程序代码创建了一个新类 ( Azure
),并在主窗体中调用该方法。
Azure 级:
using System.IO;
using Microsoft.ServiceBus.Messaging;
public class Azure
{
public static void GetQueue(Form1 form)
{
const string connectionString = "Endpoint=sb://sbusnsXXXX.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=bkjk3Qo5QFoILlnay44ptlukJqncoRUaAfR+KtZp6Vo=";
const string queueName = "bewtstest1";
var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);
try
{
queueClient.OnMessage(message => {
string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
//Form1 f = new Form1();
form.listBox1.Items.Add(body);
Console.WriteLine(body);
message.Complete();
});
Console.ReadLine();
}
catch (Exception ex)
{
queueClient.OnMessage(message => {
Console.WriteLine(ex.ToString());
message.Abandon();
});
Console.ReadLine();
}
}
}
主窗体:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Azure.GetQueue(this);
}
}
代码可以编译,但是当收到新的服务总线消息时,出现以下异常:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.'
关于如何避免此异常的任何想法(请注意,我已尝试使用 InvokeRequired
但无法编译代码)?
(感觉我已经很接近了,因为当我停止并重新运行程序时,表单会加载列表框中的消息,如下所示:listbox with message!)
最佳答案
当然,你不能从另一个线程引用在UI线程中创建的控件;正如您所注意到的, Invalid Cross-thread operation
当您尝试执行以下操作时会引发异常:Windows 窗体应用程序必须是单线程的,STAThreadAttribute Class 中很好地解释了原因。文档。
注意:删除所有 Console.ReadLine()
,您不能在 WinForms 中使用它(没有控制台)。
这里有一些可能适合您的实现,按照与您的上下文相关的顺序排列(嗯,至少我是这么认为的。您选择您喜欢的)。
▶ Progress<T>
: 这个类使用起来非常简单。您只需要定义它的返回类型( T
类型,它可以是任何东西,一个简单的 string
,一个类对象等)。您可以就地定义它(调用线程方法的地方)并传递其引用。仅此而已。
接收引用的方法调用其 Report()方法,传递 T
定义的值.
该方法在创建 Progress<T>
的线程中执行对象。
如您所见,您不需要将 Control 引用传递给 GetQueue()
:
表格侧面:
// [...]
var progress = new Progress<string>(msg => listBox1.Items.Add(msg));
Azure.GetQueue(progress);
// [...]
Azure 类端:
public static void GetQueue(IProgress<string> update)
{
// [...]
try {
queueClient.OnMessage(message => {
string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
update.Report(body);
message.Complete();
});
}
// [...]
}
<小时/>
▶ SynchronizationContext ( WindowsFormsSynchronizationContext
) Post() :该类用于同步线程上下文,其Post()
方法将异步消息分派(dispatch)到生成类对象的同步上下文,由Current引用。属性(property)。
当然,参见Parallel Computing - It's All About the SynchronizationContext .
实现与之前没有太大不同:您可以使用 Lambda 作为 SendOrPostCallback
Post() 方法的委托(delegate)。
安Action<string>
delegate 用于发布到 UI 线程,而不需要传递对 Azure.GetQueue()
的 Control 引用。方法:
表格侧面:
// Add static Field for the SynchronizationContext object
static SynchronizationContext sync = null;
// Add a method that will receive the Post() using an Action delegate
private void Updater(string message) => listBox1.Items.Add(message);
// Call the method from somewhere, passing the current sync context
sync = SynchronizationContext.Current;
Azure.GetQueue(sync, Updater);
// [...]
Azure 类端:
public static void GetQueue(SynchronizationContext sync, Action<string> updater)
{
// [...]
try {
queueClient.OnMessage(message => {
string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
sync.Post((spcb) => { updater(body); }, null);
message.Complete();
});
}
// [...]
}
<小时/>
▶ Control.BeginInvoke() :您可以使用BeginInvoke()
在创建 Control 句柄的线程上异步执行委托(delegate)(通常作为 Lambda)。
当然,您必须将 Control 引用传递给 Azure.GetQueue()
方法。
这就是为什么在本例中,此方法的优先级较低(但您无论如何都可以使用它)。
<子> BeginInvoke()
不需要检查Control.InvokeRequired
:可以从任何线程调用此方法,包括 UI 线程。调用Invoke()
相反,需要进行该检查,因为如果从 UI 线程使用它可能会导致死锁
表格侧面:
Azure.GetQueue(this, Updater);
// [...]
// Add a method that will act as the Action delegate
private void Updater(string message) => listBox1.Items.Add(message);
Azure 类端:
public static void GetQueue(Control control, Action<string> action)
{
// [...]
try {
queueClient.OnMessage(message => {
string body = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
control.BeginInvoke(new Action(()=> action(body));
message.Complete();
});
}
// [...]
}
<小时/>
您还可以使用System.Windows.Threading.Dispatcher管理线程的排队工作项,调用其 BeginInvoke()
(首选)或 Invoke()
方法。
其实现类似于SynchronizationContext
one 及其方法被称为 Control.BeginInvoke()
方法已经提到了。
我不会在这里实现它,因为调度程序需要引用 WindowsBase.dll
(通常是 WPF),这可能会在非 DpiAware 的 WinForms 应用程序中导致不良影响。
您可以在这里阅读相关内容:
DPI Awareness - Unaware in one Release, System Aware in the Other
无论如何,如果您有兴趣,请告诉我。
关于c# - Winform服务总线消息接收方如何避免跨线程操作not valid异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62438141/
我有一个 winform 应用程序和一个可观察的设置,如下所示: Form form = new Form(); Label lb = new Label(); form.Controls.Add(l
在 Windows 窗体中实现多项选择选项的最佳方法是什么?我想从列表中强制执行单个选择,从默认值开始。 看起来 ComboBox 是一个不错的选择,但是有没有办法指定一个非空白的默认值? 我可以在代
如何在 WinForm 应用程序中保护我的 ConnectionString? 最佳答案 你不能。尽管您可以在 app.config 文件中加密连接字符串,但应用程序需要能够解密它,因此,始终可以检索
有谁知道像 DotNetBar 那样的 Winforms 面包屑样式导航。 http://www.devcomponents.com/dotnetbar/BreadCrumbHorizontalTre
我正在寻找在 Windows 窗体 C# 中实现多选启用列表框的方法。 有什么建议? 谢谢。 最佳答案 只需添加一个 ListBox控制和设置属性:SelectionMode = SelectionM
我有一个简单的 WinForms 应用程序,用于输入测试用例。自从我将此应用程序升级到 .NET 4.0 并将新的标签页添加到标签页控件以根据 XSD 架构验证 XML 以来,该应用程序一直随机崩溃。
在老式的 MFC 中,DDX 例程内置了表单条目的验证。例如,可以自动检查用户是否在用于数字输入的文本框中输入了字符串。 Winforms中有这样的机制吗?显然,可以为“onChange”等类型的事件
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
简单的问题,虽然办公室里似乎没有人知道,而且我找不到一个好的方法来问谷歌这个问题。在 winforms 中,如果您有一个处理事件的函数(在本例中是在 focusLost 上),那么该函数是否与触发该事
在 Winform 中,我有一个组框,其中有几个文本框控件。如果我删除组框,文本框也会被删除。它们以某种方式与 Groupbox 联系在一起,尽管我没有故意做任何事情来实现这种情况。问题 - 如何删除
我可以在哈希表中设置表单元素: $Hash = @{} $Hash.Main = New-Object System.Windows.Forms.Form $Hash.Main.Left = 0 $H
我是 Windows 开发新手。我开发了一个 WinForm 应用程序,它与串行设备通信并在图表上绘制数据。该应用程序应每天 7/7 24 小时运行。代码执行正确,但执行几个小时后,UI 卡住,操作系
有没有办法记录 Win Forms 应用程序中的所有点击?我想拦截点击并记录该操作以及导致该操作的控件的名称。 这可能吗? 提前致谢。 更新:我正在寻找一个应用程序范围的解决方案,是否没有办法将监听器
我不知道这是否会影响其他控件,但对于列表框和选中列表框,列表框的底部仅以一定的间隔随表单调整大小。 假设我有一个表单和一个列表框,该列表框与表单边缘的所有边都有 2px 间隙,并锚定在所有四个边上。现
我在大多数 WinForms 控件的设计 View 中看到“Tag”属性。我从未使用过此标签,并且想知道为什么我要使用它。 最佳答案 它允许您使用控件存储一些自己的数据。它在树控件中最有用,您可能希望
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果有,有人吗?鱼眼 (菜单/ Pane )窗体在 Ajax 应用程序中常见的控件: 例如:http://interface.eyecon.ro/demos/fisheye.html 用 google
这是 Determine Label Size based upon amount of text and font size in Winforms/C# 的倒数. 给定一个高度固定但宽度可变的矩形
我们公司正在研究为我们的开发团队采购 22"显示器。当前唯一的问题是我们的用户将使用较小的屏幕。 我们尝试使用屏幕网格工具(gridmove 和 nvidia 的 utils),但它们并不完全现实。
我有一个 winforms 应用程序,它在网络服务请求期间被锁定 我已经尝试使用 doEvents 来保持应用程序解锁,但它仍然不够响应, 我怎样才能绕过这个锁定,让应用程序始终响应? 最佳答案 最好
我是一名优秀的程序员,十分优秀!