首页 新闻 会员 周边 捐助

Unable to evaluate expression because the code is optimized or a native frame is on top of the call

0
悬赏园豆:5 [已关闭问题] 关闭于 2016-07-07 15:21

private static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(test1);
t.Start(i);
}

}

--------------------------------------------------------

public static void test1(object obj)
{
int i = (int)obj;
string url_path = string.Empty;
try
{
url_path = "http://localhost:888/Test";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url_path);

using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
{
var encoding = ASCIIEncoding.UTF8;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
Console.WriteLine("{"+i+"}"+reader.ReadToEnd());
}
}
}
catch (System.Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
}

 

如上没问题

但是单元测试中就报错:

[TestMethod]
public void HttpListenerTest()
{
for (int i = 0; i < 1; i++)
{
Thread t = new Thread(test1);
t.Start(i);
}

}

异常信息:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

zzhi.wang的主页 zzhi.wang | 初学一级 | 园豆:5
提问于:2016-07-06 15:54
< >
分享
所有回答(1)
0

加个Semaphore/CountdownEvent去控制下,在最后去wait所有线程结束,否则你test方法会很快执行完而你的这些线程还在运行。

Daniel Cai | 园豆:10424 (专家六级) | 2016-07-06 17:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册