关于 SingleTon 了解一点!
但是我想实现跨程序的SingleTon,请问如何实现!  有个程序 A.exe 调用 Common.dll, Common.dll 就一个简单的类,代码如下:
  class C
  {
    public static readonly C Instance = new C();
    private count;
    private C ()
    {
      count = 0;
    }   public GetCount()
   {
     count ++;
     return count;
   }
  }  然后 A.exe 的代码很简单,就是调用 GetCount 方法.
  如: Common.C.Intance.GetCount();  再写一个B.exe,也是调用GetCount 方法. 把 A.exe, Common.dll, B.exe 放再同一个文件夹里面.  运行A.exe 得到结果为1 
  再运行B.exe得到的还是1 而不是2
  请问如何在两个应用程序之间实现一个SingleTon的组件,谢谢!

解决方案 »

  1.   

    Common.dll被映射到A和B的各自的地址空间中,A中的C和B中的C是各不相干的
      

  2.   

    要实现实现多个程序使用同一个对象,用.net remoting。
      

  3.   

    共享数据的话可以用WIN32中的内存映射文件
      

  4.   

    我知道 .Net Remoting 可以实现服务器组件的SingleTon
    但是我的只是在同一台计算机上啊,用Remoting开销太大吧?
    请问有谁知道什么好的方法吗?
      

  5.   

    你把Common.dll搞成exe,然后跨进程调用这个函数,要用到COM
      

  6.   

    liangge() 
    avisnet(第十维度) 
    请问你们有实现的例子吗?发个给我!谢谢!
    [email protected]
      

  7.   

    内存映射,我有个例子,不过是用C写的#include <windows.h>
    #include <stdio.h>
    #include <conio.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");
    TCHAR szMsg[]=TEXT("Message from first process");
    int   szInt = 999;void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security 
                     PAGE_READWRITE,          // read/write access
                     0,                       // max. object size 
                     BUF_SIZE,                // buffer size  
                     szName);                 // name of mapping object
     
       if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE) 
       { 
          printf("Could not create file mapping object (%d).\n", GetLastError());
          return;
       }
       pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to mapping object
                            FILE_MAP_ALL_ACCESS, // read/write permission
                            0,                   
                            0,                   
                            BUF_SIZE);           
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", GetLastError()); 
          return;
       }   
       CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
       getch();   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    } //建立第二个工程#include <windows.h>
    #include <stdio.h>
    #include <conio.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object 
     
       if (hMapFile == NULL) 
       { 
          printf("Could not open file mapping object (%d).\n", GetLastError());
          return;
       } 
     
       pBuf = (LPTSTR) MapViewOfFile(hMapFile,    // handle to mapping object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,                    
                   0,                    
                   BUF_SIZE);                   
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", GetLastError()); 
          return;
       }
       
       MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    }
      

  8.   

    你可以把数据的操作放到一个程序中,另一个程序只是和它进行交互,
    参看
    http://dev.csdn.net/article/15/15598.shtm
      

  9.   

    感谢 Knight94(愚翁) 的方法!
    但是 protected virtual void DefWndProc (ref Message m) 方法是在 System.Windows.Forms
    中的.
    现在我一个是 Windows服务,一个是WinForm,如何传递消息啊?
      

  10.   

    没有其他的办法了,我还是使用了 Remoting 中的 IpcChannel 实现的!