跨线程操作无效:控制... c#中的错误 [英] Cross-Thread operation not valid: Control... An error in c#
问题描述
大家好,我在这里有一个使用线程的类,但是当我运行代码时,出现此错误跨线程操作无效:控制...如何解决此问题?我有这段代码,如果数据库中的值更改为lock,则将ScreenSaver类设置为可见或已处置;当数据库中的数据设置为open时,将其设置为invisible.这是代码的示例
Hi guys i have a class here that make use of a thread but when i run the code i get this error Cross-Thread operation not valid: Control... how to solve this? i have this code that set the ScreenSaver Class to visible or disposed it if the value in the database changed to lock and set it to invisible when the data in the database is set to open... here is a sample of the code
public partial class client : Form
{
public static bool locks= false;
public client()
{
InitializeComponent();
checkLock cl = new checkLock();
Thread thread = new Thread(cl.checkLocks);
thread.IsBackground = true;
thread.Start();
}
}
class checkLock
{
ScreenSaver s = new ScreenSaver();
public void checkLocks()
{
while(true){
try
{
if (DBConnect.isExists("some checking in database..."))
{
/* show the screensaver when the value in the database changes to open */
s.Show();
}
else
{
/* Hide the screensaver when the value in the database changes to open */
s.Hide();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Thread.Sleep(400);
}
}
}
}
我希望您能提前帮助我...
i hope you can help me guys thanks in advance...
推荐答案
无法对任何UI对象的方法和属性进行跨线程调用.所有此类调用只能在UI线程中完成,对于System.Windows.Forms
和WPF而言也是如此.如何使用不同线程的UI?您需要通过Control
或Dispatcher
Invoke
或BeginInvoke
方法使用线程间调用机制.
您可以在此处找到详细的说明和说明: Control.Invoke()与Control. BeginInvoke() [ ^ ].
—SA
Cross-thread call to any UI object''s methods and properties is impossible. All such calls can only be done in the UI thread, forSystem.Windows.Forms
and WPF as well. How to work with UI from a different thread? You need to use inter-thread invocation mechanism viaControl
orDispatcher
Invoke
orBeginInvoke
methods.
You will find detailed explanation and instructions here: Control.Invoke() vs. Control.BeginInvoke()[^].
—SA
之所以出现此错误,是因为您试图在辅助线程上进行UI更改.
试试这个链接.
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx [ ^ ]
您将需要进行检查.
这是所需的代码.
Hi,
You are getting this error because you are trying to make UI changes on a secondary thread.
Try this link.
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx[^]
You will have to do a check.
here are the code required.
class checkLock
{
private delegate void ShowHideFormCallback(bool Show); //HERE
ScreenSaver s = new ScreenSaver();
public void checkLocks()
{
while(true){
try
{
if (DBConnect.isExists("some checking in database..."))
{
/* show the screensaver when the value in the database changes to open */
// s.Show();
ShowHideForm(true);//HERE
}
else
{
/* Hide the screensaver when the value in the database changes to open */
//s.Hide();
ShowHideForm(false);//HERE
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Thread.Sleep(400);
}
}
//HERE
private void ShowHideForm(bool Show)
{
if (s.InvokeRequired)
{
s.Invoke(new ShowHideFormCallback(ShowHideForm),Show);
}
else
{
if (Show)
{
s.Show();
}
else
{
s.Hide();
}
}
}
}
问候
特伦斯(Terence)
Regards
Terence
跨线程操作无效-当您尝试从未创建控件的线程访问控件时,通常会出现此错误.
我不确定checkLock
中的控件,但似乎hide/show
方法尝试修改对象s
的某些内部属性(如果您注意的话,您会看到s
是在主线程中创建的) ,然后从第二个线程访问该问题,有2种解决方案:1)您可以在调用checkLocks()
方法的同一线程中创建checkLock
对象,或者2)我更喜欢使用BeginInvoke()
而不是创建新线程:
Hi,
Cross-thread operation not valid - it is an error which normally appears when you try to access a control from a thread which did not create the control.
I''m not sure about the controls from yourcheckLock
but it seems thathide/show
methods try to modify some internal properties of objects
(if you pay attention you can see thats
is created in main thread, and then is accessed from the second thread. There are 2 solutions for this problem: 1) you can create yourcheckLock
object in the same thread which calls thecheckLocks()
method, or 2) I would prefer usingBeginInvoke()
instead of creating a new thread:
public partial class client : Form
{
public static bool locks= false;
public client()
{
InitializeComponent();
checkLock cl = new checkLock();
BeginInvoke(new Action(()=>
{
cl.checkLocks();
}));
}
}
有关BeginInvoke的更多信息- http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx [^ ]
问候
More about BeginInvoke - http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx[^]
Regards
这篇关于跨线程操作无效:控制... c#中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!