for(int i=0;i<dstServer.Tables[0].Rows.Count;i++)
{
//该目录在数据库中有对应记录
if (Convert.ToInt32(dstServer.Tables[0].Rows[i]["serverid"]) != 0 &&
Convert.ToInt32(dstServer.Tables[0].Rows[i]["step"]) == 0)
{
lock(this)
{
dstServer.Tables[0].Rows[i]["step"] = 1;
dstServer.WriteXml(xmlGameServerBill);
sDownloadFolder = dstServer.Tables[0].Rows[i]["shortname"].ToString();
sLastfilename = dstServer.Tables[0].Rows[i]["lastfilename"].ToString();
sIPAddress = dstServer.Tables[0].Rows[i]["ipaddress"].ToString();
sPassport = dstServer.Tables[0].Rows[i]["passport"].ToString();
sPassword = dstServer.Tables[0].Rows[i]["password"].ToString();
serverId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["serverid"]);
gameId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["id"]);
}
1.我按照以上的写法发现lock无效,dstServer.Tables[0].Rows[i]["step"] = 1;的记录依然会被其他线程获取
2.为什么我使用多线程CPU占用一直是100%? for(int i=0;i<10;i++)
{
Thread downloadThread = new Thread(new ThreadStart(bizDataProvider.DoFtpDownLoad));
downloadThread.IsBackground = true;
downloadThread.Start();
我是这样写的.
{
//该目录在数据库中有对应记录
if (Convert.ToInt32(dstServer.Tables[0].Rows[i]["serverid"]) != 0 &&
Convert.ToInt32(dstServer.Tables[0].Rows[i]["step"]) == 0)
{
lock(this)
{
dstServer.Tables[0].Rows[i]["step"] = 1;
dstServer.WriteXml(xmlGameServerBill);
sDownloadFolder = dstServer.Tables[0].Rows[i]["shortname"].ToString();
sLastfilename = dstServer.Tables[0].Rows[i]["lastfilename"].ToString();
sIPAddress = dstServer.Tables[0].Rows[i]["ipaddress"].ToString();
sPassport = dstServer.Tables[0].Rows[i]["passport"].ToString();
sPassword = dstServer.Tables[0].Rows[i]["password"].ToString();
serverId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["serverid"]);
gameId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["id"]);
}
1.我按照以上的写法发现lock无效,dstServer.Tables[0].Rows[i]["step"] = 1;的记录依然会被其他线程获取
2.为什么我使用多线程CPU占用一直是100%? for(int i=0;i<10;i++)
{
Thread downloadThread = new Thread(new ThreadStart(bizDataProvider.DoFtpDownLoad));
downloadThread.IsBackground = true;
downloadThread.Start();
我是这样写的.
这样写
Thread.Sleep(100)?
http://blog.csdn.net/tjvictor/archive/2007/01/20/1488290.aspx
理论应该不会出现,或者你试下用互斥量定义一个成员变量:private Mutex mx=new Mutex();
......mx.WaitOne();
dstServer.Tables[0].Rows[i]["step"] = 1;
dstServer.WriteXml(xmlGameServerBill);
sDownloadFolder = dstServer.Tables[0].Rows[i]["shortname"].ToString();
sLastfilename = dstServer.Tables[0].Rows[i]["lastfilename"].ToString();
sIPAddress = dstServer.Tables[0].Rows[i]["ipaddress"].ToString();
sPassport = dstServer.Tables[0].Rows[i]["passport"].ToString();
sPassword = dstServer.Tables[0].Rows[i]["password"].ToString();
serverId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["serverid"]);
gameId = Convert.ToInt32(dstServer.Tables[0].Rows[i]["id"]);mx.ReleaseMutex();TO:问题二
正常现象...如何消除?
可以在每次循环结束后,休眠一段时间..for example:for(..)
{
....
//比如休眠100ms
System.Threading.Thread.Sleep(100);
}
2.继续寻求第二个问题的处理方法.
已经休眠100ms 但是cpu依然 100
3.请问rar.exe 不支持多线程么?如何判断一个程序是否支持多线程?
{
//use x
}
你说你那有什么问题
谁能帮我解决个问题,,
public DataSet _ds;
public DataSet Ds
{
get
{
return _ds;
}
set
{
_ds=value;
}
}
-------------------------------------------------
private void bt_select_Click(object sender, System.EventArgs e)
{
...............................
xmllist=ReadData();
for(int i=0;i<xmllist.Count;i++)
{localhost.MyWebService myweb=new localhost.MyWebService("http://"+xmllist[i].ToString()+"/webxml/MyWebService.asmx");
_ds=myweb.SQLDB("sdf");/////////////////////////<注释1>Thread thd = new Thread( new ThreadStart(ExcuteImport));
j=i+1;
thd.Name="子线程"+j;
thd.Start();
}
}
------------------------------------------------------
private void ExcuteImport()
{
.........
for(int i=0;i<14;i++)
{
DataTable datatab=new DataTable(); datatab=Ds.Tables[i];
try
{
ImportData(datatab);
}
catch(Exception ex)
{
message.Items.Add(Thread.CurrentThread.Name+"异常消息:"+ex.Message.ToString());
Thread.CurrentThread.Abort();
Thread.CurrentThread.Join();
}
Thread.Sleep(5000);
}
}
.................
注释1:_ds=myweb.SQLDB("sdf");这里上一次的线程里的_ds会被下一个线程生成的_ds覆盖掉,这个怎么解决
一直都没有弄清楚 为什么 有时候是用 typeof() 而不是this
this 又表示什么那?
{
for(int i=0;i <dstServer.Tables[0].Rows.Count;i++)
{
//该目录在数据库中有对应记录
if (Convert.ToInt32(dstServer.Tables[0].Rows[i][ "serverid "]) != 0 &&
Convert.ToInt32(dstServer.Tables[0].Rows[i][ "step "]) == 0)
{
dstServer.Tables[0].Rows[i][ "step "] = 1;
dstServer.WriteXml(xmlGameServerBill);
sDownloadFolder = dstServer.Tables[0].Rows[i][ "shortname "].ToString();
sLastfilename = dstServer.Tables[0].Rows[i][ "lastfilename "].ToString();
sIPAddress = dstServer.Tables[0].Rows[i][ "ipaddress "].ToString();
sPassport = dstServer.Tables[0].Rows[i][ "passport "].ToString();
sPassword = dstServer.Tables[0].Rows[i][ "password "].ToString();
serverId = Convert.ToInt32(dstServer.Tables[0].Rows[i][ "serverid "]);
gameId = Convert.ToInt32(dstServer.Tables[0].Rows[i][ "id "]);
}
}