0

This is a follow up question to one of the answers provided for post Quit() causes Access to become visible for a moment.

The answer recommends that I move the window off screen so that the flickering window can't be seen.

If I understand the accepted answer for the question C# Process.MainWindowHandle always returns IntPtr Zero

An application that has changed its visibility (accApp.Visible = false;) does not have/return/initialize/... an IntPtr and it will always be set to zero.

How can I move this window/app off screen so that users will never see it. I don't want to make it visible and then have my program pop/move it off screen after. Unless there is a way to prevent the visibility to extend to the users eyes (Don't want to see the window ever).

In theory could I somehow pause screen rendering, make it visible, move it and then resume screen rendering? (doubtful this is a good solution)

Since my previous post I have moved the code creating the application into its separate class to allow for only one instance of the application to run instead of the previous way which was generating a new application for each request.

public class ConnectionManager
{
    [DllImport("user32.dll")]
    static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);

    //Old Code for tying to resolve flashing access window. 
    //[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    //public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);


    public ConnectionManager()
    {
        Console.WriteLine("Connection Manager Started");
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);

    }

    void OnProcessExit(object sender, EventArgs e)
    {
         BreakAllConnections();
    }

    private Microsoft.Office.Interop.Excel.Application _excelApp;
    public Microsoft.Office.Interop.Excel.Application excelApp
    {
        get
        {
            if (_excelApp == null)
            {
                try
                {
                    _excelApp = new Microsoft.Office.Interop.Excel.Application();
                }
                catch
                {
                    //TODO Add Termination
                    MessageBox.Show("Abort Error: Could not open Exel Application");
                }
            }
            return _excelApp;
        }

    }

    private Microsoft.Office.Interop.Access.Application _accessApp;
    public Microsoft.Office.Interop.Access.Application accessApp
    {
        get
        {
            if (_accessApp == null)
            {
                try
                {
                    _accessApp = new Microsoft.Office.Interop.Access.Application();
                    //Old Code for tying to resolve flashing access window. 
                    //GetWindowThreadProcessId(_accessApp.hWndAccessApp(), out int id);

                    //IntPtr handle = Process.GetProcessById(id).MainWindowHandle;
                    //if (handle != IntPtr.Zero)
                    //{
                    //    const short SWP_NOSIZE = 1;
                    //    const short SWP_NOZORDER = 0X4;
                    //    SetWindowPos(handle, 0, 10000, 10000, 0, 0, SWP_NOZORDER | SWP_NOSIZE | 0);
                    //}
                }
                catch
                {
                    //TODO Add Termination
                    MessageBox.Show("Abort Error: Could not open Exel Application");
                }
            }
            return _accessApp;
        }

    }

    public void BreakAllConnections()
    {

        try
        {
            if (_excelApp != null) { _excelApp.Quit(); Marshal.ReleaseComObject(_excelApp); }
        }
        catch
        {
            GetWindowThreadProcessId(_excelApp.Hwnd, out int id);
            Process.GetProcessById(id).Kill();
        }

        try
        {
            if (_accessApp != null) { _accessApp.Quit(); Marshal.ReleaseComObject(_accessApp); }
        }
        catch
        {
            GetWindowThreadProcessId(_accessApp.hWndAccessApp(), out int id);
            Process.GetProcessById(id).Kill();
        }
    }
}
Mandelbrotter
  • 1,181
  • 7
  • 17
  • Is [this](https://stackoverflow.com/questions/27930307/why-does-microsoft-office-interop-excel-application-quit-leave-the-background) similar problem you were having? – André Sanson Oct 01 '19 at 16:39
  • @Hans Tried the .hWnd() but it says that the Access application does not have a definition for it. And .hWndAccessApp() returns zeros. – Mandelbrotter Oct 01 '19 at 16:59
  • @Sanson From what I can tell the application are terminating correctly. Its just that they flash onto the screen when they execute the quit command. – Mandelbrotter Oct 01 '19 at 17:00

0 Answers0