Question

I have a auto log off functionality on my application. Which activates based on settings. When user make a re-login with different username then I have to dispose the existing form.

Here comes the main problem. When a user open the application from file browser of mobile and re-login with different username then the application goes to background.File explorer comes on front.

To overcome this problem i have used SetForegroundWindow of coredll.dll But it isn't working.

I made a track of current process.And put it to a Global place.

[DllImport("coredll.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

if (_autoLogOff)
{
    if (cboUsers.Text != Globals.UserName)
    {
        List<frmAutoLogOff> openedForms = Globals.OpenedForms;
        for (int i = openedForms.Count - 1; i >= 0; i--)
        {
            if (openedForms[i] != null && openedForms[i].Name != "frmMenu1")
            {
                //openedForms[i].SuspendLayout();
                openedForms[i].Dispose();
            }
        }
    }

    SetForegroundWindow(currentProcess);
    Globals.UserName = cboUsers.Text;
    CDataAccess.ShowMessageForUser();       
}

Any suggestion or solution will cordially accepted.

Was it helpful?

Solution

Use FindWindow to SetForgroundWindow

// For Windows Mobile, replace user32.dll with coredll.dll
        [DllImport("coredll.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Set text of ur frmMainMenu1.cs suppose "MainMenu" and call them at proper place

public int FindWindow(string windowName, bool wait)
        {
            int hWnd = FindWindow(null, windowName).ToInt32();
            while (wait && hWnd == 0)
            {
                System.Threading.Thread.Sleep(500);
                hWnd = FindWindow(null, windowName).ToInt32();
            }

            return hWnd;
        }

And call this

int hWnd = FindWindow("MainMenu", wait);
            if (hWnd != 0)
            {
                return SetForegroundWindow((IntPtr)hWnd);
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top