3
using System;
using System.Data.SQLite;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;
using Tulpep.NotificationWindow;   

public partial class Form1 : Form
{
    System.Timers.Timer timer = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void buttonStart_Click(object sender, EventArgs e)
    {
        if (timer == null)
        {
            timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(ObjTimer_Elapsed);
            timer.Interval = 10000;
            timer.Start();
        }
    }

    private void ObjTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        try
        {
            PopupNotifier pop = new PopupNotifier();
            pop.TitleText = "Test";
            pop.ContentText = "Hello World";
            pop.Popup();

          //MessageBox.Show("");      !!!  here is problem  !!!
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

Here i am using Tulpep notification for create desktop notification. I have one start button in my form. When start button clicked, timer start to pop desktop notification. But it is shows notification only when i not comment on MessageBox.Show("");. and if i remove or comment MessageBox.Show(""); it is not showing notification. I debug in both case, there is no error or exception in both case.

Is any one have idea why is this happening?

I am using .net framework 4.5.2,visual studio 2015, windows 8.

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Blue
  • 312
  • 2
  • 12
  • Just a suspicion: Have you tried to make sure that `pop.PopUp()` is executed on the GUI-Thread? Not sure if that is necessary. – Fildor Aug 30 '17 at 08:18
  • 1
    Check out these questions : https://stackoverflow.com/questions/31270559/sending-a-notification-popup-in-winforms-from-another-thread & https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c/18033198#18033198 – PaulF Aug 30 '17 at 08:20
  • Look at the demo app: https://github.com/Tulpep/Notification-Popup-Window/blob/master/DemoApp/Form1.cs Did you set the Delay? – Fildor Aug 30 '17 at 08:20
  • by default Delay is 1 second, @Fildor – Blue Aug 30 '17 at 08:22
  • @Blue While that may be true, I've seen OPs omitting such code for "simplification of the snippet" and thus hiding code that overrides defaults. That's why I ask specifically. – Fildor Aug 30 '17 at 08:24

3 Answers3

7

PopupNotifier needs to be called out of the UI-Thread. Since the handler of your timer runs in a different thread you need to invoke your form to solve the problem.

this.Invoke((MethodInvoker)delegate
{
    PopupNotifier pop = new PopupNotifier();
    pop.TitleText = "Test";
    pop.ContentText = "Hello World";
    pop.Popup();
});
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
2

Create a static class ControlExtensions:

public static void InvokeOnUiThreadIfRequired(this Control control, Action action)
{
    if (control.InvokeRequired)
    {
        control.BeginInvoke(action);
    }
    else
    {
        action.Invoke();
    }
}

After that go again at the line where you call the Tulpep.NotificationWindow. and assign the main form to a variable like this:

//popup var is the notificationwindow inside form1 
Form1 ff = (Form1)Application.OpenForms["Form1"];

ff.InvokeOnUiThreadIfRequired(() =>
{
    ff.popup.Image = Properties.Resources.info_icon; //icon from resources
    ff.popup.TitleText = title; // some text here
    ff.popup.ContentText = contentMessage; // some text here
    ff.popup.Popup();
});

Now you invoke the main form and show the NotificationWindow

scopchanov
  • 7,966
  • 10
  • 40
  • 68
ldal
  • 21
  • 2
0

I had the same problem but with Task.Run(), I tried calling Popup inside SomeMethod with no luck. Solved using Invoke. Hope this helps someone.

Task.Run(() => {

                SomeMethod(); //Some method that executes in background

                //Popup when SomeMethod is finished using Fruchtzwerg answer
                this.Invoke((MethodInvoker)delegate
                {
                    PopupNotifier pop = new PopupNotifier();
                    pop.TitleText = "Test";
                    pop.ContentText = "Hello World";
                    pop.Popup();
                });
            });
4lex
  • 49
  • 10