==================== Program.cs 文件的内容 ==================== /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
internal static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null 表示程序没运行
Application.Run(new frmMain());打开指定主窗口
}
else
{
//程序已经运行
}
} /// <summary>
/// 功能:检查程序是否正在运行
/// </summary>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
} ==================== frmMain文件的内容 ====================
private void frmMain_Load(object sender, EventArgs e)
{
//执行显示窗口的函数
ShowWindows();
}//显示窗口
frmWinData sw;
public void ShowWindows()
{
if (sw== null || sw.IsDisposed) sw= new frmWinData ();
sw.Show(dockPanelMain);
}
==================== 我的问题如下 ====================
在Program.cs文件里,如果程序没有启动,则会打开frmMain窗口,并执行ShowWindows函数.
如果程序已经启动,再次运行程序我要如何才能运行到ShowWindows函数呢?Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null 表示程序没运行
Application.Run(new frmMain());打开指定主窗口
}
else
{
//程序已经运行
}
/// 应用程序的主入口点。
/// </summary>
[STAThread]
internal static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null 表示程序没运行
Application.Run(new frmMain());打开指定主窗口
}
else
{
//程序已经运行
}
} /// <summary>
/// 功能:检查程序是否正在运行
/// </summary>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
} ==================== frmMain文件的内容 ====================
private void frmMain_Load(object sender, EventArgs e)
{
//执行显示窗口的函数
ShowWindows();
}//显示窗口
frmWinData sw;
public void ShowWindows()
{
if (sw== null || sw.IsDisposed) sw= new frmWinData ();
sw.Show(dockPanelMain);
}
==================== 我的问题如下 ====================
在Program.cs文件里,如果程序没有启动,则会打开frmMain窗口,并执行ShowWindows函数.
如果程序已经启动,再次运行程序我要如何才能运行到ShowWindows函数呢?Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null 表示程序没运行
Application.Run(new frmMain());打开指定主窗口
}
else
{
//程序已经运行
}
解决方案 »
- Winform发生内存错误 请高手指教
- c# 匹配正则
- 救命!新手求教各大侠,关于CollectionBase集合中的List属性的问题?
- Com object with CLSID {XXXX} is either not valid or not registered 如何解決?
- 大家帮帮忙,怎么样根据时间锁定一个层Div
- 请问:如何将文本框内的文本转换为HTML格式字符串,我是要用于页面上的格式化输出的,最好提供代码
- 请教:在导出到流文件时,如何换行
- 关于在ASP.NET中使用n-tie结构的问题,请高手进来:
- .net的 cdkey 高分求得!急急急。。
- 请问foreach这样的语句怎么转换成for或while语句?
- 不明白,xml 配置、webservice 都是咋回事。C/S的也用webservice? 局域网中的? xml配置文件为什么必须有它?
- 接口
internal static void Main()
{
......
frmMain frm = new frmMain();
Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null表示程序没运行
Application.Run(frm);//打开指定主窗口
}
else
{
//程序已经运行
frm.ShowWindows();
}
}
这样会打开两个frmMain窗体.我上面的问题可能描述不清楚..我再描述一次!==================== Program.cs 文件的内容 ==================== /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
internal static void Main()
{
Application.EnableVisualStyles();
Application.DoEvents();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; Process mainpro = RunningInstance();
if (mainpro == null)
{
//程序未运行,打开登录窗口,如果登录成功则打开主窗口
frmLogin Loginfrom = new frmLogin();//登录窗口
if (Loginfrom.ShowDialog() == DialogResult.OK)
{
Application.Run(new frmMain());//登录成功后打开主窗口
}
}
else
{
//当程序已经运行 我想在这里执行 frmMain 窗口里的ShowWindows函数
}
} /// <summary>
/// 功能:检查程序是否正在运行
/// </summary>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
==================== frmMain文件的内容 ==================== private void frmMain_Load(object sender, EventArgs e)
{
//执行显示窗口函数
ShowWindows();
} //显示窗口
frmWinData sw;
public void ShowWindows()
{
if (sw== null ¦ ¦ sw.IsDisposed) sw= new frmWinData ();
sw.Show(dockPanelMain);
}
==================== 我的问题如下 ====================
在Program.cs文件里,如果程序没有启动,则会打开frmMain窗口,并执行ShowWindows函数.
如果程序已经启动,再次运行程序我要如何才能运行到ShowWindows函数呢?
Process mainpro = RunningInstance();
if (mainpro == null)
{
//程序未运行,打开登录窗口,如果登录成功则打开主窗口
frmLogin Loginfrom = new frmLogin();//登录窗口
if (Loginfrom.ShowDialog() == DialogResult.OK)
{
Application.Run(new frmMain());//登录成功后打开主窗口
}
}
else
{
//当程序已经运行 我想在这里执行 frmMain 窗口里的ShowWindows函数
}
if (mainpro == null)
{
//程序未运行,打开登录窗口,如果登录成功则打开主窗口
frmLogin Loginfrom = new frmLogin();//登录窗口
if (Loginfrom.ShowDialog() == DialogResult.OK)
{
Application.Run(new frmMain());//登录成功后打开主窗口
}
}
else
{
//当程序已经运行 我想在这里执行 frmMain 窗口里的ShowWindows函数
frmMain frm = new frmMain();
frm.ShowWindows();
}
class frmMain()
{
private static frmMain frm; private frmMain()
{
//原有的初始化代码
} public static frmMain getInstance()
{
if (frm == null)
{
frm = new frmMain();
}
return frm;
}
}internal static void Main()
{
......
Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null表示程序没运行
Application.Run(frmMain.getInstance());//打开指定主窗口
}
else
{
//程序已经运行
frmMain.getInstance().ShowWindows();
}
}
internal static void Main()
{
......
Process mainpro = RunningInstance();
if (mainpro == null)
{
//mainpro的值为null表示程序没运行
//Application.Run(frmMain.getInstance());//打开指定主窗口
frmMain.getInstance().ShowWindows(); //如果这样执行,ShowWindows函数是被运行的
}
else
{
//程序已经运行
frmMain.getInstance().ShowWindows();
}
}
Application.Run(frmMain.getInstance());//打开指定主窗口 不执行ShowWindows函数
还是
//程序已经运行
frmMain.getInstance().ShowWindows(); 不执行ShowWindows函数?