http://topic.csdn.net/u/20090827/17/3b45a850-ff12-4f65-b83c-002d5ef09d17.html这个是原帖,本来问题解决了,但是后来加了一个托盘,问题就来了,双击我自己的自定义文件或者系统快捷方式,就找不到这个进程了........添加托盘也没加什么代码,就是把主窗体Visible设置为false只要主窗体显示在桌面上,功能还是可以实现的,就是一隐藏到托盘里去了,就不行了我总不能让别人先打开主窗体,在双击文件吧...........这样托盘就没啥意义了自己搞了半天,还是没搞出来啊,因为代码是你的,所以向你提问了,麻烦你帮下忙了..........先谢谢了(哎,不知道你在不在线哟)其他的各路高手也可以看下呀,帮下忙
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle 获取不到窗体。你可以建立一个大小为0的窗体 为制是-的窗体
Form.ShowInTaskbar = false; 这样也找不到了.....
唉 我也遇到过这问题
后来也是用是socket侦听的
需要命名的,可以做到
下面是MSDN上代码:
/ This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named
// mutex is destroyed when all the Mutex objects that represent it
// have been released.
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.using System;
using System.Threading;public class Test
{
public static void Main()
{
// Set this variable to false if you do not want to request
// initial ownership of the named mutex.
bool requestInitialOwnership = true;
bool mutexWasCreated; // Request initial ownership of the named mutex by passing
// true for the first parameter. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object. If "MyMutex" is created by this call,
// then mutexWasCreated contains true; otherwise, it contains
// false.
Mutex m = new Mutex(requestInitialOwnership,
"MyMutex",
out mutexWasCreated);
// This thread owns the mutex only if it both requested
// initial ownership and created the named mutex. Otherwise,
// it can request the named mutex by calling WaitOne.
if (!(requestInitialOwnership && mutexWasCreated))
{
Console.WriteLine("Waiting for the named mutex.");
m.WaitOne();
} // Once the process has gained control of the named mutex,
// hold onto it until the user presses ENTER.
Console.WriteLine("This process owns the named mutex. " +
"Press ENTER to release the mutex and exit.");
Console.ReadLine(); // Call ReleaseMutex to allow other threads to gain control
// of the named mutex. If you keep a reference to the local
// Mutex, you can call WaitOne to request control of the
// named mutex.
m.ReleaseMutex();
}
}
{
int iProcessNum = 0; foreach (Process singleProc in Process.GetProcesses())
{
if (singleProc.ProcessName == Process.GetCurrentProcess().ProcessName)
{
iProcessNum += 1;
}
}
if (iProcessNum == 2)
{
MessageBox.Show("【系统提示】:程序已经在运行中……", "dgsdgsg技术", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (iProcessNum > 2)
{
return;
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fmLogin());
}
}
当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。是进程不是线程.....Mutex类 是跟线程一起用的吧,我这里是要找到已经运行的进程...并得到一个主窗体实例现在是只要最小化到托盘里了就找不到这个进程了......不最小化到托盘是可以的,功能都实现了窗体的Visible 不能设置为false;
窗体的ShowInTaskbar 不能设置为false; //这个是true的话,托盘也没多大意义了
Mutex是可以跨进程的,8楼举得就是进程单例的例子
只要任务管理器里还有你的进程,你就可以这样找到
比如
System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("你的进程名字");
for (int i = 0; i < CurrentProcess.Length; i++)
{
if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
{
MessageBox.Show("我还活着!");
}
}
匿名的Mutex不能跨进程
命名的Mutex是操作系统内核级别,可以跨进程的
看楼上的代码:
Mutex m = new Mutex(requestInitialOwnership,
"MyMutex",
out mutexWasCreated);
是命名的mutex,可以保证整个操作系统只有一个你的程序进程
要发的时候取出来sendmessage
int HANDLE=P.ToInt32();要还原的时候就IntPtr P=new IntPtr(HANDLE);
其实不还原也可以,整数直接给 API函数 SendMessage 的第一个句柄参数就是了
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle 获取不到窗体。 你可以建立一个大小为0的窗体 为制是-的窗体