c#任务(WIndows Mobile 5.0上的.net 2.0)传递参数的工作方式与预期不符
在(stoneage)Windows Mobile 5.0设备上,我遇到以下演示代码问题:c#任务(WIndows Mobile 5.0上的.net 2.0)传递参数的工作方式与预期不符,c#,windows,parameter-passing,windows-mobile,multitasking,C#,Windows,Parameter Passing,Windows Mobile,Multitasking,在(stoneage)Windows Mobile 5.0设备上,我遇到以下演示代码问题: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; using System.Text; using System.Threading; namespace TaskTest1 { class Program { static L
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace TaskTest1
{
class Program
{
static List<String> Urls = new List<String>()
{
"https://www.google.de",
"https://www.wikipedia.de",
"https://kjdsfhkjfkhls.de", //non-existent
"https://kjdsfhkfgddfgfjfkhls.de", //non-existent
"https://www.msdn.com", //repeated call to same address
"https://www.msdn.com" };
static string TimeStamp()
{
return DateTime.Now.ToString("hh:mm:ss.fff");
}
static void WriteLine(String S)
{
Console.WriteLine(String.Format("{0} {1}", TimeStamp(),S));
System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}", TimeStamp(), S));
}
static void Test1Url(string Url)
{
WriteLine(String.Format("Testing: {0}",Url));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Timeout = 2000;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
long BytesRead = reader.ReadToEnd().Length;
WriteLine(String.Format("{0} responded with {1} bytes", Url, BytesRead));
}
}
else
{
WriteLine(String.Format("{0} returned error {1}", Url, response.StatusCode));
}
}
catch (System.Net.WebException e)
{
WriteLine(String.Format("Exception: call to {0} failed with error {1}", Url, e.Status));
}
}
static void Main(string[] args)
{
List<Thread> Threads = new List<Thread>();
// create one thread per url
foreach(String url in Urls)
{
Thread T = new Thread(() => Test1Url(url));
Threads.Add(T);
T.Start();
}
// Wait for all threads to complete
foreach (Thread T in Threads)
{
T.Join();
}
// ended by debugger
while (true) {};
}
}
}
在Windows Mobile上,VS 2008,.net 2.0我得到
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:44:01.000 Exception: call to https://www.msdn.com failed with error Timeout
11:44:01.000 Exception: call to https://www.msdn.com failed with error Timeout
11:44:01.000 Exception: call to https://www.msdn.com failed with error Timeout
11:44:02.000 Exception: call to https://www.msdn.com failed with error Timeout
11:44:02.000 Exception: call to https://www.msdn.com failed with error Timeout
11:44:02.000 Exception: call to https://www.msdn.com failed with error Timeout
有时,在非常罕见的情况下,我还看到:
11:43:59.000 Testing: https://www.google.de
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
似乎我的Url字符串没有正确维护。存在竞争条件,任务倾向于选择列表中的最后一个字符串
我做错了什么?这回答了你的问题吗?我能看出问题所在,只是不知道如何用语言来表达。我认为@canton7是正确的。实际上。我将以dup的形式结束这篇文章,并提供一些其他问题的链接,这些问题有很好的答案,可以解释这里发生了什么。你不是第一个违反这一点的人,这就是为什么语言设计团队决定在C#5中对该语言进行唯一突破性的更改来修复它!布尔西:-)谢谢!奇怪“罕见病例”是怎么发生的。可能是我自己在几个小时试图解决这个问题后发了疯:-)这是因为线程实际上在下一个循环迭代发生之前就开始运行了。出现此问题是因为循环在新线程有机会读取以前的
url
值之前更改了url
的值。这是否回答了您的问题?我能看出问题所在,只是不知道如何用语言来表达。我认为@canton7是正确的。实际上。我将以dup的形式结束这篇文章,并提供一些其他问题的链接,这些问题有很好的答案,可以解释这里发生了什么。你不是第一个违反这一点的人,这就是为什么语言设计团队决定在C#5中对该语言进行唯一突破性的更改来修复它!布尔西:-)谢谢!奇怪“罕见病例”是怎么发生的。可能是我自己在几个小时试图解决这个问题后发了疯:-)这是因为线程实际上在下一个循环迭代发生之前就开始运行了。出现此问题是因为循环在新线程有机会读取以前的url
值之前更改了url
的值
11:43:59.000 Testing: https://www.google.de
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com
11:43:59.000 Testing: https://www.msdn.com