Label.Text = Struct.Value(Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException)
我正在开发一个应用程序,用于轮询 ISP(下载配额)的使用情况。我已经尝试通过“new Thread(ThreaProc)”对此进行线程化,但这不起作用,现在尝试基于 IAsyncResult 的方法,它可以做同样的事情...我不知道如何纠正,请帮忙?
需要了解的:
// Global
public delegate void AsyncPollData(ref POLLDATA pData);
// Class scope:
private POLLDATA pData;
private void UpdateUsage()
{
AsyncPollData PollDataProc = new AsyncPollData(frmMain.PollUsage);
IAsyncResult result = PollDataProc.BeginInvoke(ref pData,
new AsyncCallback(UpdateDone), PollDataProc);
}
public void UpdateDone(IAsyncResult ar)
{
AsyncPollData PollDataProc = (AsyncPollData)ar.AsyncState;
PollDataProc.EndInvoke(ref pData, ar);
// The Exception occurs here:
lblStatus.Text = pData.LastError;
}
public static void PollUsage(ref POLLDATA PData)
{
PData.LastError = "Some string";
return;
}
I have an app that I'm working on that polls usage from an ISP (Download quota). I've tried threading this via 'new Thread(ThreaProc)' but that didn't work, now trying an IAsyncResult based approach which does the same thing... I've got no idea on how to rectify, please help?
The need-to-know:
// Global
public delegate void AsyncPollData(ref POLLDATA pData);
// Class scope:
private POLLDATA pData;
private void UpdateUsage()
{
AsyncPollData PollDataProc = new AsyncPollData(frmMain.PollUsage);
IAsyncResult result = PollDataProc.BeginInvoke(ref pData,
new AsyncCallback(UpdateDone), PollDataProc);
}
public void UpdateDone(IAsyncResult ar)
{
AsyncPollData PollDataProc = (AsyncPollData)ar.AsyncState;
PollDataProc.EndInvoke(ref pData, ar);
// The Exception occurs here:
lblStatus.Text = pData.LastError;
}
public static void PollUsage(ref POLLDATA PData)
{
PData.LastError = "Some string";
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
跨线程更新值并不安全,因此编译器会警告您。通过使用 Invoke() ,传递的代码将在 GUI 线程中调用,因此您可以在 GUI 线程中更新 GUI 值,这是安全的。
Updating values across threads isn't safe so the compiler warns you. By using
Invoke()
the passed code will be called in the GUI thread, so you're updating a GUI value in the GUI thread, which is safe.您可以为自己创建一个新类并创建如下扩展:
然后您可以在代码中的任何位置使用它,如下所示:
您可以为其他事物创建多个类似的扩展,例如
CheckBox
、RadioButtons 在同一类中。这样您就可以拥有易于记忆和使用的扩展方法。
当然,您也可以创建一个像这样的普通方法(注意 Label 旁边缺少
this
):并使用如下代码:
You could create yourself a new class and create extensions like this:
And then u use this anywhere in your code like this:
You can create multiple similar extensions for other things like
CheckBox
,RadioButtons
in same class. That way you can have an easy to remember and use extension methods.Of course you could also create a normal method like this (notice lack of
this
next to Label):and use the code like this:
您的控件是在线程 A [绘制和处理 Windows 消息的线程] 上创建的,因此线程 B [监视器] 无法访问它 [万岁竞争条件],看看这个:
如何从 C# 中的另一个线程更新 GUI?
干杯
Your control was created on thread A [The one that paints and handles windows messages] so thread B [Monitor] cannot access it [Hurray for race-conditions], take a look at this:
How to update the GUI from another thread in C#?
Cheers
[更多说明:我正在使用 .NET 3.0]
即使我使用“如何从 C# 中的另一个线程更新 GUI?”中描述的方法也是如此该函数的其他部分 (UpdateDone) 失败(即使与线程无关的部分也会失败,因为类的某些部分已在当前线程之外访问)。
如果我错了,请纠正我,但由于调用了 EndInvoke,线程应该已终止。因此,不应该存在“竞争条件”;所有数据再次归主线程所有,我应该可以自由地对数据执行我想要的操作...?
我在整个课程中使用该结构中的多个项目;有更好的方法吗?你会怎么做?
基本限制:
* 单个函数完成数据轮询,其想法是不要让它阻塞 UI 交互。
* 因此该函数必须是异步的
* 需要通知(异步)主 Form 类 (frmMain) 相关功能的完成,并根据需要更新其 GUI。
* 当然,首先,函数获得的数据需要能够被 frmMain 的成员轻松访问
(天啊,使用 C++ 线程化要简单得多)
[More notes: I'm using .NET 3.0]
Even when I use the method described in 'How to update GUI from another thread in C#?' other parts of that function (UpdateDone) fails (Even parts that are nothing to do with threading fails because certain parts of the class have been accessed outside of the current thread).
Correct me if I'm wrong, but since EndInvoke is called, the thread should have terminated. Therefore no "Race conditions" should exist; all data is owned by the primary thread again and I should be free to do what I want with the data...?
There are multiple items in that struct that I use throughout the class; Is there a better way of doing this? How would you do it?
Basic constraints:
* A single function does the data polling, the idea is not to let it block UI interaction.
* Therefore that function must be Asynchronous
* The main Form class (frmMain) needs to be notified (Async) of the completion of the function in question and update it's GUI as required.
* And of course primarily, that data obtained by the function needs to be easily accessible to the members of frmMain
(God, threading was so much simpler with C++)
线程睡眠方法可能适合你
Thread Sleep method may be work for you