-1

I am developing a standalone app and need to do some action(Exactly I need to post some request on some URL) when user loggs out the system..

I've tried to achieve this with Application.ApplicationExit and AppDomain.CurrentDomain.ProcessExit events or with destructors but non of this seems to work.

I would be grateful for some hint on how to do it.

Here is some snippet from my main function:

[STAThread]
static void Main()
{
    ...

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.ApplicationExit += Application_ApplicationExit;
    AppDomain.CurrentDomain.ProcessExit += Application_ApplicationExit; 
    Application.Run();
}

static void Application_ApplicationExit(object sender, EventArgs e)
{
    File.Create("C:\\kod\\test5.txt");
}

I am trying to test this behaviour with creating file(easier to see the results).

Hope this time it will be more clear.

Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
Dawid Wysakowicz
  • 3,402
  • 17
  • 33
  • 1
    Without *any* indication what your code looks like, this would be too broad. Add more details so your question does not get closed. Is there a reason you cannot do whatever you want to do as the last lines of your `main` method? – nvoigt Jan 07 '15 at 07:32
  • "user loggs off" have many meanings, but rarely used in relation to WinForms... Would you mind clarifying? – Alexei Levenkov Jan 07 '15 at 07:35
  • If "logs off" == "application exit" than this is duplicate of http://stackoverflow.com/questions/10579446/capturing-application-exit-event-winforms – Alexei Levenkov Jan 07 '15 at 07:36
  • Hope this time it is more clear – Dawid Wysakowicz Jan 07 '15 at 07:45
  • What exactly happens? Is the file not created? Can you put a breakpoint in `Application_ApplicationExit`? – Codor Jan 07 '15 at 07:54
  • The file is not created. I quite don't see how could I put a breakpoint while logging out. – Dawid Wysakowicz Jan 07 '15 at 07:55
  • Because you can't guarantee the file operation will complete before the application exits. – aevitas Jan 07 '15 at 08:21
  • Your question is more like how to delay shutdown (check [this](http://stackoverflow.com/q/5217246/1997232)). – Sinatr Jan 07 '15 at 09:01
  • http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx – dariogriffo Jan 07 '15 at 09:22

2 Answers2

0

To make visible your Windows Form you need to pass the Form object in Application.Run() method.

Application.Run();

. Application.Run Method: Begins running a standard application message loop on the current thread, without a form.

pass the form to make it visible.

Application.Run Method (Form)
Mitz
  • 561
  • 8
  • 21
0

Ok. Using some advice from You guys I came up with a satisfactory solution.

I've created a minimized, not showing in the taskbar form(so it is invisible for the user) and overriden the WndProc when I run some code on WM_QUERYENDSESSION and WM_ENDSESSION messages.

And some code:

public class BlockingForm : Form
{
    public delegate void SessionEnd();
    public event SessionEnd SessionEndEvent;

    public BlockingForm()
    {
        WindowState = FormWindowState.Minimized;
        ShowInTaskbar = false;
    }

    protected override void WndProc(ref Message aMessage)
    {
        const int WM_QUERYENDSESSION = 0x0011;
        const int WM_ENDSESSION = 0x0016;

        if (aMessage.Msg == WM_QUERYENDSESSION || aMessage.Msg == WM_ENDSESSION)
        {
            OnSessionEnd();
        }

        base.WndProc(ref aMessage);
    }

    private void OnSessionEnd()
    {
        var handler = SessionEndEvent;
        if (handler != null) handler();
    }
}

Mostly helpful comment was the one from @Sinatr. Thx.

Dawid Wysakowicz
  • 3,402
  • 17
  • 33