gpt4 book ai didi

c# - 尝试在 C# 中读取共享内存时发生堆栈溢出错误

转载 作者:行者123 更新时间:2023-11-30 16:02:25 26 4
gpt4 key购买 nike

<分区>

以下代码产生堆栈溢出错误。它创建一个共享内存空间,然后尝试将共享内存内容复制到本地缓冲区。我已经用非托管 C++ 编写了几个程序来执行此操作,但 C# 对我来说很陌生……我在堆上分配了一个缓冲区,并试图将共享内存缓冲区复制到我的本地缓冲区中。这是堆栈溢出错误触发的地方:accessor.Read<my_struct>(0, out ps.hi); .也许 accessor.Read函数尝试在将共享内存缓冲区复制到我提供的引用之前创建共享内存缓冲区的本地副本?如果是这样,在 C# 中将大内存块传入和传出共享内存的推荐方法是什么?我没有在我的互联网搜索中发现这个问题,所以任何帮助将不胜感激......

确切的错误消息是:“在 mscorlib.dll 中发生类型为‘System.StackOverflowException’的未处理异常”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;
namespace ConsoleApplication2
{
unsafe struct my_struct
{
public fixed UInt16 img[1280 * 960];
}
class Program
{
my_struct hi;
static void Main(string[] args)
{
Program ps = new Program();
ps.hi = new my_struct();

using (var mmf = MemoryMappedFile.CreateOrOpen("OL_SharedMemSpace", System.Runtime.InteropServices.Marshal.SizeOf(ps.hi)))
{
using (var accessor = mmf.CreateViewAccessor())
{
//Listen to event...
EventWaitHandle request_event;
EventWaitHandle ready_event;
try
{
request_event = EventWaitHandle.OpenExisting("OL_ReceiveEvent");
}
catch (WaitHandleCannotBeOpenedException)
{
Console.WriteLine("Receive event does not exist...creating one.");
request_event = new EventWaitHandle(false, EventResetMode.AutoReset, "OL_ReceiveEvent");
}
try
{
ready_event = EventWaitHandle.OpenExisting("OL_ReadyEvent");
}
catch (WaitHandleCannotBeOpenedException)
{
Console.WriteLine("Ready event does not exist...creating one.");
ready_event = new EventWaitHandle(false, EventResetMode.AutoReset, "OL_ReceiveEvent");
}
System.Console.WriteLine("Ok...ready for commands...");
while (true)
{
accessor.Read<my_struct>(0, out ps.hi);
request_event.WaitOne();
}
}
}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com