我正在将 C# .NET Framework 项目转换为 .NET Core。它编译时没有错误或警告,并且在 Windows 上运行良好,但是当我在 CentOS 7 上尝试它时,当我尝试创建 EventWaitHandle 的实例时,我得到了 System.PlatformNotSupportedException。这是代码:
this.eventHandle = new EventWaitHandle(initialState, resetMode, name);
显然 .NET Core 2.1 支持此类,因为这里有它的 MS 文档:
https://learn.microsoft.com/en-us/dotnet/api/system.threading.eventwaithandle.-ctor?view=netcore-2.1
这是否像错误描述的那样简单,并且此类不支持跨多个平台?如果是这样,为什么它包含在 .NET Core 中?难道还有其他事情在起作用吗?
如果它根本不受支持,是否有人可以建议在 .NET Core 2.1 中完全支持的替代类?
谢谢。
最佳答案
.Net Core 有一些特定于平台的 api。这是其中之一。这是它无法处理的命名句柄,请参阅 source
public EventWaitHandle(bool initialState, EventResetMode mode, string name)
{
if(name != null)
{
#if PLATFORM_UNIX
throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_NamedSynchronizationPrimitives"));
#else
if (System.IO.Path.MaxPath < name.Length)
{
throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
}
#endif
}
...
一些更有趣的读物 here :
I'm uncertain what .NET technology you should be using instead. If you don't require cross-process synchronization, you don't need to specify a name for EventWaitHandle, or you can use a different subclass of EventWaitHandle, such as ManualResetEvent or AutoResetEvent.
关于c# - .NET Core 2.1 不支持 EventWaitHandle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526755/