for(int i=0;i<10;i++)
thread th1=new thread(function1)
th1.start(i)
}
public void function1(object obj)
{
}
向线程传递个参数,然后在线程里判断自己是哪个线程
网址可以放全局数组里,然后对应线程去调自己的网址字符串
thread th1=new thread(function1)
th1.start(i)
}
public void function1(object obj)
{
}
向线程传递个参数,然后在线程里判断自己是哪个线程
网址可以放全局数组里,然后对应线程去调自己的网址字符串
解决方案 »
- 将Form Hide之后 再new Form的问题
- Castle.ActiveRecord 的问题请教
- ❶基于商业控件进行第二次封装的问题, 授权这块不知道怎么搞❶
- 如何清空DataGridView里的数据,但要保留Header
- BindingSource 数据源能否是 Hashtable
- 如何修改listview某一行,某一列的值
- 怎样在按钮事件中调用外部应用程序?
- com序列化
- 求助datagrid两个简单问题
- 【线程问题】:如何实现有两个线程,当一个线程运行完毕后,通知另外一个线程偶运行好了,让另外一个线程继续运行。
- 刚换vs2012。。好多问题啊
- combobox控件的高度只能通过Font来设置吗?
th1 = new Thread(delegate() { function( "http:xxxxx"); });
th1.Start();private void fun2(string s1)
{
}
直接将字符串当参数传递进去
你开了10个线程,如果任务1超过10个页面,那么任务2,3,4 都在等待中了,并不是同时进行啊
我这只是给你写了个代码例子.具体每个线程分配哪几个页面,得你自己去处理啊.
你给线程1分配了100个页面,给2-10分配2个页面,那多线程还有什么意义啊.
你可以用个二维数组,预先存入每个线程要访问的页面url,然后各自去访问各自的页面.
然后再同步访问x1.com/002,x2.com/002,x3.com/002??
多线程只是并行处理,如果你一下给每个线程分配10个页面,它们访问速度不一样,最后执行完肯定不是完全同步的
除非你在主线程里建立个while循环
每次只给线程分一个页面,等所有线程都执行完,再给它们分下一个页面
thread.IsAlaive
private void SumPageSizes()
{
// Make a list of web addresses.
List<string> urlList = SetUpURLList(); var total = 0;
foreach (var url in urlList)
{
// GetURLContents returns the contents of url as a byte array.
byte[] urlContents = GetURLContents(url); DisplayResults(url, urlContents); // Update the total.
total += urlContents.Length;
} // Display the total count for all of the web addresses.
resultsTextBox.Text +=
string.Format("\r\n\r\nTotal bytes returned: {0}\r\n", total);
}
private List<string> SetUpURLList()
{
var urls = new List<string>
{
"http://msdn.microsoft.com/library/windows/apps/br211380.aspx",
"http://msdn.microsoft.com",
"http://msdn.microsoft.com/en-us/library/hh290136.aspx",
"http://msdn.microsoft.com/en-us/library/ee256749.aspx",
"http://msdn.microsoft.com/en-us/library/hh290138.aspx",
"http://msdn.microsoft.com/en-us/library/hh290140.aspx",
"http://msdn.microsoft.com/en-us/library/dd470362.aspx",
"http://msdn.microsoft.com/en-us/library/aa578028.aspx",
"http://msdn.microsoft.com/en-us/library/ms404677.aspx",
"http://msdn.microsoft.com/en-us/library/ff730837.aspx"
};
return urls;
}
private byte[] GetURLContents(string url)
{
// The downloaded resource ends up in the variable named content.
var content = new MemoryStream(); // Initialize an HttpWebRequest for the current URL.
var webReq = (HttpWebRequest)WebRequest.Create(url); // Send the request to the Internet resource and wait for
// the response.
// Note: you can't use HttpWebRequest.GetResponse in a Windows Store app.
using (WebResponse response = webReq.GetResponse())
{
// Get the data stream that is associated with the specified URL.
using (Stream responseStream = response.GetResponseStream())
{
// Read the bytes in responseStream and copy them to content.
responseStream.CopyTo(content);
}
} // Return the result as a byte array.
return content.ToArray();
}
private void DisplayResults(string url, byte[] content)
{
// Display the length of each website. The string format
// is designed to be used with a monospaced font, such as
// Lucida Console or Global Monospace.
var bytes = content.Length;
// Strip off the "http://".
var displayURL = url.Replace("http://", "");
resultsTextBox.Text += string.Format("\n{0,-58} {1,8}", displayURL, bytes);
}详细内容:
http://msdn.microsoft.com/zh-cn/library/vstudio/hh300224.aspx