今天在处理一个问题,就是为会员生成目录的问题,但是系统现在注册的会员有20万人,也就是要生成20万个文件夹。一开始没用多线程的时候,电脑直接卡机,现在改用多线程来处理。代码如下:
public class FoxThread
{
public FoxThread()
{ }
public FoxThread(int currentPage, int showCount, string swhere)
{
this.currengPage = currentPage;
this.showCount = showCount;
this.swhere = swhere;
}
private Thread myThread; public Thread MyThread
{
get { return myThread; }
set { myThread = value; }
}
public void StartThread()
{
myThread = new Thread(new ThreadStart(RunFunction));
myThread.Start();
}
private string connectString = "data source=(local);database=fox;uid=sa;pwd=123";
/// <summary>
/// 总的记录数
/// </summary>
private int recordCount = 0; public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
private int currengPage = 1; public int CurrengPage
{
get { return currengPage; }
set { currengPage = value; }
}
/// <summary>
/// 每页显示字段
/// </summary>
private int showCount; public int ShowCount
{
get { return showCount; }
set { showCount = value; }
}
private string swhere; public string Swhere
{
get { return swhere; }
set { swhere = value; }
}
private DataTable dtRecord; public DataTable DtRecord
{
get { return dtRecord; }
set { dtRecord = value; }
} public void RunFunction()
{
SqlConnection lvConn = new SqlConnection(connectString);
lvConn.Open();
SqlCommand lvCmd = new SqlCommand("[Page_PROC]", lvConn);//调用的存储过程
lvCmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters = {
new SqlParameter("@TblName",SqlDbType.VarChar),
new SqlParameter("@ID",SqlDbType.VarChar),
new SqlParameter("@CurrentPage",SqlDbType.Int),
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@FldName",SqlDbType.VarChar),
new SqlParameter("@OrderType",SqlDbType.Int),
new SqlParameter("@StrWhere",SqlDbType.VarChar)
};
parameters[0].Value = "users";//表名,这里是乱取的
parameters[1].Value = "localn";
parameters[2].Value = currengPage;
parameters[3].Value = showCount;
parameters[4].Value = "luckey_num,maketime";
parameters[5].Value = 1;
parameters[6].Value = swhere; foreach (SqlParameter parm in parameters)
{
lvCmd.Parameters.Add(parm);
}
SqlDataAdapter lvApter = new SqlDataAdapter(lvCmd);
DataSet ds = new DataSet();
lvApter.Fill(ds, "users");
if (ds != null)
{
DataTable dt = ds.Tables[0];
for (int j = 0; j < dt.Rows.Count; j++)
{
string gg = dt.Rows[j][1].ToString();
DateTime dd = DateTime.Parse(dt.Rows[j][1].ToString());
FolderCreate(dd.Year + "\\" + dd.Month + "\\" + dd.Day + "\\" + dt.Rows[j][0].ToString());
}
}
lvConn.Close();
lvCmd.Dispose();
lvConn.Dispose();
} public static void FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}然后在主程序中调用
RecordCount = dt.Rows.Count;
int showCount = 5000; int currentPage = 1;
int allPage=0;
allPage=RecordCount%showCount==0?RecordCount/showCount:RecordCount/showCount+1;
if(allPage<=0)
return;
for (int i = currentPage; i < allPage; i++)
{
FoxThread otherThread = new FoxThread(i, showCount, "");
otherThread.StartThread();
}
我是想通过像存储过程分页那样来生成线程,但是这样电脑还是很卡,有没有什么可以优化的方法,希望高手不吝赐教。
分数不够在加。
public class FoxThread
{
public FoxThread()
{ }
public FoxThread(int currentPage, int showCount, string swhere)
{
this.currengPage = currentPage;
this.showCount = showCount;
this.swhere = swhere;
}
private Thread myThread; public Thread MyThread
{
get { return myThread; }
set { myThread = value; }
}
public void StartThread()
{
myThread = new Thread(new ThreadStart(RunFunction));
myThread.Start();
}
private string connectString = "data source=(local);database=fox;uid=sa;pwd=123";
/// <summary>
/// 总的记录数
/// </summary>
private int recordCount = 0; public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
private int currengPage = 1; public int CurrengPage
{
get { return currengPage; }
set { currengPage = value; }
}
/// <summary>
/// 每页显示字段
/// </summary>
private int showCount; public int ShowCount
{
get { return showCount; }
set { showCount = value; }
}
private string swhere; public string Swhere
{
get { return swhere; }
set { swhere = value; }
}
private DataTable dtRecord; public DataTable DtRecord
{
get { return dtRecord; }
set { dtRecord = value; }
} public void RunFunction()
{
SqlConnection lvConn = new SqlConnection(connectString);
lvConn.Open();
SqlCommand lvCmd = new SqlCommand("[Page_PROC]", lvConn);//调用的存储过程
lvCmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters = {
new SqlParameter("@TblName",SqlDbType.VarChar),
new SqlParameter("@ID",SqlDbType.VarChar),
new SqlParameter("@CurrentPage",SqlDbType.Int),
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@FldName",SqlDbType.VarChar),
new SqlParameter("@OrderType",SqlDbType.Int),
new SqlParameter("@StrWhere",SqlDbType.VarChar)
};
parameters[0].Value = "users";//表名,这里是乱取的
parameters[1].Value = "localn";
parameters[2].Value = currengPage;
parameters[3].Value = showCount;
parameters[4].Value = "luckey_num,maketime";
parameters[5].Value = 1;
parameters[6].Value = swhere; foreach (SqlParameter parm in parameters)
{
lvCmd.Parameters.Add(parm);
}
SqlDataAdapter lvApter = new SqlDataAdapter(lvCmd);
DataSet ds = new DataSet();
lvApter.Fill(ds, "users");
if (ds != null)
{
DataTable dt = ds.Tables[0];
for (int j = 0; j < dt.Rows.Count; j++)
{
string gg = dt.Rows[j][1].ToString();
DateTime dd = DateTime.Parse(dt.Rows[j][1].ToString());
FolderCreate(dd.Year + "\\" + dd.Month + "\\" + dd.Day + "\\" + dt.Rows[j][0].ToString());
}
}
lvConn.Close();
lvCmd.Dispose();
lvConn.Dispose();
} public static void FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}然后在主程序中调用
RecordCount = dt.Rows.Count;
int showCount = 5000; int currentPage = 1;
int allPage=0;
allPage=RecordCount%showCount==0?RecordCount/showCount:RecordCount/showCount+1;
if(allPage<=0)
return;
for (int i = currentPage; i < allPage; i++)
{
FoxThread otherThread = new FoxThread(i, showCount, "");
otherThread.StartThread();
}
我是想通过像存储过程分页那样来生成线程,但是这样电脑还是很卡,有没有什么可以优化的方法,希望高手不吝赐教。
分数不够在加。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货