{"id":2056,"date":"2023-04-01T21:21:18","date_gmt":"2023-04-01T13:21:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2056"},"modified":"2023-04-07T06:05:22","modified_gmt":"2023-04-06T22:05:22","slug":"csharp-multithreaded-communication","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/csharp-multithreaded-communication\/","title":{"rendered":"CSharp \u591a\u7ebf\u7a0b\u901a\u4fe1"},"content":{"rendered":"<p>\u65b9\u6cd5\uff1a\u4f7f\u7528C#\u63d0\u4f9b\u7684Action&lt;&gt;\u548cFun&lt;&gt;\u6cdb\u578b\u59d4\u6258\u6765\u5904\u7406<\/p>\n<p>\u9700\u6c42\uff1a\u5f00\u542f\u4e24\u4e2a\u5b50\u7ebf\u7a0b\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23UDP\u63a5\u6536\u7f51\u7edc\u6570\u636e\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u6570\u636e\u7684UI\u754c\u9762\u663e\u793a<\/p>\n<p><!-- more --><\/p>\n<p>\u9700\u6c42\u6d89\u53ca3\u4e2a\u7ebf\u7a0b\uff1a<\/p>\n<ul>\n<li>UI\u4e3b\u7ebf\u7a0b<\/li>\n<li>\u754c\u9762\u66f4\u65b0\u7ebf\u7a0b<\/li>\n<li>UDP\u63a5\u6536\u7ebf\u7a0b<\/li>\n<\/ul>\n<pre><code class=\"language-csharp\">namespace \u7ebf\u7a0b\u901a\u77e5\u6f14\u793a\n{\n    public partial class Form1 : Form\n    {\n        private UdpClient mUdpRecvClient;\n\n        public Form1()\n        {\n            InitializeComponent();\n\n            TextBox.CheckForIllegalCrossThreadCalls = false;\n\n            \/\/UI\u663e\u793a\u7ebf\u7a0b\n            Thread showThread = new Thread(show);\n            showThread.Start();\n        }\n\n        private void show()\n        {\n            \/\/\u6cdb\u578b\u59d4\u6258\n            Action&lt;string&gt; callback = update;\n\n            \/\/UDP\u63a5\u6536\u7ebf\u7a0b\n            Thread receiveThread = new Thread(udpReceiver);\n            receiveThread.IsBackground = true;\n            receiveThread.Start(callback);\n        }\n\n        private void udpReceiver(Object obj)\n        {\n            \/\/\u83b7\u53d6\u672c\u5730\u7684IP\u5730\u5740\n            string localIP = string.Empty;\n            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)\n            {\n                if (_IPAddress.AddressFamily.ToString() == &quot;InterNetwork&quot;)\n                {\n                    localIP = _IPAddress.ToString();\n                    break;\n                }\n            }\n\n            Console.WriteLine(localIP);\n\n            IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse(localIP), 25000); \/\/\u672c\u673aIP\u548c\u76d1\u542c\u7aef\u53e3\u53f7\n\n            IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0);\n\n            mUdpRecvClient = new UdpClient(localIpep);\n\n            while (true)\n            {\n                try\n                {\n                    byte[] data = mUdpRecvClient.Receive(ref remoteIpep);\n                    string message = Encoding.Default.GetString(data, 0, data.Length);\n                    Console.WriteLine(message);\n\n                    Action&lt;string&gt; callback = obj as Action&lt;string&gt;;\n                    callback(message);\n                }\n                catch (Exception ex)\n                {\n                    Console.WriteLine(ex.Message);\n                    break;\n                }\n            }\n        }\n\n        private void update(string message)\n        {\n            textBox.Text = message;\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\uff1a\u4f7f\u7528C#\u63d0\u4f9b\u7684Action&lt;&gt;\u548cFun&lt;&gt;\u6cdb\u578b\u59d4\u6258\u6765\u5904\u7406 \u9700\u6c42\uff1a\u5f00\u542f\u4e24\u4e2a\u5b50\u7ebf\u7a0b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[518],"tags":[],"class_list":["post-2056","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2056"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2056\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}