http://topic.csdn.net/u/20090827/17/3b45a850-ff12-4f65-b83c-002d5ef09d17.html这个是原帖,本来问题解决了,但是后来加了一个托盘,问题就来了,双击我自己的自定义文件或者系统快捷方式,就找不到这个进程了........添加托盘也没加什么代码,就是把主窗体Visible设置为false只要主窗体显示在桌面上,功能还是可以实现的,就是一隐藏到托盘里去了,就不行了我总不能让别人先打开主窗体,在双击文件吧...........这样托盘就没啥意义了自己搞了半天,还是没搞出来啊,因为代码是你的,所以向你提问了,麻烦你帮下忙了..........先谢谢了(哎,不知道你在不在线哟)其他的各路高手也可以看下呀,帮下忙

解决方案 »

  1.   

    Form.Visible = false;
     System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle 获取不到窗体。你可以建立一个大小为0的窗体 为制是-的窗体
      

  2.   

    另外的方法你可以写到注册表里..另外一段监视注册表. 或则麻烦点使用SOCKET
      

  3.   

    你可以建立一个大小为0的窗体,哎,搞了半天这个方法也否定了..........
    Form.ShowInTaskbar = false;  这样也找不到了.....
      

  4.   

    C#里 如果窗体 是visible=false 则 handle 获取不到吧
    唉 我也遇到过这问题
    后来也是用是socket侦听的
      

  5.   

    就没其他的方法了,socket没搞过...
      

  6.   

    保证只有一个进程,另外一个方法,使用Mutex类
    需要命名的,可以做到
    下面是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();
        }
    }
      

  7.   

    使用注册表吧!运行了就往注册表写TRUE,退出的时候写FALSE
      

  8.   

            static void Main()
            {
                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());
                }
            }
      

  9.   


    当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex 是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥体。是进程不是线程.....Mutex类 是跟线程一起用的吧,我这里是要找到已经运行的进程...并得到一个主窗体实例现在是只要最小化到托盘里了就找不到这个进程了......不最小化到托盘是可以的,功能都实现了窗体的Visible 不能设置为false;
    窗体的ShowInTaskbar  不能设置为false;  //这个是true的话,托盘也没多大意义了
      

  10.   

    给高手们搞篇文章看下,我的水平看有点难度http://blog.csdn.net/DavidHsing/archive/2009/04/30/4138378.aspx
      

  11.   


    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("我还活着!");
                        }
                    }
      

  12.   

    Mutex:
    匿名的Mutex不能跨进程
    命名的Mutex是操作系统内核级别,可以跨进程的
    看楼上的代码:
          Mutex m = new Mutex(requestInitialOwnership, 
                                "MyMutex", 
                                out mutexWasCreated);
    是命名的mutex,可以保证整个操作系统只有一个你的程序进程
      

  13.   

    光得到这个进程还不行呀,怎么跟这个进程通信,把我要传的参数,传进去我的项目是多文档的,就是传一个文件路径,在查找到的进程里打开一个Tab页
      

  14.   

    你关联的时候写注册表,那你把handle也写进注册表好了,
    要发的时候取出来sendmessage
      

  15.   

    哦,谢谢,但是我没写关联文件的代码,因为不需要写软件和文件关联代码的,只需要做安装程序时候设置文件后缀名和想关联的程序即可但是怎么把handle写到注册表呢,就是把IntPtr这个结构存入注册表,读出来的时候又把存进去的数据转换成IntPtr吗????????
      

  16.   

    IntPtr有个方法ToInt32() 可以转化成整数IntPtr P=this.Handle;
    int HANDLE=P.ToInt32();要还原的时候就IntPtr P=new IntPtr(HANDLE);
    其实不还原也可以,整数直接给 API函数 SendMessage 的第一个句柄参数就是了
      

  17.   

    Form.Visible = false; 
    System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle 获取不到窗体。 你可以建立一个大小为0的窗体 为制是-的窗体
      

  18.   

    搞的我郁闷调试 也没法调试  VS运行的进程是DuxSystem.vshost.exe 文件关联的是DuxSystem.exe系统和文件关联不起来,没法调试,在文件打开方式上找到DuxSystem.vshost.exe和文件关联也没用哎,晕了