繁体   English   中英

是否可以重构此EventWaitHandle以不使用Thread.Sleep()来控制竞争条件?

[英]Is it possible to refactor this EventWaitHandle to not use Thread.Sleep() to control the race condition?

我想从服务器向所有客户端发送消息。 有0- *个客户端。 加载客户端时,服务器可能正在运行或可能未运行。 这里的功能按我的需要工作。 我试图弄清楚是否可以在没有Thread.Sleep()情况下完成此工作? 还要注意,客户端和服务器分别处于独立的进程中。

服务器部分

class NamedEventsServer
{
    internal static void Main()
    {
        const string ewhName = "StickyNoteEwh";

        EventWaitHandle ewh = null;
        bool doesNotExist = false;
        bool wasCreated;

        // Attempt to open the named event.
        try
        {
            ewh = EventWaitHandle.OpenExisting(ewhName);
        }
        catch (WaitHandleCannotBeOpenedException)
        {
            Console.WriteLine("Named event does not exist.");
            doesNotExist = true;
        }
        if (doesNotExist)
        {
            // The event does not exist, so create it.

            ewh = new EventWaitHandle(true,
                EventResetMode.ManualReset,
                ewhName,
                out wasCreated);

            if (wasCreated)
            {
                Console.WriteLine("Created the named event.");
            }
            else
            {
                Console.WriteLine("Unable to create the event.");
                return;
            }
        }
        ewh.Set();
        Thread.Sleep(1000);//wait one second...giving threads enough time to all respond.  Then stop triggering the event.
        ewh.Reset();

        //exit
    }
}

客户部分

class NamedEventsClient
{
    internal static void Main()
    {
        const string ewhName = "StickyNoteEwh";

        while (true)
        {
            EventWaitHandle ewh = null;
            bool doesNotExist = false;
            bool wasCreated;
            // Attempt to open the named event.
            try
            {
                ewh = EventWaitHandle.OpenExisting(ewhName);
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                Console.WriteLine("Named event does not exist.");
                doesNotExist = true;
            }
            if (doesNotExist)
            {
                // The event does not exist, so create it.

                ewh = new EventWaitHandle(false,
                    EventResetMode.ManualReset,
                    ewhName,
                    out wasCreated);

                if (wasCreated)
                {
                    Console.WriteLine("Created the named event.");
                }
                else
                {
                    Console.WriteLine("Unable to create the event.");
                    return;
                }
            }
            Console.WriteLine("Wait on the event.");
            ewh.WaitOne();
            Console.WriteLine("Event was signaled.");
            //Console.WriteLine("Press the Enter key exit.");
            Thread.Sleep(1000);
            //Console.ReadLine();
        }
    }
}

我想这取决于您是否确定所有客户都将在一秒钟内获得他们的时间。 这听起来很合理,但是在极大的压力下,有些客户可能会错过它。 这有多重要?

无论如何,我认为这正是您应该使用ZeroMQ进行的事情。 它重量轻,可以为您解决所有潜在的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM