对话框不显示原始形式

时间:2015-01-29 15:14:51

标签: c# windows mobile dialog windows-mobile-6.5

我正在开发一款Windows Mobile 6.5应用程序。当用户打开App时,会出现一个包含登录表单的对话框。当计时器没有活动时,用户登录然后30秒(生产中的小时间)我使用事件再次显示登录对话框:

static private void _TimerTick(object state)
    {
        // the user has been inactive for 30 secs; log him out
        MainForm.timer = null;
        using (LoginForm LoginForm = new LoginForm())
        {

            if (LoginForm.ShowDialog() == DialogResult.OK)
            {
               MainForm.timer = new System.Threading.Timer(_TimerTick, null, 1000 * 30 * 1, Timeout.Infinite);
            }
            else
            {
                Application.Exit();
            }
        } 


    }

但是,一旦我按下登录并从登录表单返回确认按钮,原始表单就会显示。虽然它仍然在任务管理器中。我试过了: .TopMost = true;但是我无法评估应用程序底部栏中的窗口按钮,并且没有其他应用程序可以运行,因为我的应用程序中的表单总是在它前面。

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案,因为这只适用于登录和一个主要形式:

LoginForm.cs(带有两个文本框和带有登录和退出的主菜单):

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LoginFormTest
{

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

        public void doShow(bool bShow)
        {
            if(bShow)
                Invoke(new Action(() => this.Show()));
            else
                Invoke(new Action(() => this.Hide()));
        }

        public void doClose()
        {
            Invoke(new Action(() => this.Close()));
        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            MainForm mainForm = new MainForm();
            mainForm.Show();
            System.Diagnostics.Debug.WriteLine("mainForm started");
        }

    }
}

没什么特别的。 如果没有活动,MainForm会有关闭表单的代码:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace LoginFormTest
{
    public partial class MainForm : Form
    {
        int countDown = 3;  //number of seconds for timeout

        System.Threading.Timer timer;
        object lockCounter = new object(); //to sync access to counter var

        public MainForm()
        {
            InitializeComponent();
            //start a independent timer after 1000ms and with a 1000ms interval
            timer = new System.Threading.Timer(new TimerCallback(this.timerCallback), null, 1000, 1000);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            doClose();
        }

        private void mnuLogout_Click(object sender, EventArgs e)
        {
            doClose();
        }

        private void doClose()
        {
            System.Diagnostics.Debug.WriteLine("mainForm closing");
            try
            {
                timer.Dispose(); //else timer thread will continue running!
                Invoke(new Action(() => this.Close()));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in doClose(): " + ex.Message);
            }
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            resetTimeout();
        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            resetTimeout();
        }

        private void MainForm_Click(object sender, EventArgs e)
        {
            resetTimeout();
        }

        public void resetTimeout()
        {
            System.Diagnostics.Debug.WriteLine("resetTimeout()");
            lock(lockCounter)
                countDown = 3;
        }

        public void timerCallback(object stateInfo)
        {
            lock (lockCounter)
                countDown--;
            if (countDown == 0)
            {
                System.Diagnostics.Debug.WriteLine("timeout->doClose()");
                doClose();
            }
        }

        private void MainForm_Closed(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("mainForm CLOSED");
        }

    }
}

特价:

  • 用于同步对计数器var
  • 的访问的锁定对象
  • 独立于消息泵运行的线程计时器
  • 来自TimerCallback函数的委托