private async Taskの場合と両方の例ですが、どちらも例外が発生しません。どうしてでしょうか?
s = ex.Messageの部分を通過させたいです。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace PrivateAccess { class Program { static void Main(string[] args) { } } public class PrivateAccess { /// <summary> public PrivateAccess() { } private void btn_Click(object sender, EventArgs e) { try { LedSwitchTaskvoid("1 0"); } catch{ throw; } } private async Task btn_Click2(object sender, EventArgs e) { try { await LedSwitchTask("1 0"); } catch{ throw; } } private async void LedSwitchTaskvoid(string allArg) { var res = await Task.Run(new Func<string>(() => { string chan; chan = allArg.Substring(0, 1); Thread.Sleep(800); return "OK"; })); throw new NotImplementedException(); } private async Task LedSwitchTask(string allArg) { var res = await Task.Run(new Func<string>(() => { string chan; chan = allArg.Substring(0, 1); Thread.Sleep(800); return "OK"; })); throw new NotImplementedException(); } } }using Microsoft.VisualStudio.TestTools.UnitTesting; using PrivateAccess; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace PrivateAccess.Tests { [TestClass()] public class PrivateAccessTests { [TestMethod()] //[DeploymentItem("SampleLibrary.dll")] public void privateasynctestvoid() { try { PrivateAccess theObject = new PrivateAccess(); Type t = theObject.GetType(); MethodInfo dynMethod = t.GetMethod("btn_Click", BindingFlags.NonPublic | BindingFlags.Instance); dynMethod.Invoke(theObject, new string[] { null, null }); } catch(Exception ex) { string s; s = ex.Message; } } [TestMethod()] //[DeploymentItem("SampleLibrary.dll")] public async Task privateasynctest() { try { PrivateAccess theObject = new PrivateAccess(); Type t = theObject.GetType(); MethodInfo dynMethod = t.GetMethod("btn_Click2", BindingFlags.NonPublic | BindingFlags.Instance); dynMethod.Invoke(theObject, new string[] { null, null }); await Task.Delay(2000).ConfigureAwait(false); } catch (Exception ex) { string s; s = ex.Message; } } } } コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/20 23:26
2017/06/20 23:32
2017/06/21 01:04