CodeSnippet/CsharpTests/TaskTests.cs

(开头部分) 16KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

#define debug
namespace CodeSnippet.CsharpTests
{
    using System;
    using System.Diagnostics;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestCategory("Task")]
    [TestClass()]

    public class TaskTests
    {
        /// <summary>
        /// 同步方式
        /// </summary>
        [TestMethod]
        public void Task_Example_01()
        {
            Stopwatch Watch = new Stopwatch();

            int CountCharacters(int id, string address)
            {
                //                using var client = new WebClient
                //                {
                //#if debug
                //                    Proxy = new WebProxy("proxy1.bj.petrochina", 8080)
                //#endif
                //                };
                var client = new WebClient();
                Console.WriteLine($"开始调用 id = {id}:{Watch.ElapsedMilliseconds} ms");
                var result = client.DownloadString(address);
                Console.WriteLine($"调用完成 id = {id}:{Watch.ElapsedMilliseconds} ms");

                return result.Length;
            }

            void ExtraOperation(int id)
            {
                // 通过拼接字符串进行一些相对耗时的操作
                StringBuilder stringBuilder = new StringBuilder();

                for (var i = 0; i < 6000; i++)
                {
                    stringBuilder.Append(id);
                }
                Console.WriteLine($"id = {id} 的 ExtraOperation 方法完成:{Watch.ElapsedMilliseconds} ms");
            }

            // 启动计时器
            Watch.Start();

            const string url1 = "https://www.baidu.com/";
            const string url2 = "https://cn.bing.com/";

            // 两次调用 CountCharacters 方法(下载某网站内容,并统计字符的个数)
后面还有 450 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。