Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to set achieve skype like notification for my wpf application tired solutions to achieve this from code behind but either getting stuck at an error "The calling thread cannot access the object because different thread owns it" or when i create a fresh thread and add overlay to TaskbarItemInfo nothing happens

What I have tried:

public void createIcon(string countNot)
        {
            try
            {
                RectangleF rectF = new RectangleF(0, 0, 40, 40);
                Bitmap bitmap = new Bitmap(40, 40, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bitmap);
                g.FillRectangle(System.Drawing.Brushes.White, 0, 0, 40, 40);
                g.DrawString(countNot, new Font("Arial", 25), System.Drawing.Brushes.Black, new PointF(0, 0));

                IntPtr hBitmap = bitmap.GetHbitmap();

                ImageSource wpfBitmap =
                    Imaging.CreateBitmapSourceFromHBitmap(
                        hBitmap, IntPtr.Zero, Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());
                
                new Thread(() =>
                {

                    Thread.CurrentThread.IsBackground = false;
                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate {

                        TaskbarItemInfo ittem = new TaskbarItemInfo();
                        ittem.Overlay = wpfBitmap;
                        

                    }, null);
                }).Start();
               
                





            }
            catch (Exception ex)
            {
                string path = @"C:\Logs\logs.txt";
                File.AppendAllLines(path, new[] { "---------------------" + "\rn" + ex.Message + "\n" + " stack trace: " + ex.StackTrace });

            }
        }
Posted
Updated 30-Jan-19 23:46pm
v2
Comments
[no name] 23-Jan-19 23:28pm    
Seems like a lot of trouble for nothing. WPF does not require this low-level crud.
Harmohan Singh 24-Jan-19 2:35am    
Didn't got you Gerry
Richard Deeming 4-Feb-19 14:25pm    
new Thread(() =>
{
    Thread.CurrentThread.IsBackground = false;
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate {
        TaskbarItemInfo ittem = new TaskbarItemInfo();
        ittem.Overlay = wpfBitmap;
    }, null);
}).Start();


Why are you starting a new thread, just to call back into the UI thread?

If you're already on the UI thread, just create and update the TaskbarItemInfo directly. If you're on a non-UI thread, just call Application.Current.Dispatcher.BeginInvoke directly. Neither case requires the new Thread(...).Start() wrapper.

Would somehting like this help?

WPF NotifyIcon[^]
 
Share this answer
 
 
Share this answer
 
Comments
Richard Deeming 4-Feb-19 14:19pm    
Isn't that the same thing that's now built-in via TaskbarItemInfo[^]?
Graeme_Grant 4-Feb-19 19:35pm    
I haven't looked at it in a long time ... will check and update my code ... thanks

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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