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)
{
}