111,092
社区成员




public delegate void treeinvoke(int i);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("AAA");
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(this.startupdate));
th.IsBackground = true;
th.Start();
Console.WriteLine("BBB");
}
private void startupdate()
{
Console.WriteLine("CCC");
this.BeginInvoke(new treeinvoke(this.UpdateTreeView), 0);
Console.WriteLine("DDD");
}
private void UpdateTreeView(int j)
{
try
{
Console.WriteLine("EEE");
Thread.Sleep(5000);
Console.WriteLine("FFF");
}
catch (Exception ex)
{
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Text.RegularExpressions;
using System.Net;
namespace GetWebData
{
public partial class Form1 : Form
{
public delegate void treeinvoke(int i);
int num1 = 0;
int num2 = 10000;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("AAA");
for (int i = 0; i < 5000; i++)
{
ThreadPool.QueueUserWorkItem(startupdate, i);
}
Console.WriteLine("BBB");
}
private delegate void updateui();
private void TimeEvent(object source, System.Timers.ElapsedEventArgs e)
{
this.BeginInvoke(new updateui(upui));
}
private void upui()
{
label1.Text = num1.ToString() + "/" + num2.ToString();
}
private void startupdate(object c)
{
System.DateTime dt = DateTime.Now;
Console.WriteLine("CCC");
UpdateTreeView(int.Parse(c.ToString()));
Console.WriteLine("DDD");
}
private void UpdateTreeView(int j)
{
//反正这里执行耗时间操作Start
try
{
string strPageData = GetHttp("http://news.163.com/rank/", Encoding.GetEncoding("GB2312"));
this.BeginInvoke(new updateui(upui));//这里更新UI
Console.WriteLine(j.ToString());
num1 = j;
}
catch (Exception ex)
{
}
//反正这里执行耗时间操作End
}
public static string GetHttp(string url, Encoding encode)
{
string strResult;
try
{
WebClient client = new WebClient();
client.Encoding = encode;
strResult = client.DownloadString(url);
}
catch
{
strResult = "";
}
return strResult;
}
}
}
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdateTreeView));
th.IsBackground = true;
th.Start();
不明太你所需要的 多线程+异步 处理数据的意义?[/quote]
我要做的项目是:获取某些网站的数据,直接把整站所有链接都下载下来,所以用多线程异步同时下载多个网站。。。。用多线程+异步没问题吧?[/quote]
线程方式:点个按钮启动后台线程,开始下载链接。前台UI该干嘛干嘛。适合耗时很长的情况
异步:点下载按钮,开始下载数据,前台UI loading等待下载结束。适合速度较快的情况
别再多线程+异步了。
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdateTreeView));
th.IsBackground = true;
th.Start();
不明太你所需要的 多线程+异步 处理数据的意义?[/quote]
我要做的项目是:获取某些网站的数据,直接把整站所有链接都下载下来,所以用多线程异步同时下载多个网站。。。。用多线程+异步没问题吧?
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdateTreeView));
th.IsBackground = true;
th.Start();
不明太你所需要的 多线程+异步 处理数据的意义?如果有既定的顺序为何要使用异步线程? 如果按照你上面所说,先AAA再BBB,比如有烧水是沏茶的前提这样的关系,那么使用不知道谁先结束的异步线程本身逻辑上就有问题。 而你在后边补充,像“迅雷”一样,迅雷任务都是平行的,没有先后之分,相信你有后下载的东西先下完的经历。 总之,表述前后存在矛盾,希望整理清楚思路后再重新说明问题,方便大家解决。
Control.Check....这个不安全,还是要用BeginINvoke的,谢谢
理解,线程和异步是两回事。 你要多线程的话,BeginInvoke换成再new个线程好了。
BeginInvoke 会阻塞UI线程 你开再多的线程也没用 WinForm好想有个属性Control.Check....(check后面是什么忘了 反正是最长的)这个属性可以修改