Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 14-Aug-11 23:40pm
v2
Comments
Herman<T>.Instance 15-Aug-11 5:34am    
are you doing things in another thread? (Using a backgroundworker?)

use the Dispatcher to invoke your code on the ui thread.

example:
C#
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[^]
 
Share this answer
 
v2
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900