下面的代码是为共享内存开发的。当它用于两个服务或两个用户应用程序时,它工作得很好。但是在服务中创建内存时,应用程序找不到内存。这段代码有什么问题?
在役:
mmf=MemoryMappedFile.CreateNew("ALFMap",10000);
bool mutexCreated;
Mutex mutex=new Mutex(true,"ALFMutex",out mutexCreated);
stream=mmf.CreateViewStream(0,1000);
BinaryWriter writer=new BinaryWriter(stream);
writer.Write("I am reza dadkhah");
mutex.ReleaseMutex();
在用户应用程序中:
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("ALFMap",MemoryMappedFileRights.FullControl))
{
Mutex mutex=Mutex.OpenExisting("ALFMutex");
mutex.WaitOne();
using (MemoryMappedViewStream stream=mmf.CreateViewStream(0,1000))
{
BinaryReader reader = new BinaryReader(stream);
textBox1.Text=reader.ReadString();
}
mutex.ReleaseMutex();
}