1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace testform
  8. {
  9. using System;
  10. using System.IO;
  11. using System.Security.Cryptography.X509Certificates;
  12. using System.Threading.Tasks;
  13. using DotNetty.Codecs;
  14. using DotNetty.Handlers.Logging;
  15. using DotNetty.Handlers.Tls;
  16. using DotNetty.Transport.Bootstrapping;
  17. using DotNetty.Transport.Channels;
  18. using DotNetty.Transport.Channels.Sockets;
  19. using Examples.Common;
  20. using System.Collections.Concurrent;
  21. using System.Threading;
  22.  
  23. class RunServerto
  24. {
  25. //private static BlockingCollection<MesServer> bcms = new BlockingCollection<MesServer>();
  26.  
  27. //public static string userId { get; internal set; }
  28. //public static string SAY_WELCOME { get; internal set; }
  29. //public static string BYE { get; internal set; }
  30.  
  31. //public static readonly string Content = "消息内容";
  32. //public static readonly string serverId = "Endv Cn Server";//当前托管线程的唯一标识符
  33. //public static readonly int WAIT = 20000;
  34. //internal static readonly string SAY_THANKS;
  35. //internal static readonly string WHAT;
  36. //internal static string TASK;
  37.  
  38. public static void Main(string [] args)
  39. {
  40. Parallel.Invoke(
  41. () =>
  42. {
  43. Form3 form3 = new Form3();
  44. form3.Show();
  45. //Console.WriteLine("任务1……");
  46. },
  47. () =>
  48. {
  49. //RunServerAsync().Wait();
  50. Task thread2 = new Task(() => { RunServerAsync().Wait(); });//创建新线程
  51. thread2.Start();
  52. //RunServerAsync();
  53. });
  54.  
  55. //Console.ReadKey();
  56. //Form1 form1 = new Form1();
  57. ////消费者线程
  58. //ParallelOptions po = new ParallelOptions();
  59. //po.MaxDegreeOfParallelism = -1;
  60. //Parallel.Invoke(po, () => {
  61. // ////////////////////////////////////////
  62. // //int threadid = Environment.CurrentManagedThreadId;
  63. // //MesServer server = new MesServer();
  64. // //while(true)
  65. // //{
  66. // form1.textBox2.Text = "1234567form1";
  67. // form1.Show();
  68.  
  69. // 尝试在指定的时间段内从 server 中移除一个项
  70. //if(bcms.TryTake(out server, RunServerto.WAIT))
  71. //{
  72. // server.ThreadID = threadid;
  73. // server.doAction();
  74. // //Console.WriteLine(" ");
  75. // //Console.WriteLine(server.ToString(false));
  76.  
  77. //}
  78. //else
  79. //{
  80. // if(server == null)
  81. // {
  82. // server = new MesServer();
  83. // }
  84. // server.ThreadID = threadid;
  85. // server.content = Content;
  86. // //Console.WriteLine(server.ToString(false));
  87. //}
  88. //}
  89. //},
  90. //////////////////////////////////////////
  91. //() => {
  92.  
  93. //int prdID = Environment.CurrentManagedThreadId;
  94. //MesServer server = new MesServer();
  95. //server.ProductorID = prdID;
  96. //server.content = Content;
  97. ////Console.WriteLine(productor.ToString(true));
  98. //while(true)
  99. //{
  100. // Form3 form3 = new Form3();
  101. // form3.Show();
  102. // form3.label1.Text = "";
  103. // //Console.Write("Productor Behavior (i.e. say,hello) : ");
  104. // //string msgContent = Console.ReadLine();
  105.  
  106. // form3.textBox2.Text="打开窗体3";
  107. // string msgContent = form3.textBox2.Text ;
  108. // server = new MesServer();
  109. // server.ProductorID = prdID;
  110. // //:“索引超出了数组界限。”
  111.  
  112. // //server.key = msgContent.Split(',') [0];
  113. // //server.content = msgContent.Split(',') [1];
  114. // bcms.Add(server);
  115.  
  116. //Thread thread1 = new Thread(() => { threadPro(); });//创建新线程
  117. //thread1.Start();
  118. //Thread thread2= new Thread(() => { MesServer.RunServerAsync().Wait(); });//创建新线程
  119. //thread2.Start();
  120. //MesServer.RunServerAsync().Wait();
  121. //}
  122.  
  123. //});
  124.  
  125. }
  126. public static async Task RunServerAsync()
  127. {
  128. //在控制台命令中启用日志
  129. //ExampleHelper.SetConsoleLogger();
  130. Form1 form1 = new Form1();
  131. form1.Show();
  132. //多线程事件循环组,创建一个新实例,老板组
  133. var bossGroup = new MultithreadEventLoopGroup();
  134. //多线程事件循环组,创建一个新实例,工作组
  135. var workerGroup = new MultithreadEventLoopGroup();
  136.  
  137. //字符串编码
  138. var STRING_ENCODER = new StringEncoder();
  139. //字符串解码
  140. var STRING_DECODER = new StringDecoder();
  141. //安全聊天服务器处理程序
  142. var SERVER_HANDLER = new SecureChatServerHandler();
  143.  
  144. X509Certificate2 tlsCertificate = null;
  145. if(ServerSettings.IsSsl)
  146. {
  147. // 创建 X.509 证书
  148. tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
  149. }
  150. try
  151. {
  152. //服务器启动
  153. var bootstrap = new ServerBootstrap();
  154. bootstrap
  155. .Group(bossGroup, workerGroup)
  156. //服务器套接字通道
  157. .Channel<TcpServerSocketChannel>()
  158. //所以返回日志
  159. .Option(ChannelOption.SoBacklog, )
  160. //Handler用于服务请求 “信息”日志级别。
  161. .Handler(new LoggingHandler(LogLevel.INFO))
  162. //设置{@链接channelhandler }这是用来服务请求{@链接通道}的。
  163. .ChildHandler(
  164. new ActionChannelInitializer<ISocketChannel>(channel =>
  165. {
  166. IChannelPipeline pipeline = channel.Pipeline;
  167. if(tlsCertificate != null)
  168. {
  169. //添加协议到最后
  170. pipeline.AddLast(TlsHandler.Server(tlsCertificate));
  171. }
  172. //添加基于行分隔符的帧解码器到最后
  173. pipeline.AddLast(new DelimiterBasedFrameDecoder(, Delimiters.LineDelimiter()));
  174. pipeline.AddLast(STRING_ENCODER, STRING_DECODER, SERVER_HANDLER);
  175. }));
  176. // 创建异步通道并绑定异步端口
  177. IChannel bootstrapChannel = await bootstrap.BindAsync(ServerSettings.Port);
  178. form1.textBox2.Text = "IChannel线程已启动";
  179. //Console.ReadLine();
  180. //关闭异步
  181. await bootstrapChannel.CloseAsync();
  182. }
  183. finally
  184. {
  185. // 等待提供的所有 System.Threading.Tasks.Task 对象完成执行过程
  186. Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
  187. }
  188. }
  189.  
  190. //static void threadPro()
  191. //{
  192. // //this. form3.Show();
  193. //}
  194. }
  195.  
  196. class MesServer
  197. {
  198.  
  199. //public int ThreadID { get; internal set; }//当前托管线程的唯一标识符
  200. ////public string userId { get; set; }
  201. //public string key { get; set; }
  202. //public string content { get; set; }
  203. //public int ProductorID { get; internal set; }
  204.  
  205. //public void doAction()
  206. //{
  207. // //比较
  208. // if(string.Compare(key, RunServerto.userId) == 0)
  209. // { Form1 form2 = new Form1();
  210. // content = string.Compare(content, RunServerto.SAY_THANKS) == 0 ? RunServerto.SAY_WELCOME : RunServerto.WHAT;
  211.  
  212. // form2.Show();
  213. // }
  214.  
  215. // if(string.Compare(key, RunServerto.TASK) == 0)
  216. // {
  217. // Task taskA = Task.Factory.StartNew(() => {
  218. // //Main2().Show();
  219. // //Console.WriteLine("task A begin ");
  220. // Task ChildOfFatehrA = Task.Factory.StartNew(() => {
  221. // //Console.WriteLine("Sub task A begin ");
  222. // //Thread.Sleep(1000);
  223. // //Console.WriteLine("Sub task A end ");
  224. // //Main2().Show();
  225. // Form4 form4 = new Form4();
  226. // form4.textBox2.Text = "打开窗体4";
  227. // //string msgContent = form3.textBox2.Text;
  228.  
  229. // });
  230. // ChildOfFatehrA.Wait();
  231. // Console.WriteLine("task A end ");
  232.  
  233. // });
  234. // taskA.ContinueWith(taskB => {
  235. // //Console.WriteLine("task B begin ");
  236. // //Thread.Sleep(5000);
  237. // //Console.WriteLine("task B end ");
  238. // });
  239. // taskA.Wait();
  240. // }
  241.  
  242. //}
  243.  
  244. }
  245. }

