[返回首页]
 ◎ 您当前的位置:首页 >> 站长学堂 >> 经验技巧 >> 正文
用C#开N个线程生惊云soft页面
作者:未知 来源:网上收集 发布时间:2007-3-24 0:08:46

cs代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.Data; // State variables
using System.Data.OleDb; // Database
using System.Globalization; // Date
using System.Net;
using System.IO;
using System.Text.RegularEXPressions;
using System.Diagnostics;
using System.Threading;


namespace WindowsApplication5
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Data.OleDb.OleDbCommand oleDbCommand1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 40);
this.button1.TabIndex = 0;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 24);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(264, 144);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT notdown FROM [- JyDown - NotDown -]";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 267);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{

string Sortdir="Catalog";//软件分类前的前缀,可以自定义
int N=10;//开N+1个线程
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=down.asp";
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
OleDbConnection TConn = new OleDbConnection ( strCon ) ;
TConn.Open ( ) ;
//##############################取模板#####################################
string sql="select down_show from mb";
OleDbCommand cmd=new OleDbCommand(sql,myConn);
OleDbDataReader dr;
dr=cmd.ExecuteReader();
dr.Read();
string pencatbak = dr["down_show"].ToString();
dr.Close();
cmd.Dispose();
//################################
sql="select id,tname,ts from downtype order by id desc";
cmd=new OleDbCommand(sql,myConn);
dr=cmd.ExecuteReader();
string TTY_id ,TTY_name,all_type_top_id,all_type_name,thistype,txtop,sQQq;
string[] TN;
string[] TI;
string[] thistypes;
string[] txtops;
int i,h,tid;
thistypes=new string[50];
txtops=new string[50];
while (dr.Read())
{
tid=Convert.ToInt16(dr["id"].ToString());
//################ 读取标题 ################
TN = dr["tname"].ToString().Split(new Char[]{''});
TI = dr["ts"].ToString().Split(new Char[]{','});
thistype = "";
for(i = 0; i<TN.Length-1;i++)
{
if(i == TN.Length - 2 && TN.Length > 1 )
{
TTY_id = TI[i];
TTY_name = TN[i];
}
all_type_top_id = TI[i];
all_type_name = TN[i];
thistype = thistype + " >> <a href='http://"+Sortdir+TI[i]+"/"+TI[i]+"_1.htm'>" + TN[i] + "</a>";
}
thistypes[tid] = thistype;
//################ 读取标题结束 ################
//################################

sql = "select id from downtype where ts like '" + dr["ts"].ToString() + "%'";
OleDbCommand Tcmd=new OleDbCommand(sql,TConn);
OleDbDataReader Tdr;
Tdr=Tcmd.ExecuteReader();
sqqq = "";
while(Tdr.Read()){
sqqq = sqqq + "" + Tdr["id"].ToString() + ", ";
}

Tdr.Close();
Tcmd.Dispose();
//################ 读取本类top10 ################

txtop = "";
sql = "select top 10 cxn,soFTPath from down where tid =" + dr["id"].ToString() + " order by hits desc";
Tcmd=new OleDbCommand(sql,TConn);
Tdr=Tcmd.ExecuteReader();
if(Tdr.Read()==false)
{
txtop = "·还没有下载";
}
else
{
h = 0;
while(h<=10)
{
if(Tdr.Read()==false)
{
h=11;
}
else
{
h = h + 1;
txtop = txtop + "<img src="http://www.vipcn.com/InfoView/\../../images/c.gif"\" border=\"0\" width=\"1\" height=\"3\">·<a href=\"../" + Tdr["softpath"].ToString() + "\">" + Tdr["cxn"].ToString() + "</a><br>";
}
}
h = 0;
}

txtops[tid] = txtop;
Tdr.Close();
Tcmd.Dispose();
//################################
}
dr.Close();
cmd.Dispose();
TConn.Close();

//################################


//############ 统计有多少数据 #################
sql="select id from down order by id desc";
cmd=new OleDbCommand(sql,myConn);
dr=cmd.ExecuteReader();
dr.Read();
int shu=Convert.ToInt32(dr["id"].ToString());
dr.Close();
cmd.Dispose();
myConn.Close();
int perPage=Convert.ToInt16(shu/N);


//创建新线程
Thread[] tdTmp = new Thread[N+1];
for(i=0;i<=N;i++)
{
sql="select id,tid,cxn,title,hot,body,date,hits,user,gs,softpath,sortpath from down where id> "+i*perPage+" and id<="+(i+1)*perPage+" order by id";
sss=sql+""+sss;
tdTmp[i] = new Thread(new ThreadStart(new CreateHtml(sql,pencatbak,thistypes,txtops,i).CreateAB));
tdTmp[i].Start();
}
textBox1.Text = "等待弹出N+1个消息框才完成" ;
textBox1.Focus();

}
public void Thread1()
{

}

