• 近期总结


    1. 线程同步:
    private
     static readonly ManualResetEvent Event = new ManualResetEvent(false);
    in the thread:
    {
      xxx
      if (Interlocked.Decrement(ref _load) == 0)
           Event.Set();
      xxx
    }

    in the main thread:
    {
     for (int i = 0; i < _load; ++i)
      {
          ThreadPool.QueueUserWorkItem(ExecuteHiveJob)
      }   Event.WaitOne();
    }

    2. C#邮件类
         private static string _userMailUser = "user@domain.com";
            private static string _userMailPassword = "xxxxxxx";
            private static string _from = "user@domain.com";
            private static string _to = "user@domain.com";
            private static string _subject = "xxxx";
            private static string _body = "xxxx";
            private static string _smtpServer = "smtp.gmail.com"; // gmail smtp server
            private static int _smtpPort = 587;
         static void Main(string[] args)
            {
                var client = new SmtpClient();
                client.Port = _smtpPort;
                client.Host = _smtpServer;
                client.EnableSsl = true;
                client.Timeout = 10000;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(_userMailUser, _userMailPassword);
     
                var mm = new MailMessage(_from, _to, _subject, _body);
                mm.BodyEncoding = UTF8Encoding.UTF8;
                mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
     
                client.Send(mm);
            }
    
    
    3. C#异步线程调用

        
         public Task<MobileServiceCollection<T, T>> QueryRecords()
            {   
                return todoTable.ToCollectionAsync(); // 异步方法
            }

        
          // 在调用方法内,如click方法中
        
    itemsProfile = await clientProfileTable.QueryRecords(); // 使用await关键字,程序执行到这里时,会直接跳过去(返回界面),待真正查询成功后,继续执行这条语句后面的语句,很适合View Model模式的应用。
         // do something
        

    在调用过程中,也可以捕捉异常,不过需要等过一段时间才能捕捉到异常,因此,必须加await等等异常抛出并捕捉。
                try
                {
                    await BuildProfile(UserProfiles.UserName);
                }
                catch (Exception e)
                { 
                }
     







  • 相关阅读:
    锚接口(上)——hashchange api 和 $.uriAnchor
    仿B站项目(4)webpack打包第三方库jQuery
    仿B站项目(3)页面配置
    微信小程序:scroll滑到指定位置
    开发微信小程序——古龙小说阅读器
    仿B站项目——(2)环境配置,文件目录
    仿B站项目——(1)计划,前端工程
    腾讯Alloy团队代码规范
    webpack热加载:修改文件自动刷新浏览器并更新
    日时相克,困龙被伤。日落死地
  • 原文地址:https://www.cnblogs.com/codefinder/p/3673270.html
Copyright © 2020-2023  润新知