Пример #1
0
        /// <summary>
        /// 启动
        /// </summary>
        public void StartUp()
        {
            Asyn.Exec(() =>
            {
                Syn.Wait(() => DateTime.Now >= this.StartTime);

                if (!this.HasShutDown)
                {
                    this.Exec();
                }

                Asyn.Exec(() =>
                {
                    Syn.Wait(() =>
                    {
                        if (this.HasShutDown)
                        {
                            return(true);
                        }
                        else
                        {
                            if (DateTime.Now - this.Last >= this.Interval)
                            {
                                this.Exec();
                            }

                            return(false);
                        }
                    });
                });
            });
        }
Пример #2
0
 private void Form1_Load(object sender, System.EventArgs e)
 {
     Asyn.Exec(() => {
         this.Invoke(() =>
         {
             this.Close();
         });
     });
 }
Пример #3
0
        private void Exec()
        {
            this.Last = DateTime.Now;

            switch (this.Mode)
            {
            case AgentMode.New:
                lock (this.Process)
                {
                    Asyn.Exec(() => this.Process.Start());
                }

                break;

            case AgentMode.Restart:
                lock (this.Process)
                {
                    this.Kill();

                    Asyn.Exec(() => this.Process.Start());
                }

                break;

            case AgentMode.Singleton:
                lock (this.Process)
                {
                    if (this.HasExited())
                    {
                        Asyn.Exec(() => this.Process.Start());
                    }
                }

                break;
            }
        }
Пример #4
0
        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //启动一个等待线程
            Asyn.Exec(() =>
            {
                int times = 0;

                while (times < 20)
                {
                    Thread.Sleep(1000);

                    Document document = null;

                    //交还 UI 线程获取这个时候的文档
                    this.Invoke(new Action(() =>
                    {
                        document = HtmlParser.Parse(this.webBrowser1.Document.Body.InnerHtml);
                    }));

                    //绑定选择器
                    var q = Selector.Bind(document);

                    //获取页面所有的商品
                    var sku = q(".j-sku-item");

                    List <Tuple <string, string> > list = new List <Tuple <string, string> >();

                    foreach (Element i in sku)
                    {
                        //商品名称
                        string name = q(i).find(
                            ".p-name a em").text();

                        //商品价格
                        string price = q(i).find(
                            ".p-price .J_price:first").text();

                        if (name.IsNotWhiteSpace() && price.IsNotWhiteSpace())
                        {
                            list.Add(new Tuple <string, string>(name, price));
                        }
                        //如果找不到商品名称或商品价格
                        //说明 ajax 请求还没完成
                        else
                        {
                            list.Clear();

                            break;
                        }
                    }

                    if (list.Count > 0)
                    {
                        foreach (var i in list)
                        {
                            Console.WriteLine(i.Item1 + Symbol.Tab + i.Item2);
                        }

                        break;
                    }

                    times++;
                }

                this.Invoke(new Action(() =>
                {
                    this.Close();
                }));
            });
        }