public class CreateHtml
{
string pencatbak;
string[] thistypes;
string[] txtops;
string strSQL;
int N;
public CreateHtml(string sqlTmp,string pencatbakTmp,string[] thistypesTmp,string[] txtopsTmp,int NTmp)
{
this.strSQL = sqlTmp;
this.pencatbak = pencatbakTmp;
this.thistypes = thistypesTmp;
this.txtops = txtopsTmp;
this.N = NTmp;
}
public void CreateAB()
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=down.asp";
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
OleDbConnection TConn = new OleDbConnection ( strCon ) ;
TConn.Open ( ) ;
string sql=strSQL;
OleDbCommand cmd=new OleDbCommand(sql,myConn);
OleDbDataReader dr;
dr=cmd.ExecuteReader();
int id,ttid,h;
string cxn,Title,hot,body,hits,User,gs,softpath,sortpath,dlist,xlist,pencat,foldername,ltitle;
FileStream fs;
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("GB2312");
byte[] buff;
foldername="";
while(dr.Read())
{
id = Convert.ToInt16(dr["id"].ToString());
ttid = Convert.ToInt16(dr["tid"].ToString());
cxn = dr["cxn"].ToString();
hot = dr["hot"].ToString();
body = dr["body"].ToString();
hits = dr["hits"].ToString();
User = dr["user"].ToString();
gs = dr["gs"].ToString();
softpath = dr["softpath"].ToString();
sortpath = dr["sortpath"].ToString();
//########读取下载连接
sql = "select downno from downfile where downid=" + id + " order by downno";
OleDbCommand Tcmd=new OleDbCommand(sql,TConn);
OleDbDataReader Tdr;
Tdr=Tcmd.ExecuteReader();
dlist = "";
while(Tdr.Read())
{
dlist = dlist + "<a href=\"../../down.asp?id=" + id + "&no=" + Convert.ToInt16(Tdr["downno"].ToString()) + "\">点这里下载</a> ";
}

Tdr.Close();
Tcmd.Dispose();
//#######读取相关软件
sql = "select top 10 id,cxn,softpath from down where cxn like '%" +cxn + "%' and id<>" + id + " order by date desc";
xlist = "";
try
{
Tcmd=new OleDbCommand(sql,TConn);
Tdr=Tcmd.ExecuteReader();
h = 0;
Tdr.Read();
while(h<=10)
{
if(Tdr.Read()==false)
{
h = 11;
}
else
{
xlist = xlist + "·<a href=\"../" + Tdr["softpath"].ToString() + "\">" + Tdr["cxn"].ToString() + "";
xlist = xlist + "</a><br>";
h=h+1;
}
}

h = 0;
xlist = xlist + "";
Tdr.Close();
Tcmd.Dispose();
}
catch(Exception e){}
finally{}
pencat = pencatbak;
pencat = pencat.Replace("S_CXN", cxn);
pencat = pencat.Replace("S_TXT", body);
pencat = pencat.Replace("T_TYPE", thistypes[ttid]);
pencat = pencat.Replace("Txtop", txtops[ttid]);
pencat = pencat.Replace("DOWN_LIST", dlist);
pencat = pencat.Replace("XG_LIST", xlist);
pencat = pencat.Replace("{id}", dr["id"].ToString());
//=============生成文件
foldername = Application.StartupPath + @"\" + sortpath;
softpath = Application.StartupPath + @"\" + softpath;

System.IO.Directory.CreateDirectory(foldername);

try
{

buff = gb.GetBytes(pencat);
fs = File.Open(softpath,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write,System.IO.FileShare.Write);
fs.Write (buff,0,buff.Length );
fs.Close ();


}
catch(System.IO.IOException e)
{

}
finally{}


}
System.Windows.Forms.MessageBox.Show("第"+Convert.ToString(N+1)+"个线程完成");
dr.Close();
cmd.Dispose();
TConn.Close();
myConn.Close();

}
}

private void oleDbConnection1_InfoMessage(object sender, System.Data.OleDb.OleDbInfoMessageEventArgs e)
{

}
}
}

[返回上一页] [打 印]
热点文章 推荐文章
·韩国明星站点大全
·ASP+FSO+框架实现ASP生成htm并分页的
·动网论坛首页调用
·对动网论坛作搜索引擎优化
·jsp Smart Upload上传下载全攻略
·给惊云下载系统添加ewebeditor编辑器
·[Google Adsense]如何增加点击率
·世界经典网页设计
·巧妙复制网页内容的方法
·特色网址站的制作
·右键被禁止 巧妙破解右键被禁的方法
·Web2.0式失败路径