コード例 #1
0
 /// <summary>
 /// 模拟上传(超时)
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         //设置超时1秒,上传耗时3秒
         var result = TaskTimeout.WaitAsync(ct => UploadAsync(ct),
                                            TimeSpan.FromMilliseconds(1000), CancellationToken.None).Result;
         MessageBox.Show($"上传返回结果:{result}");
     }
     catch (AggregateException ex)
     {
         if (ex.InnerException is TimeoutException)
         {
             MessageBox.Show($"上传超时啦....");
         }
         else
         {
             MessageBox.Show(ex.ToString());
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
コード例 #2
0
 private void button3_Click(object sender, EventArgs e)
 {
     try
     {
         //设置超时2秒,上传耗时3秒
         TaskTimeout.WaitAsync(ct => UploadAsync2(ct),
                               TimeSpan.FromMilliseconds(2000), CancellationToken.None).Wait();
         MessageBox.Show($"上传返回结果:");
     }
     catch
     {
         MessageBox.Show($"上传超时啦....");
     }
 }
コード例 #3
0
 /// <summary>
 /// 模拟上传(不超时)
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         //设置超时4秒,上传耗时3秒
         var result = TaskTimeout.WaitAsync(ct => UploadAsync(ct),
                                            TimeSpan.FromMilliseconds(4000), CancellationToken.None).Result;
         MessageBox.Show($"上传返回结果:{result}");
     }
     catch
     {
         MessageBox.Show($"上传超时啦....");
     }
 }