Skip to content

Commit

Permalink
--真正实现了多线程,但总逻辑有点不对头。
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjohnsan committed Dec 7, 2015
1 parent 1090251 commit 9a5ed02
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 56 deletions.
Binary file modified YQSQLite.suo
Binary file not shown.
Binary file modified YQSQLite/Data/YQ.db
Binary file not shown.
21 changes: 2 additions & 19 deletions YQSQLite/NavFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,7 @@ private void DownLoadRssItem(NavUrl navurl)
break;

}
////保存RssItem数据 由于更改了RssItem类,有strat()方法,得到的RssItem对象。

//List<RssItem> listRss = new List<RssItem>();
//var q = from p in mf.DS.RssItem.AsEnumerable()
// select p;
//foreach (var i in q)
//{
// RssItem ri = new RssItem(mf.DS, i.RssItemID, i.SiteName, i.ChannelCode, i.Title, i.Link, Convert.ToDateTime(i.PubDate)
// , i.IsRead, i.Content);
// listRss.Add(ri);
//}
////多线程启动了下载内容部分。
//for (int i = 0; i < listRss.Count; i++)
//{
// listRss[i].Start();
//}
//如何更新到库?此时得到是个RssItem的集合。从集合到DS→DB???如果直接去采集内容网络数据量太大,不方便处理。把这人问题放在SelcetFrm中处理吧。


mf.SaveRssItemToDB(mf.DS.RssItem);
//更新总数 不要在这里作了。容易出错

Expand Down Expand Up @@ -216,7 +199,7 @@ private void Nomal_GetRssXml(NavUrl navurl)
if (Convert.ToDateTime(result.PubDate) > (DateTime.Now.AddDays(-7)))
{

////先在DS中加入一行,
//先在DS中加入一行,
mf.DS.RssItem.AddRssItemRow(navurl.Name, navurl.Code, result.Title.Trim(), result.Link, result.PubDate, "F", "");
//既然是新增的找ID困难,那么我们就只专心做上面的一件事,其它的:比如显示呀,多线程采集内容呀,下一步再说。现在测试

Expand Down
89 changes: 60 additions & 29 deletions YQSQLite/SelectFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,28 @@ private void btnAdd_Click(object sender, EventArgs e)
if (listView1.SelectedItems.Count > 0)
{
RssItem rssitem = listView1.SelectedItems[0].Tag as RssItem;

//从库中查找数据:以link为唯一索引的ID值
int qID = (from p in mf.DS.RssItem.AsEnumerable()
where p.Link == rssitem.Link
select p.RssItemID).FirstOrDefault();
where p.Link == rssitem.Link
select p.RssItemID).FirstOrDefault();

YQDataSet.RssItemRow rir = mf.DS.RssItem.FindByRssItemID(qID);
rssitem.IsRead = "W"; //待处理 变为 W
rssitem.Content = htmlEditor1.HTML;



mf.NewsAdd(rssitem);//重点

rir.Content = rssitem.Content;
rir.IsRead = rssitem.IsRead;
mf.rssTap.Update(rir);//todo:不能更新,ID问题,且内容部分为空。


listView1.SelectedItems[0].Remove();
htmlEditor1.HTML = "";



}
Expand Down Expand Up @@ -372,8 +372,17 @@ private void listView1_SelectedIndexChanged(object sender, EventArgs e)
//初始化
rtbCode.Clear();
rtbText.Clear();
htmlEditor1.HTML = "";
DownContent(it.Link);
if (it.Content == "")
{
// DownContent(it.Link);
}
else
{
this.Invoke(new ThreadStart(delegate { htmlEditor1.HTML = it.Content; }));

}


}
}
//定义不同网站,不同的正文定义,和排除部分源码标记
Expand Down Expand Up @@ -600,35 +609,57 @@ public SortOrder Order
#endregion

#region 导步更新ListView
public void _UplistView(ListViewItem i)
{
this.Invoke(new ThreadStart(delegate
{
listView1.Items.Add(i);

}));
}