testform的更多相关文章

  1. 12、Struts2表单重复提交

    什么是表单重复提交 表单的重复提交: 若刷新表单页面, 再提交表单不算重复提交. 在不刷新表单页面的前提下: 多次点击提交按钮 已经提交成功, 按 "回退" 之后, 再点击 &qu ...

  2. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  3. Javascript本地存储小结

    前言 总括:详细讲述Cookie,LocalStorge,SesstionStorge的区别和用法. 人生如画,岁月如歌. 原文博客地址:Javascript本地存储小结 知乎专栏&& ...

  4. PHP跨域form提交

    因为安全性因素,直接跨域访问是不被允许的. 1.PHP CURL方式 function curlPost($url,$params) { $postData = ''; foreach($params ...

  5. ajax+div 代替iframe 学习尝试

    工作的时候遇到了所谓html内多tab展示的情况,主要是通过iframe来关联子页面: 不过也不知道从何时开始记得是说iframe不建议多用,所以想想,还是找找有没有其他方法(不应用于工作): 先说下 ...

  6. session跟cookie的区别

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...

  7. js 预处理用户上传图片

    前几个月闲得无聊写得一段代码,没想最近刚好用上了,在硬盘里翻了半天找回来,还没好好整理直接用上了手机用户拍照上传的图片一般都在1M 到4M 之间,如果只是用作头像尺寸就可以缩小很多,1M甚至几M的图转 ...

  8. struts.xml的编辑

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...

  9. Winform 生成不需要安装的exe可执行文件 ILMerge使用

    今天应领导要求,把一个程序打包生成一个可以执行的exe文件,不是安装包那种,类似于绿色文件,就是一个exe,可以直接运行.上网查了一下有一个工具可以实现ILMerge. 参照两个文档http://bl ...

随机推荐

  1. POJ 1698 Alice's Chance

    题目:Alice 要拍电影,每一天只能参与一部电影的拍摄,每一部电影只能在 Wi 周之内的指定的日子拍摄,总共需要花 Di 天时间,求能否拍完所有电影. 典型的二分图多重匹配,这里用了最大流的 din ...

  2. python 多进程multiprocessing 模块

    multiprocessing 常用方法: cpu_count():统计cpu核数 multiprocessing.cpu_count() active_children() 获取所有子进程 mult ...

  3. [ Linux 命令 ] awk

    一.AWK简介 awk:报告生成器,是以行为单位进行处理,并格式化后显示 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说a ...

  4. jquery.qrcode生成二维码支持中文

    基本使用方法: 1.首先在页面中加入jquery库文件和qrcode插件. <script type="text/javascript" src="jquery.j ...

  5. 《Java编程思想》笔记 第六章 访问权限控制

    1.编译单元 一个 编译单元即 .java 文件 内只能有一个 public 类  且该文件名必须与public 类名 完全一致. 编译单元内也可以没有public类 文件名可随意. 2. 包:库单元 ...

  6. SQLAlchemy技术文档(中文版)-下

    10.建立联系(外键) 是时候考虑怎样映射和查询一个和Users表关联的第二张表了.假设我们系统的用户可以存储任意数量的email地址.我们需要定义一个新表Address与User相关联. from ...

  7. dart Stream

  8. JavaScript代码放在head和body的区别(QRCode生成)

    1.在head中时,所代表的functions只加载而不执行,执行是在某一事件触发后才开始. 2.在body中时,直接加载并执行 典型的区别: 如果有不在函数中的执行语句,比如变量初始化,如果在hea ...

  9. boost::operators

    boost 的 operators 提供了comparison operators.arithmetic operators.operators for iterators 操作.虽然使用 C++ 的 ...

  10. Xamarin XAML语言教程ContentView视图作为自定义视图的父类

    Xamarin XAML语言教程ContentView视图作为自定义视图的父类 自定义视图的父类:ContentView视图可以作为自定义视图的父类. [示例14-2]以下将自定义一个颜色视图.具体的 ...