【C#】C#+Winform 實現切換用戶功能

方法一和方法二均爲退出主進程後,重新開啓新進程。方法三爲不退出主進程,將窗體隱藏,切換用戶時,重新新建一個窗體。被隱藏的窗體與新建的窗體同在一個線程下。

方法一:

 

#region 登錄部分
private void RtnLoginOK_Click(object sender, EventArgs e)	//登錄按鈕單擊事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗體
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切換用戶
private void RtnMainSwitchUser_Click(object sender, EventArgs e)	//切換用戶按鈕單擊事件
{
	if (DialogResult.Yes == MessageBox.Show("您確定要退出登陸嗎?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, "+自己程序設置的啓動參數,沒有則省略");
		Process.GetCurrentProcess().Kill();
	}
}
#endregion	//登錄按鈕單擊事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗體
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切換用戶
private void RtnMainSwitchUser_Click(object sender, EventArgs e)	//切換用戶按鈕單擊事件
{
	if (DialogResult.Yes == MessageBox.Show("您確定要退出登陸嗎?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, "+自己程序設置的啓動參數,沒有則省略");
		Process.GetCurrentProcess().Kill();
	}
}
#endregion

 

 

方法二:

 

#region 登錄部分
private void RtnLoginOK_Click(object sender, EventArgs e)	//登錄按鈕單擊事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗體
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切換用戶
private void RtnMainSwitchUser_Click(object sender, EventArgs e)
{
	if (DialogResult.Yes == MessageBox.Show("您確定要退出登陸嗎?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		StartExe(Application.ExecutablePath);
		Thread.Sleep(1000);
		Application.ExitThread();
	}
}

private static void StartExe(string appName)
{
	string path = appName;
	Process ps = new Process();
	ps.StartInfo.FileName = path;
	ps.StartInfo.Arguments = "-routine";
	ps.StartInfo.CreateNoWindow = true;
	ps.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
	ps.Start();
}
#endregion	//登錄按鈕單擊事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗體
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切換用戶
private void RtnMainSwitchUser_Click(object sender, EventArgs e)
{
	if (DialogResult.Yes == MessageBox.Show("您確定要退出登陸嗎?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		StartExe(Application.ExecutablePath);
		Thread.Sleep(1000);
		Application.ExitThread();
	}
}

private static void StartExe(string appName)
{
	string path = appName;
	Process ps = new Process();
	ps.StartInfo.FileName = path;
	ps.StartInfo.Arguments = "-routine";
	ps.StartInfo.CreateNoWindow = true;
	ps.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
	ps.Start();
}
#endregion


方法三:

 

 

 

 

#region 登錄部分
private void RtnLoginOK_Click(object sender, EventArgs e) //登錄按鈕單擊事件 { this.Hide(); FrmRoutineMain formRtnLoginOK = new FrmRoutineMain(); //新建主窗體 formRtnLoginOK.ShowDialog(); }

#endregion#region 切換用戶private void RtnMainSwitchUser_Click(object sender, EventArgs e){if (DialogResult.Yes == MessageBox.Show("您確定要退出登陸嗎?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)){this.Hide();Form NewForm = new FrmRoutineLogin(); //新建登錄窗體NewForm.ShowDialog(); }}#endregion


 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章