Пример #5
0
        static void Main(string[] args)
        {
            //
            MemberInfo[] members = typeof(Test2).GetPropertiesAndFields().ToArray();

            //
            string json = JsonConvert.SerializeObject(new Test1
            {
                Md5 = "test".MD5(Encoding.UTF8),
                Msg = Guid.Empty.ToBase64()
            });

            Test1 test1 = JsonConvert.DeserializeObject <Test1>(json);

            Assert.IsTrue(test1.Md5 == "test".MD5(Encoding.UTF8));
            Assert.IsTrue(test1.Msg == Guid.Empty.ToBase64());

            //
            Uri uri = new Uri("https://item.jd.com/37175085087.html");

            Assert.IsTrue(uri.GetExtension() == ".html");

            //
            uri = new Uri("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1");

            Assert.IsTrue(uri.SchemaAndHost() == "https://www.baidu.com");

            Assert.IsTrue(uri.GetUri("/test/test.html").OriginalString == "https://www.baidu.com/test/test.html");

            Assert.IsTrue(uri.IsInternalLink(uri.GetUri("/test/test.html")));

            Assert.IsFalse(uri.IsInternalLink(new Uri("https://cn.bing.com/")));

            //
            Kernel32.AddConsoleCtrlHandler(() =>
            {
                //
            });

            //
            RegEdit.UpdateWebBrowserVersion();

            //
            {
                Asyn.Exec(() =>
                {
                    Tcp.Listen("127.0.0.1", 10601, (stream) =>
                    {
                        string request = stream.ReadLine(Encoding.UTF8);

                        Console.WriteLine("request:" + request);

                        stream.Write(request.ToByteArray(Encoding.UTF8));
                    });
                });
            }

            {
                string response = Tcp.SendLine("127.0.0.1", 10601, "test", Encoding.UTF8);

                Console.WriteLine("response:" + response);

                response = Tcp.SendLine("127.0.0.1", 10601, "test", Encoding.UTF8);

                Console.WriteLine("response:" + response);
            }

            //
            Asyn.Exec(() =>
            {
                NamedPipe.Server("testPipe", (stream) =>
                {
                    string request = stream.ReadLine(Encoding.UTF8);

                    Console.WriteLine("request:" + request);

                    stream.WriteLine(request, Encoding.UTF8);
                });
            });

            {
                string response = NamedPipe.SendLine("testPipe", "hello", Encoding.UTF8);

                Console.WriteLine("response:" + response);

                response = NamedPipe.SendLine("testPipe", "hello", Encoding.UTF8);

                Console.WriteLine("response:" + response);
            }

            //
            Cmd.Exec("ping bibaoke.com");

            //
            Cmd.Exec("TestForm1.exe");

            //
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://265.com");

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        byte[] data1 = stream.ToByteArray();

                        string text1 = data1.ToString(Encoding.UTF8);

                        WebClient client = new WebClient();

                        byte[] data2 = client.DownloadData("http://265.com");

                        string text2 = data2.ToString(Encoding.UTF8);

                        Assert.IsTrue(data1.Length == data2.Length);
                    }
                }
            }

            //
            string testString = "test string";

            string file = Application.SetupDir.CombinePath(Guid.NewGuid());

            file.Write(testString, Encoding.UTF8);

            Assert.IsTrue(file.ReadString(Encoding.UTF8) == testString);

            file.DeleteFile();

            //
            Pool pool = new Pool(50);

            for (int i = 0; i < 100; i++)
            {
                pool.Execute(i, j => Console.WriteLine(j));
            }

            pool.Execute(() => Thread.Sleep(1000));

            pool.Wait();

            //
            int number1 = "10".ToInt();

            string number2String = null;

            int?number2 = number2String.ToInt();

            Assert.IsTrue(number1 == 10);

            Assert.IsTrue(number2 == null);

            //
            byte[] data = "测试字符串".ToByteArray(Encoding.UTF8);

            Base64 base64 = new Base64(data);

            HexString hex = new HexString(data);

            Assert.IsTrue(base64.ToHexString() == hex.ToString());

            Assert.IsTrue(base64 == hex.ToBase64());

            //
            Assert.IsTrue(new Uri("http://bibaoke.com/post/74").GetUri("71") == new Uri("http://bibaoke.com/post/71"));

            Assert.IsTrue(new Uri("http://bibaoke.com/post/74").GetUri("/post/71") == new Uri("http://bibaoke.com/post/71"));

            Assert.IsTrue(new Uri("http://bibaoke.com").GetUri("post/71") == new Uri("http://bibaoke.com/post/71"));

            //
            Assert.IsTrue(new Uri("http://bibaoke.com/icon").SetQuery("char=和") == new Uri("http://bibaoke.com/icon?char=和"));

            Assert.IsTrue(
                new Uri("http://bibaoke.com/icon?char=和").SetQuery("color=white") == new Uri("http://bibaoke.com/icon?char=和&color=white"));

            //
            Assert.IsTrue(new Uri("http://jianzhimao.com.com").GetDomain() == "com.com");

            Assert.IsTrue(new Uri("http://pconline.com.cn").GetDomain() == "pconline.com.cn");

            //
            Assert.IsTrue(2.Pow(3) == 8);

            //
            Assert.IsTrue("نیک تم | قالب وردپرس, قالب اورجينال فارسي و قالب وردپرس شرکتی".HasUnicode());

            //
            Encoding gbk = MyEncoding.GBK;

            //
            Assert.IsTrue("".SplitByWhiteSpace().Length == 0);

            Assert.IsTrue(" ".SplitByWhiteSpace().Length == 0);

            Assert.IsTrue(" test1  test2 ".SplitByWhiteSpace()[1] == "test2");

            //
            Assert.IsTrue(new int[] { 1, 2 }.ExtArray(new int[0]).Length == 2);

            //
            Assert.IsTrue("test".Repeat(2) == "testtest");

            //
            int[] testArray = new int[100];

            100.Each(index => testArray[index] = index);

            IEnumerable <int> enumerator = testArray.GetEnumerator(50);

            int count = 0;

            enumerator.Each((index, item) =>
            {
                Assert.IsTrue(item == index + 50);

                count++;
            });

            Assert.IsTrue(count == 50);

            //
            Assert.IsTrue("abcd".SubstringUnsafe(1, 2) == "bc");

            Assert.IsTrue("".SubstringUnsafe(0) == "");

            //
            file = Application.SetupDir.CombinePath("testReadBytes.txt");

            Assert.IsTrue(file.ReadString(Encoding.UTF8, true) == "abc");

            file.Write("abcd", Encoding.UTF8);

            Assert.IsTrue(file.ReadString(Encoding.UTF8, true) == "abcd");

            //
            Regex pattern = @"(?<order>order\s+by\s+.*?)\s*$".ToRegex(
                RegexOptions.Compiled |
                RegexOptions.ExplicitCapture |
                RegexOptions.IgnoreCase);

            Assert.IsTrue(
                pattern.Match("select * from user order by dept desc, id asc").GetValue("order") == "order by dept desc, id asc");

            Assert.IsTrue(
                pattern.Match("select * from user order by id ").GetValue("order") == "order by id");

            //
            Assert.IsTrue("90".IsInt());
            Assert.IsTrue("-90".IsInt());
            Assert.IsTrue("0".IsInt());
            Assert.IsTrue("-9".IsInt());
            Assert.IsTrue("-19".IsInt());
            Assert.IsTrue("09".IsInt());
            Assert.IsTrue("9".IsInt());
            Assert.IsFalse("1.9".IsInt());
            Assert.IsTrue(" 19".IsInt());
            Assert.IsTrue("19".IsInt());
            Assert.IsTrue("19 ".IsInt());
            Assert.IsFalse("1九".IsInt());

            //
            Assert.IsTrue("192.168.1.1".IsIpv4());
            Assert.IsFalse("192.168.1".IsIpv4());

            //
            Agent agent = new Agent("ping vaiying.com -t", AgentMode.Restart, TimeSpan.FromSeconds(2));

            agent.StartUp();

            Thread.Sleep(3000);

            agent.ShutDown();

            //
            DateTime startTime = DateTime.Now.AddSeconds(2);

            Agent notepad = new Agent("notepad", AgentMode.Singleton, startTime.Hour, startTime.Minute, startTime.Second);

            notepad.StartUp();

            Thread.Sleep(1000);

            notepad.ShutDown();

            //
            long l = 10;

            Assert.IsTrue(l.ToInt() == 10);

            l = int.MinValue;

            Assert.IsTrue(l.ToInt() == int.MinValue);

            //
            Syn.Wait(() =>
            {
                100.Each((index) =>
                {
                    Console.WriteLine(index);

                    Thread.Sleep(10);
                });

                return(true);
            });

            //
            try
            {
                Syn.Wait(TimeSpan.FromSeconds(1), () =>
                {
                    return(false);
                });
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine(ex.Message);
            }

            //
            Process.GetCurrentProcess().Kill();
        }