public void ReLoadSelectFrmListView(TreeNode tn)
{
NavUrl nu = tn.Tag as NavUrl;
this.Invoke((new ThreadStart(delegate

List<RssItem> listRss = new List<RssItem>();

listView1.Items.Clear();
var q = from p in mf.DS.RssItem.AsEnumerable()
where p.ChannelCode.StartsWith(nu.Code) && p.IsRead == "F"
orderby p.PubDate descending, p.Title
select p;
foreach (var i in q)
{
listView1.Items.Clear();
var q = from p in mf.DS.RssItem.AsEnumerable()
where p.ChannelCode.StartsWith(nu.Code) && p.IsRead == "F"
orderby p.PubDate descending, p.Title
select p;
foreach (var item in q)
RssItem ri = new RssItem(mf.DS, i.RssItemID, i.SiteName, i.ChannelCode, i.Title, i.Link, Convert.ToDateTime(i.PubDate)
, i.IsRead, i.Content);
ri.uplist = new UpdataListView(_UplistView);
listRss.Add(ri);
}
//todo:这里可否运行多线程的自动下载内容部分?然后改一下,listview表中项的背景显示。

//保存RssItem数据 由于更改了RssItem类,有strat()方法,得到的RssItem对象。


//多线程启动了下载内容部分。
for (int i = 0; i < listRss.Count; i++)
{
if (listRss[i].Content == "")
{
listRss[i].Start();
}
else
{
RssItem rsit = new RssItem();
rsit.RssItemID = item.RssItemID;
rsit.SiteName = item.SiteName;
rsit.ChannelCode = item.ChannelCode;
rsit.Title = item.Title;
rsit.Link = item.Link;
rsit.PubDate = Convert.ToDateTime(item.PubDate);
rsit.IsRead = item.IsRead;
rsit.Content = item.Content;
ListViewItem lv = new ListViewItem(new string[] { rsit.Title, rsit.PubDate.ToString("yyyy-MM-dd HH:mm:ss"), rsit.SiteName });
lv.Tag = rsit;
listView1.Items.Add(lv);
DownContent(listRss[i].Link);
}
})));

}
//存盘吗???
// mf.SaveRssItemToDB(mf.DS.RssItem);

}
#endregion


}
}
Binary file modified YQSQLite/bin/Debug/YQSQLite.exe
Binary file not shown.
Binary file modified YQSQLite/bin/Debug/YQSQLite.pdb
Binary file not shown.
21 changes: 13 additions & 8 deletions YQSQLite/myClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
using ScrapySharp.Extensions;
using HtmlAgilityPack;
using System.Linq;
using System.Drawing;

namespace YQSQLite
{
// public delegate void UpdataListView(ListViewItem lv);
public delegate void UpdataListView(ListViewItem lv);
public class caiji
{
public int ID { get; set; }
Expand Down Expand Up @@ -94,16 +95,20 @@ public class RssItem
//传参用的
public YQDataSet mDs;
public Thread th;
//public UpdataListView uplist;
public UpdataListView uplist;
ListViewItem lvs = new ListViewItem();
public void Start()
{
th = new Thread(new ThreadStart(delegate
{
DownContent();

//uplist(lvs);
RssItem ri = new RssItem(this.RssItemID,this.SiteName,this.ChannelCode,this.Title,this.Link,this.PubDate,this.IsRead,this.Content);
ListViewItem lv = new ListViewItem(new string[] { this.Title, this.PubDate.ToString("yyyy-MM-dd HH:mm:ss"), this.SiteName});
lv.Tag = ri;
lv.BackColor= Color.Red;
uplist(lv);
}));
th.IsBackground = true;
th.Start();
}
public int RssItemID { get; set; }
Expand Down Expand Up @@ -205,8 +210,8 @@ private void CastCont(string link, string FlagCont)
//htmlEditor1.HTML = doc.InnerHtml;
//rtbText.Text = doc.InnerText;
this.Content = doc.InnerText;
//YQDataSet.RssItemRow rir = mDs.RssItem.FindByRssItemID(this.RssItemID);
//rir.Content = this.Content;
YQDataSet.RssItemRow rir = mDs.RssItem.FindByRssItemID(this.RssItemID);
rir.Content = this.Content;
}
}
private void CastCont(string link, string FlagCont, string[] DelFlag)
Expand Down Expand Up @@ -238,8 +243,8 @@ private void CastCont(string link, string FlagCont, string[] DelFlag)
//htmlEditor1.HTML = doc.InnerHtml;
//rtbText.Text = doc.InnerText;
this.Content = doc.InnerText;
//YQDataSet.RssItemRow rir = mDs.RssItem.FindByRssItemID(this.RssItemID);
//rir.Content = this.Content;
YQDataSet.RssItemRow rir = mDs.RssItem.FindByRssItemID(this.RssItemID);
rir.Content = this.Content;

}
}
Expand Down
Binary file modified YQSQLite/obj/x86/Debug/YQSQLite.exe
Binary file not shown.
Binary file modified YQSQLite/obj/x86/Debug/YQSQLite.pdb
Binary file not shown.
Binary file modified YQSetup/Debug/YQSetup.msi
Binary file not shown.

0 comments on commit 9a5ed02

Please sign in to comment.