尝试无法成功使用TaskbarNotifier C# [英] Trying to use TaskbarNotifier not successfully C#
问题描述
我正在尝试使用TaskbarNotifier,这是我的项目中C#中的可换肤的类似于MSN Messenger的弹出窗口,我在TaskbarNotifier随附的cs文件中有问题.
在此行:
SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-1, BackgroundBitmap.Width, 0);
我收到一条错误消息:
跨线程操作无效:从创建该线程的线程之外的线程访问控件''''.
我该如何绕过?我不知道.
谢谢
I am trying to use TaskbarNotifier, a skinnable MSN Messenger-like popup in C# in my project, I have a problem in the cs file that comes with the TaskbarNotifier.
At the line:
SetBounds(WorkAreaRectangle.Right-BackgroundBitmap.Width-17, WorkAreaRectangle.Bottom-1, BackgroundBitmap.Width, 0);
I get an error saying:
Cross-thread operation not valid: Control '''' accessed from a thread other than the thread it was created on.
How can I bypass it? I have no clue.
Thanks
推荐答案
使用Dispatcher
在ui线程上调用您的代码.
例如:
use theDispatcher
to invoke your code on the ui thread.
example:
public void MyMethod()
{
if(!Dispatcher.CurrentDispatcher.CheckAccess())
{
Dispatcher.CurrentDispatcher.Invoke((Action)MyMethod, null);
return;
}
}
http://msdn.microsoft.com/en-us/library/system. windows.threading.dispatcher.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^]
您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.Dispatcher
的Invoke
或BeginInvoke
方法(对于Forms或WPF)或System.Windows.Forms.Control
(仅对于Forms).
在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].
—SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the methodInvoke
orBeginInvoke
ofSystem.Windows.Threading.Dispatcher
(for both Forms or WPF) orSystem.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].
—SA
这篇关于尝试无法成功使用TaskbarNotifier C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!