http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4632|回复: 6

[HttpHelper] 师哥师姐来看看 多线程更新Listview卡UI 哪里不对啊

[复制链接]
发表于 2017-3-6 16:36:54 | 显示全部楼层 |阅读模式
本帖最后由 hacker 于 2017-3-6 16:38 编辑

需求:使用多线程,将HttpHelper获取的数据动态的更新到Listview中,不知道哪里写的不对,启动后发现UI卡死。

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;

namespace MutiThread
{
    public partial class Form1 : Form
    {
        // 定义委托类
        public delegate void DEUpdateListView(int i, string str);
        // 声明一个委托对象
        DEUpdateListView DEUpdateList;
        public Form1()
        {
            InitializeComponent();
            // 初始化委托对象, 绑定委托方法
            DEUpdateList = new DEUpdateListView(UpdateListviewMethod);
        }

        //
        public string HttpServer() {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "http://1212.ip138.com/ic.asp",//URL     必需项
                Method = "GET",//URL     可选项 默认为Get
                Timeout = 100000,//连接超时时间     可选项默认为100000
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                Cookie = "",//字符串Cookie     可选项
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统     可选项有默认值
                Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                ContentType = "text/html",//返回类型    可选项有默认值
                Referer = "",//来源URL     可选项
                Allowautoredirect = false,//是否根据301跳转     可选项
                AutoRedirectCookie = false,//是否自动处理Cookie     可选项
                Postdata = "",//Post数据     可选项GET时不需要写
                ResultType = ResultType.String,//返回数据类型,是Byte还是String
            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;
            return html;
        }

        // 更新Listview方法
        public void UpdateListviewMethod(int i, string str) {

            Lsv_Thread.Items.SubItems[3].Text = HttpServer();
        }

        // 
        public void CallUpdateListview() {

            for (int i = 0; i < Lsv_Thread.Items.Count; i++) {
                this.BeginInvoke(DEUpdateList, i, "开始");
            }
        }

        // 初始化Listview
        public void InitListview()
        {
            for (int j = 0; j < 100; j++)
            {
                Lsv_Thread.Items.Add(new ListViewItem(new string[] { j.ToString(), "", "", "" }));
            }
        }

        private void Btn_Start_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(delegate {
                CallUpdateListview();
            }));
            th.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitListview();
        }
    }
}


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2017-3-6 17:32:14 | 显示全部楼层
参考一下我的一些文章,线程的写法问题,没有将Http请求这部分放到线程中
http://www.sufeinet.com/forum.ph ... on=view&ctid=26
 楼主| 发表于 2017-3-6 17:40:12 | 显示全部楼层
站长苏飞 发表于 2017-3-6 17:32
参考一下我的一些文章,线程的写法问题,没有将Http请求这部分放到线程中
http://www.sufeinet.com/forum. ...

[C#] 纯文本查看 复制代码
public void CallUpdateListview() {

            for (int i = 0; i < Lsv_Thread.Items.Count; i++) {
                string html = HttpServer();
                this.BeginInvoke(DEUpdateList, i, html);
            }
        }


老大,这个改就可以了是吧,不过在线程方法里面,想得到listview中item的值是不是不行呢? 跨线程操作了?有什么办法呢?
发表于 2017-3-7 08:22:06 | 显示全部楼层
跨线程使用委托就行了,我上面给你的文章里都有介绍,你看看应该就明白了。
发表于 2017-3-9 19:31:03 | 显示全部楼层
最简单的方法,使用匿名委托
比如说我在线程中更新textBox
this.textBox1.Invoke(new Action(()=>{this.textBox1.appendText("xxxx");}));
这样写就能防止界面假死的情况,
发表于 2017-3-10 17:57:40 | 显示全部楼层
用委托啊!
回复

使用道具 举报

发表于 2017-3-10 20:33:45 | 显示全部楼层
匿名函数
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-19 16:23

© 2014-2021

快速回复 返回顶部 返回列表