C# 对话框不显示原始形式
我正在开发一个Windows Mobile 6.5应用程序。当用户打开应用程序时,会出现一个带有登录表单的对话框。用户登录,然后在30秒小时间投入生产后,当计时器在没有活动的情况下用完时,我使用事件再次显示登录对话框:C# 对话框不显示原始形式,c#,windows,mobile,dialog,windows-mobile-6.5,C#,Windows,Mobile,Dialog,Windows Mobile 6.5,我正在开发一个Windows Mobile 6.5应用程序。当用户打开应用程序时,会出现一个带有登录表单的对话框。用户登录,然后在30秒小时间投入生产后,当计时器在没有活动的情况下用完时,我使用事件再次显示登录对话框: static private void _TimerTick(object state) { // the user has been inactive for 30 secs; log him out MainForm.timer = n
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();
}
}
}
但一旦我按下login并从登录表单返回ok按钮,原始表单就会显示出来。尽管它仍然在任务管理器中。我试过:
.TopMost=true;但是,我无法评估应用程序底部栏中的windows按钮,并且没有其他应用程序可以运行,因为我的应用程序中的表单总是在它前面。一个简单的解决方案,因为这仅适用于登录和一个主表单: 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");
}
}
}
特价:
用于同步访问计数器变量的锁定对象
独立于消息泵运行的线程计时器
从TimerCallback函数调用对的委托
对不起,我不清楚描述。你能详细解释一下吗?只要还有对表单的引用,Compact Framework就不会处理表单。只要有应用程序创建的正在运行的线程,它也不会关闭应用程序。由于某种原因,当对话框关闭时,程序被隐藏。它进入手机最后一次打开的程序或主屏幕。该程序仍在任务管理器中,但直到我切换回任务后才可见。我有一个游戏,改变了我的一些代码,但现在不行!奇怪的无论如何谢谢你!事实上,我认为是形式继承的问题。对话框继承了与原始表单相同的表单类。所以这可能就是问题所在!好吧,我又遇到同样的问题了!所以也许我没有解决这个问题。我有一个名为:userActivityEvent的事件,每当有人单击按钮或做某事时,计时器就会重置,当计时器用完时,就会调用一个函数注销。该对话框工作正常,一旦收到响应,就会关闭并按预期显示原始形式。但是我的主窗体上有一个注销按钮,我调用了相同的函数,对话框显示出来,但是一旦我返回OK,原始窗体就不可见了。它仍在任务管理器中,但我必须手动切换到它以使其可见。看起来您需要管理表单堆栈,并跟踪和控制何时显示/激活哪个表单。