• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

How do I know when my WinForms program has lost focus?

I need to set a breakpoint in my Winforms application, on a machine I can not watch constantly. But I want to email myself the second the breakpoint has been hit.

So I'd like to catch the event where my program loses focus. I assume when Visual Studio grabs focus, to display the breakpoint, my code would execute for that lose focus event.

What event is that? I would rather NOT be that my FORM has lost focus, since I'd get emailed any time another form in my program opens. Instead, I'd like it to be that my PROGRAM has lost focus to another program, in this case Visual Studio.

How do I do that? What's the property name?

Thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 8
  • 5
  • 4
  • +2
2 Solutions
 
magicdlfCommented:
I am afraid when the breakpoint triggers, the lose focus event (if there's one) will never be triggered because it happens later then your breakpoint. I see your question, I answered in another of your thread. If your program is not that big (thus it is easy to compile), I suggest you write a function called breakpoint() and send the email to yourself in that function. Now you can set the breakpoint at the last line of the function, and insert breakpoint() everywhere you want to watch.
0
 
eitamaCommented:
There is an easier way,

In the application that you are debugging, start a Thread, which writes to a file every 10 seconds.
With an external script, check for a new file every 20 seconds. (new being at least 11 seconds old).

If the program you are debugging halts cause of a break point, it will stop creating those files.
The external script will recognize files are not being created, and will e-mail you.

Eitam.
0
 
magicdlfCommented:
Eitam, that one is really smart. : )
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
newbiewebAuthor Commented:
I like Eitam's solution. The problem with explicitly pre-decising where I want to set breakpoints, is that I would need to recompile a live system, which is not posible. BUT the problem with Eitam's solution is that when I myself amm looking at the breakpoint and stepping through the code, I'll be getting spammed by my own program, which is not fun.

I'll need to think harder on this problem.
0
 
eitamaCommented:
@magicdlf :
Well, it's a known "Health-Check" mechanism, I just ported it to our friend's need :)

Anyway, I can write a script that monitors those files in TCL in about 5 minutes,
and the Thread will take 5 more :)

Tell me if you need me to do it, or you have already found a solution to your issue. newbieweb.
0
 
magicdlfCommented:
That should be a problem. Once you find the program stopped, you send email only once until you get that file updated again.
0
 
eitamaCommented:
I Can provide you with a toggle button to start and stop the monitoring while you work,
And even more, the script can check the last time the computer was "touched" using the win32 api,
and it will not send e-mails unless you didn't touch the computer for say 2 minutes or something.
0
 
eitamaCommented:
By the way the monitoring script can just check the "Last modified" attribute of the file,
so you will not be cluttering the file system.
0
 
AshokCommented:
I like magicdlf's idea.
I would change one thing.

Write breakpoint() function as he suggested.

Then in code wherever you place breakpoint, insert call to breakpoint function one line above the actual breakpoint.
This way e-mail will be sent and after returning from function, next line will break.

HTH
Ashok
0
 
newbiewebAuthor Commented:
If you want to write it, I'd love that.

I speak C#...

Cheers,
newbieweb
0
 
AshokCommented:
void BreakPoint()
{
  // create mail message object
  MailMessage mail = new MailMessage();
  mail.From = "";           // put the from address here
  mail.To = "";             // put to address here
  mail.Subject = "";        // put subject here      
  mail.Body = "";           // put body of email here
  SmtpMail.SmtpServer = ""; // put smtp server you will use here
  // and then send the mail
  SmtpMail.Send(mail);
}

To call above, just use
BreakPoint();

HTH
Ashok
0
 
AshokCommented:
using System.Net.Mail;  // This is recommended.
//using System.Web.Mail;  // Old

namespace CSharpTest
{

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      try
      {
        MailMessage MyMail = new MailMessage();
        MyMail.To.Add("ashok122@yahoo.com");

        MyMail.Subject = "This is the Subject line";
        MyMail.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
        MyMail.Body = "This is the message body";
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
        smtp.Send(MyMail);
      }
      catch (Exception)
      {}
    }
  }

Also, add "System.Net" in References.

HTH
Ashok
0
 
eitamaCommented:
"
newbieweb:
If you want to write it, I'd love that.

I speak C#...

Cheers,
newbieweb
"

Whom were you directing that to? :)
0
 
newbiewebAuthor Commented:
Which part? "I speak C#"?
0
 
eitamaCommented:
hehe no,

I meant which solution did you find you would like help with? the c# breakpoint() method?
or the external script monitoring?

0
 
newbiewebAuthor Commented:
The breakpoint method wouild require me to know beforehand which function I'd like to set a  breakpoint on. Right?

That would never work for me. I have not had the chance to examine the code posted, but will do that shortly.

I think an external process it brilliant. I need that anyway, since it might mean my program is hung. So to get a text message when a breakpoint is hit or the program hang is perfect.

0
 
eitamaCommented:
You will still need to add some code to your program to dispatch a new Thread, which will be dedicated to writing a file every 10 seconds.

Ill write something and post it here soon.
0
 
AshokCommented:
The breakpoint method wouild require me to know beforehand which function I'd like to set a  breakpoint on. Right?

Regarding you question, you know where in the code you want to put a break point, right?
Because without enabling brakpoint in your code, it will not break.
When are you enabling your breakpoints?   Before running your application or after?

If you intend to enable breakpoint befor running your application, BreakPoint() method is best.

For example, you want to place breakponts on line 675 and 444.
You would make call to BreakPoint() by inserting a call to BreakPoint at line # 674 and 443.

HTH
Ashok
0
 
eitamaCommented:
newbieweb:

A few question on my side :

1. Are you going to use Gmail to send those e-mails? or some other smtp server?
2. Is the smtp server going to require a secure connection?

Eitam.
0
 
DeliveryCommented:
breakpoint() would not work since I'd need to know before I start up for the night where I'd want to break. But I appreciate the code.

I realized, I already have an external Windows Utilities program I wrote running on the PC. So I could easily intiate a chatter between them. And if the make program does not respond for more that 15 seconds, I could write a text mesage.

So I'm all set.

Thanks,
Bob

0
 
newbiewebAuthor Commented:
Sorry,

I answered from a friends Experts Exchange by mistake: Delivery.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now