1

I did an smt email sending using C# program. The problem i am facing is my pop is not coming. when i press the button. Email is sending properly and the form is closing also.

I want the email to send first and then popup to display, then the application need to closed. But at this moment email is sent perfectly and form is closing. When i remove the this.close() the pop up is coming and email is sending, but form..:-(.

How to resolve this issue.

My codes

private void button7_Click(object sender, EventArgs e)
    {

            //System.Threading.Thread.Sleep(4000);

            SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("stacy1983@gmail.com", "sorrynopass");
            MailMessage msg = new MailMessage();
            msg.To.Add("jack.margret@gmail.com");
            msg.From = new MailAddress("stacy1983@gmail.com");
            msg.Subject = ("hello good");
            msg.Body = ("congo");
            client.Send(msg);
        taskbarNotifier3.CloseClick+=new EventHandler(CloseClick);
         taskbarNotifier3.Show("Email Successfully Sent!!!", "GOOB BYE!!!.", 500, 3000, 500);
         this.Hide();

         System.Timers.Timer aTimer = new System.Timers.Timer(3000);
         aTimer.Elapsed += OnTimedEvent;
         aTimer.Enabled = true;
    }

    private void CloseClick(object sender, EventArgs e)
        {
       this.Close();
        }

    private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
        this.Close(); 

        }

// Error called Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.

3 Answers3

0

Initialize a timer and set its interval to 100ms.

Register timer.tick event and check for the taskbarNotifier3 State in that event. If the state is hidden, then close the form.

You can check State like this:

TaskbarNotifier.TaskbarStates state = taskbarNotifier1.TaskbarState;
            if (state == TaskbarNotifier.TaskbarStates.hidden)
                this.Close();

Start the timer after showing the PopUp:

taskbarNotifier3.Show("Email Successfully Sent!!!", "GOOB BYE!!!.", 500, 3000, 500);
timer1.Start();
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • sorry nothing is coming.. no popup. when i gave `taskbarNotifier3.ShowDialog` –  Aug 23 '14 at 07:50
  • i am suing the pop up from this site.. please have a look.. its actually running in the back ground.. So my pop is like it wont interupt you... if your doing a job in winform,,, http://www.codeproject.com/Articles/3285/TaskbarNotifier-a-skinnable-MSN-Messenger-like-pop –  Aug 23 '14 at 07:56
  • 1
    You will have to find when the popup closes, at that moment you can close the form. You can create a timer to check it out – Shaharyar Aug 23 '14 at 08:06
  • As Shaharyar said, use can also use Thread.Sleep(3000) before calling this.Close(). – Alireza Aug 23 '14 at 08:15
  • `taskbarNotifier3.Show("Email Successfully Sent!!!", "GOOB BYE!!!.", 500, 3000, 500);` popup close after 3000 ms. But its running back ground boss how can i keep the form still for 3 sec. –  Aug 23 '14 at 08:17
  • 1
    Don't use a TaskbarNotifier but see my answer for the desired solution. It's straightforward and easy ;) – Youp Bernoulli Aug 23 '14 at 08:23
  • 1
    I've downloaded that demo code and has gone through the code. You can create your own event for closing the popup. But it'll be time consuming. So I've created an example using timer. Check out the updated answer. I hope now you'll solve your problem. – Shaharyar Aug 23 '14 at 08:29
  • @Shaharyar i did as you said now my form is closing, the moment i run the application. By defualt its going to the closing mode, SInce the taskbarNotifier3 is hidden i guess so. –  Aug 23 '14 at 09:20
0

Apparently the TaskbarNotifier is a child of the Form and will close as a result of closing the Form. Nothing weird so far. As Shaharyar is suggesting ShowDialog should do the trick. But from your code it's not known what kind of control taskbarnotifier is and if it exposes a method like ShowDialog().

What you can do is create a simple .NET Framework included MessageBox and show it (by default it is shown modal -> will block other UI thread processing until user closes the Messagebox by clicking Ok or something.)

See this example for the right way to do it.

Community
  • 1
  • 1
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • And if you really want to use a TaskbarNotifier then set up a timer event for closing the application / Form after x seconds or so. Then it will show the notifier and after x seconds (when a timer.tick event fires) you call this.Close(). – Youp Bernoulli Aug 23 '14 at 08:24
  • i know. if i create a simple message box my problem is solved. but i dont want to settle down with with a cheap dialogbox. i want to see my form more profesional. –  Aug 23 '14 at 08:56
0

handle the close click event of the TaskbarNotifier and place the this.Close() in that event.

{
    client.Send(msg);
    taskbarNotifier3.CloseClick+=new EventHandler(CloseClick);
    taskbarNotifier3.Show("Email Successfully Sent!!!", "GOOB BYE!!!.", 500, 3000, 500);
    this.Hide();

    System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();
    aTimer.Interval = 3000;
    aTimer.Tick += OnTimedEvent;
    aTimer.Enabled = true;
}


private void CloseClick(...)
{
    this.Close();
}

private void OnTimedEvent(Object source, EventArgs e)
{
    this.Close();
}
Shell
  • 6,818
  • 11
  • 39
  • 70
  • This event will only fire when you click on `close` on popup. Automatically hiding will not fire this event. – Shaharyar Aug 23 '14 at 08:36
  • Then `System.Timer` should be also added with it. Coz.. we cannot assume that user will wont click on close button. – Shell Aug 23 '14 at 08:44
  • @Shell your code is working but.. when i click the close button of popup only the form is closing .. else it stays like that. I want to automatically close the form, when i press the button7_Click. –  Aug 23 '14 at 08:48
  • @Stacy you can hide it with `this.Hide()` instead of `this.Close()` – Shell Aug 23 '14 at 08:49
  • @Shell which one i should give hide –  Aug 23 '14 at 08:57
  • @Shell its giving Error'ElapsedEventArgs' could not be found (are you missing a using directive or an assembly reference?) –  Aug 23 '14 at 09:00
  • the current form until popup not get closed. look at the code above. you need to add that just after `popup.Show()` method. – Shell Aug 23 '14 at 09:01
  • 1
    have u imported `System.Timers` namespace check this [msdn](http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx) – Shell Aug 23 '14 at 09:02
  • @Shell please find the edited code as you said. Now its showing an error called - error here .Error 1 Keyword 'this' is not valid in a static property, static method, or static field initializer –  Aug 23 '14 at 09:08
  • 1
    ohh.. sorry remove the `static` keyword from the `OnTimedEvent` event. – Shell Aug 23 '14 at 09:11
  • @Shell now an error called Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on. I guess we are close to the solution. please help me out –  Aug 23 '14 at 09:16
  • 1
    ok, rather than going to invoking delegate and making this more complex. i have changed the `System.Timers.Timer` to `System.Windows.Forms.Timer` look at the updated code. – Shell Aug 23 '14 at 09:27