testform
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace testform
- {
- using System;
- using System.IO;
- using System.Security.Cryptography.X509Certificates;
- using System.Threading.Tasks;
- using DotNetty.Codecs;
- using DotNetty.Handlers.Logging;
- using DotNetty.Handlers.Tls;
- using DotNetty.Transport.Bootstrapping;
- using DotNetty.Transport.Channels;
- using DotNetty.Transport.Channels.Sockets;
- using Examples.Common;
- using System.Collections.Concurrent;
- using System.Threading;
- class RunServerto
- {
- //private static BlockingCollection<MesServer> bcms = new BlockingCollection<MesServer>();
- //public static string userId { get; internal set; }
- //public static string SAY_WELCOME { get; internal set; }
- //public static string BYE { get; internal set; }
- //public static readonly string Content = "消息内容";
- //public static readonly string serverId = "Endv Cn Server";//当前托管线程的唯一标识符
- //public static readonly int WAIT = 20000;
- //internal static readonly string SAY_THANKS;
- //internal static readonly string WHAT;
- //internal static string TASK;
- public static void Main(string [] args)
- {
- Parallel.Invoke(
- () =>
- {
- Form3 form3 = new Form3();
- form3.Show();
- //Console.WriteLine("任务1……");
- },
- () =>
- {
- //RunServerAsync().Wait();
- Task thread2 = new Task(() => { RunServerAsync().Wait(); });//创建新线程
- thread2.Start();
- //RunServerAsync();
- });
- //Console.ReadKey();
- //Form1 form1 = new Form1();
- ////消费者线程
- //ParallelOptions po = new ParallelOptions();
- //po.MaxDegreeOfParallelism = -1;
- //Parallel.Invoke(po, () => {
- // ////////////////////////////////////////
- // //int threadid = Environment.CurrentManagedThreadId;
- // //MesServer server = new MesServer();
- // //while(true)
- // //{
- // form1.textBox2.Text = "1234567form1";
- // form1.Show();
- // 尝试在指定的时间段内从 server 中移除一个项
- //if(bcms.TryTake(out server, RunServerto.WAIT))
- //{
- // server.ThreadID = threadid;
- // server.doAction();
- // //Console.WriteLine(" ");
- // //Console.WriteLine(server.ToString(false));
- //}
- //else
- //{
- // if(server == null)
- // {
- // server = new MesServer();
- // }
- // server.ThreadID = threadid;
- // server.content = Content;
- // //Console.WriteLine(server.ToString(false));
- //}
- //}
- //},
- //////////////////////////////////////////
- //() => {
- //int prdID = Environment.CurrentManagedThreadId;
- //MesServer server = new MesServer();
- //server.ProductorID = prdID;
- //server.content = Content;
- ////Console.WriteLine(productor.ToString(true));
- //while(true)
- //{
- // Form3 form3 = new Form3();
- // form3.Show();
- // form3.label1.Text = "";
- // //Console.Write("Productor Behavior (i.e. say,hello) : ");
- // //string msgContent = Console.ReadLine();
- // form3.textBox2.Text="打开窗体3";
- // string msgContent = form3.textBox2.Text ;
- // server = new MesServer();
- // server.ProductorID = prdID;
- // //:“索引超出了数组界限。”
- // //server.key = msgContent.Split(',') [0];
- // //server.content = msgContent.Split(',') [1];
- // bcms.Add(server);
- //Thread thread1 = new Thread(() => { threadPro(); });//创建新线程
- //thread1.Start();
- //Thread thread2= new Thread(() => { MesServer.RunServerAsync().Wait(); });//创建新线程
- //thread2.Start();
- //MesServer.RunServerAsync().Wait();
- //}
- //});
- }
- public static async Task RunServerAsync()
- {
- //在控制台命令中启用日志
- //ExampleHelper.SetConsoleLogger();
- Form1 form1 = new Form1();
- form1.Show();
- //多线程事件循环组,创建一个新实例,老板组
- var bossGroup = new MultithreadEventLoopGroup();
- //多线程事件循环组,创建一个新实例,工作组
- var workerGroup = new MultithreadEventLoopGroup();
- //字符串编码
- var STRING_ENCODER = new StringEncoder();
- //字符串解码
- var STRING_DECODER = new StringDecoder();
- //安全聊天服务器处理程序
- var SERVER_HANDLER = new SecureChatServerHandler();
- X509Certificate2 tlsCertificate = null;
- if(ServerSettings.IsSsl)
- {
- // 创建 X.509 证书
- tlsCertificate = new X509Certificate2(Path.Combine(ExampleHelper.ProcessDirectory, "dotnetty.com.pfx"), "password");
- }
- try
- {
- //服务器启动
- var bootstrap = new ServerBootstrap();
- bootstrap
- .Group(bossGroup, workerGroup)
- //服务器套接字通道
- .Channel<TcpServerSocketChannel>()
- //所以返回日志
- .Option(ChannelOption.SoBacklog, )
- //Handler用于服务请求 “信息”日志级别。
- .Handler(new LoggingHandler(LogLevel.INFO))
- //设置{@链接channelhandler }这是用来服务请求{@链接通道}的。
- .ChildHandler(
- new ActionChannelInitializer<ISocketChannel>(channel =>
- {
- IChannelPipeline pipeline = channel.Pipeline;
- if(tlsCertificate != null)
- {
- //添加协议到最后
- pipeline.AddLast(TlsHandler.Server(tlsCertificate));
- }
- //添加基于行分隔符的帧解码器到最后
- pipeline.AddLast(new DelimiterBasedFrameDecoder(, Delimiters.LineDelimiter()));
- pipeline.AddLast(STRING_ENCODER, STRING_DECODER, SERVER_HANDLER);
- }));
- // 创建异步通道并绑定异步端口
- IChannel bootstrapChannel = await bootstrap.BindAsync(ServerSettings.Port);
- form1.textBox2.Text = "IChannel线程已启动";
- //Console.ReadLine();
- //关闭异步
- await bootstrapChannel.CloseAsync();
- }
- finally
- {
- // 等待提供的所有 System.Threading.Tasks.Task 对象完成执行过程
- Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
- }
- }
- //static void threadPro()
- //{
- // //this. form3.Show();
- //}
- }
- class MesServer
- {
- //public int ThreadID { get; internal set; }//当前托管线程的唯一标识符
- ////public string userId { get; set; }
- //public string key { get; set; }
- //public string content { get; set; }
- //public int ProductorID { get; internal set; }
- //public void doAction()
- //{
- // //比较
- // if(string.Compare(key, RunServerto.userId) == 0)
- // { Form1 form2 = new Form1();
- // content = string.Compare(content, RunServerto.SAY_THANKS) == 0 ? RunServerto.SAY_WELCOME : RunServerto.WHAT;
- // form2.Show();
- // }
- // if(string.Compare(key, RunServerto.TASK) == 0)
- // {
- // Task taskA = Task.Factory.StartNew(() => {
- // //Main2().Show();
- // //Console.WriteLine("task A begin ");
- // Task ChildOfFatehrA = Task.Factory.StartNew(() => {
- // //Console.WriteLine("Sub task A begin ");
- // //Thread.Sleep(1000);
- // //Console.WriteLine("Sub task A end ");
- // //Main2().Show();
- // Form4 form4 = new Form4();
- // form4.textBox2.Text = "打开窗体4";
- // //string msgContent = form3.textBox2.Text;
- // });
- // ChildOfFatehrA.Wait();
- // Console.WriteLine("task A end ");
- // });
- // taskA.ContinueWith(taskB => {
- // //Console.WriteLine("task B begin ");
- // //Thread.Sleep(5000);
- // //Console.WriteLine("task B end ");
- // });
- // taskA.Wait();
- // }
- //}
- }
- }
testform的更多相关文章
- 12、Struts2表单重复提交
什么是表单重复提交 表单的重复提交: 若刷新表单页面, 再提交表单不算重复提交. 在不刷新表单页面的前提下: 多次点击提交按钮 已经提交成功, 按 "回退" 之后, 再点击 &qu ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- Javascript本地存储小结
前言 总括:详细讲述Cookie,LocalStorge,SesstionStorge的区别和用法. 人生如画,岁月如歌. 原文博客地址:Javascript本地存储小结 知乎专栏&& ...
- PHP跨域form提交
因为安全性因素,直接跨域访问是不被允许的. 1.PHP CURL方式 function curlPost($url,$params) { $postData = ''; foreach($params ...
- ajax+div 代替iframe 学习尝试
工作的时候遇到了所谓html内多tab展示的情况,主要是通过iframe来关联子页面: 不过也不知道从何时开始记得是说iframe不建议多用,所以想想,还是找找有没有其他方法(不应用于工作): 先说下 ...
- session跟cookie的区别
这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...
- js 预处理用户上传图片
前几个月闲得无聊写得一段代码,没想最近刚好用上了,在硬盘里翻了半天找回来,还没好好整理直接用上了手机用户拍照上传的图片一般都在1M 到4M 之间,如果只是用作头像尺寸就可以缩小很多,1M甚至几M的图转 ...
- struts.xml的编辑
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- Winform 生成不需要安装的exe可执行文件 ILMerge使用
今天应领导要求,把一个程序打包生成一个可以执行的exe文件,不是安装包那种,类似于绿色文件,就是一个exe,可以直接运行.上网查了一下有一个工具可以实现ILMerge. 参照两个文档http://bl ...
随机推荐
- POJ 1698 Alice's Chance
题目:Alice 要拍电影,每一天只能参与一部电影的拍摄,每一部电影只能在 Wi 周之内的指定的日子拍摄,总共需要花 Di 天时间,求能否拍完所有电影. 典型的二分图多重匹配,这里用了最大流的 din ...
- python 多进程multiprocessing 模块
multiprocessing 常用方法: cpu_count():统计cpu核数 multiprocessing.cpu_count() active_children() 获取所有子进程 mult ...
- [ Linux 命令 ] awk
一.AWK简介 awk:报告生成器,是以行为单位进行处理,并格式化后显示 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说a ...
- jquery.qrcode生成二维码支持中文
基本使用方法: 1.首先在页面中加入jquery库文件和qrcode插件. <script type="text/javascript" src="jquery.j ...
- 《Java编程思想》笔记 第六章 访问权限控制
1.编译单元 一个 编译单元即 .java 文件 内只能有一个 public 类 且该文件名必须与public 类名 完全一致. 编译单元内也可以没有public类 文件名可随意. 2. 包:库单元 ...
- SQLAlchemy技术文档(中文版)-下
10.建立联系(外键) 是时候考虑怎样映射和查询一个和Users表关联的第二张表了.假设我们系统的用户可以存储任意数量的email地址.我们需要定义一个新表Address与User相关联. from ...
- dart Stream
- JavaScript代码放在head和body的区别(QRCode生成)
1.在head中时,所代表的functions只加载而不执行,执行是在某一事件触发后才开始. 2.在body中时,直接加载并执行 典型的区别: 如果有不在函数中的执行语句,比如变量初始化,如果在hea ...
- boost::operators
boost 的 operators 提供了comparison operators.arithmetic operators.operators for iterators 操作.虽然使用 C++ 的 ...
- Xamarin XAML语言教程ContentView视图作为自定义视图的父类
Xamarin XAML语言教程ContentView视图作为自定义视图的父类 自定义视图的父类:ContentView视图可以作为自定义视图的父类. [示例14-2]以下将自定义一个颜色视图.具体的 ...