怎么才能不让两个系统同时运行啊?

yanglg 2006-07-04 10:03:20
登陆系统后,在不退出系统的情况下,还可以多次登陆系统。这样就造成了一台电脑,运行了多个一样的软件。怎么样才能控制系统在没有退出的情况下,不允许第二次登陆系统呢?

谢谢
...全文
161 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liaodanz 2006-07-04
  • 打赏
  • 举报
回复
引用 using System.Runtime.InteropServices;


#region 调用API

[StructLayout( LayoutKind.Sequential)]//只允许启动一个API函数
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetLastError();
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);

[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;
[STAThread]
#endregion
static void Main()
{
IntPtr hMutex;
hMutex=CreateMutex(null,false,"mytext");//自己修改名字
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{

Application.Run(new Form1());//修改为你的启动窗体名
}
else
{
MessageBox.Show("本程序只允许同时运行一个!","确定",MessageBoxButtons.OK,MessageBoxIcon.Stop);
ReleaseMutex(hMutex);
}
cscer 2006-07-04
  • 打赏
  • 举报
回复
设置检查

判断软件已经运行,则终止它

这样保证程序只有一个实例在运行

就只能登录一次了
yanglg 2006-07-04
  • 打赏
  • 举报
回复
谢谢
xvting 2006-07-04
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace MutexDemo1
{

public class Form1 : System.Windows.Forms.Form
{

private System.ComponentModel.Container components = null;

public Form1()
{

InitializeComponent();

}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}

static void Main()
{
string pID = "MyApp";
Mutex m = new Mutex(false, pID);

if(m.WaitOne(0, false) == true)
{

Application.Run(new Form1());
}
else
{
MessageBox.Show("已有相同线程在运行中",
"",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
}
}
}
Knight94 2006-07-04
  • 打赏
  • 举报
回复
c#本身就支持mutex,参看

http://blog.csdn.net/knight94/archive/2006/03/16/625809.aspx

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