首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Winform单线程窗体假死有关问题紧急求救

2012-03-02 
Winform单线程窗体假死问题紧急求救!我winform里面有个方法publicvoidUserState(){stringfilepaththis.te

Winform单线程窗体假死问题紧急求救!
我winform   里面有个方法
public   void   UserState()
{
string   filepath=this.textBox1.Text.Trim();
string   path;
if   (filepath.Substring(filepath.Length-1,1)!=@ "\ ")
path=filepath+@ "\UserRegion ";
else
path=filepath+@ "UserRegion ";      
Directory.CreateDirectory(path);         //创建“用户管理”文件夹

DataTable   dt=getUser();

string   m1= " ";
string   m2= " ";
string   m3= " ";
string   m4= " ";
//string   restr= " ";
DrawChart   ch=new   DrawChart();
for(int   i=0;i <dt.Rows.Count;i++)
{
string   fullpath=path+@ "\ "+dt.Rows[i][ "用户名 "].ToString();
Directory.CreateDirectory(fullpath);         //创建“用户管理--用户”文件夹

Directory.CreateDirectory(fullpath+@ "\in ");     //创建“用户管理--用户---in”文件夹
Directory.CreateDirectory(fullpath+@ "\out ");   //创建“用户管理--用户---out”文件夹

string   hs_in=dt.Rows[i][ "进口过货物的HS编码 "].ToString();
string   hs_out=dt.Rows[i][ "出口过货物的HS编码 "].ToString();

string   com_name= "深圳市奇鼎实业有限公司 ";
#region   进口
if   (hs_in!= " ")
{
string   []   s_in=hs_in.Split( '$ ');
for(int   j=0;j <s_in.Length;j++)
{
string   hs1=s_in[j].ToString();
Directory.CreateDirectory(fullpath+@ "\in\ "+hs1+ " ");
m1=fullpath+@ "\in\ "+hs1+ " "+@ "\Month1 ";
m2=fullpath+@ "\in\ "+hs1+ " "+@ "\Month3 ";
m3=fullpath+@ "\in\ "+hs1+ " "+@ "\Month6 ";
m4=fullpath+@ "\in\ "+hs1+ " "+@ "\Month12 ";

Directory.CreateDirectory(m1);         //创建“用户管理--用户---in---hs编码---Month1”文件夹
Directory.CreateDirectory(m2);       //创建“用户管理--用户---in---hs编码---Month3”文件夹
Directory.CreateDirectory(m3);     //创建“用户管理--用户---in---hs编码---Month6”文件夹
Directory.CreateDirectory(m4);   //创建“用户管理--用户---in---hs编码---Month12”文件夹

label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理1个月用户进口过的HS编码是: "   +s_in[j].ToString()+ "\n ";
//调用生成图片
string   saveurl1=fullpath+@ "\in\ "+hs1+ " "+@ "\Month1\ ";
ch.DrawChartIn(hs1,1,c1Chart1,saveurl1,com_name);

this.label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理3个月用户进口过的HS编码是: "   +s_in[j].ToString()+ "\n ";
//调用生成图片
string   saveurl2=fullpath+@ "\in\ "+hs1+ " "+@ "\Month3\ ";
ch.DrawChartIn(hs1,2,c1Chart1,saveurl2,com_name);


label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理6个月用户进口过的HS编码是: "   +s_in[j].ToString()+ "\n ";
//调用生成图片
string   saveurl3=fullpath+@ "\in\ "+hs1+ " "+@ "\Month6\ ";
ch.DrawChartIn(hs1,3,c1Chart1,saveurl3,com_name);


label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理12个月用户进口过的HS编码是: "   +s_in[j].ToString()+ "\n ";
//调用生成图片
string   saveurl4=fullpath+@ "\in\ "+hs1+ " "+@ "\Month12\ ";


ch.DrawChartIn(hs1,4,c1Chart1,saveurl4,com_name);
//restr= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理的HS编码是: "   +s_in[j].ToString()+ "\n ";

}
}
#endregion  

#region   出口
if   (hs_out!= " ")
{

string   []   s_out=hs_out.Split( '$ ');
for(int   j=0;j <s_out.Length;j++)
{
string   hs2=s_out[j].ToString();
Directory.CreateDirectory(fullpath+@ "\out\ "+hs2+ " ");
m1=fullpath+@ "\out\ "+hs2+ " "+@ "\Month1 ";
m2=fullpath+@ "\out\ "+hs2+ " "+@ "\Month3 ";
m3=fullpath+@ "\out\ "+hs2+ " "+@ "\Month6 ";
m4=fullpath+@ "\out\ "+hs2+ " "+@ "\Month12 ";

Directory.CreateDirectory(m1);         //创建“用户管理--用户---out---hs编码---Month1”文件夹
Directory.CreateDirectory(m2);       //创建“用户管理--用户---out---hs编码---Month3”文件夹
Directory.CreateDirectory(m3);     //创建“用户管理--用户---out---hs编码---Month6”文件夹
Directory.CreateDirectory(m4);   //创建“用户管理--用户---out---hs编码---Month12”文件夹

label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理1个月用户出口过的HS编码是: "   +s_out[j].ToString()+ "\n ";
//调用生成图片
string   saveurl1=fullpath+@ "\out\ "+hs2+ " "+@ "\Month1\ ";
ch.DrawChartOut(hs2,1,c1Chart1,saveurl1,com_name);

label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理3个月用户出口过的HS编码是: "   +s_out[j].ToString()+ "\n ";
//调用生成图片
string   saveurl2=fullpath+@ "\out\ "+hs2+ " "+@ "\Month3\ ";
ch.DrawChartOut(hs2,2,c1Chart1,saveurl2,com_name);


label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理6个月用户出口过的HS编码是: "   +s_out[j].ToString()+ "\n ";
//调用生成图片
string   saveurl3=fullpath+@ "\out\ "+hs2+ " "+@ "\Month6\ ";
ch.DrawChartOut(hs2,3,c1Chart1,saveurl3,com_name);


label1.Text= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理12个月用户出口过的HS编码是: "   +s_out[j].ToString()+ "\n ";
//调用生成图片
string   saveurl4=fullpath+@ "\out\ "+hs2+ " "+@ "\Month12\ ";
ch.DrawChartOut(hs2,4,c1Chart1,saveurl4,com_name);
//restr= "正在处理的用户是: "+dt.Rows[i][ "用户名 "]+ "\n处理的HS编码是: "   +s_out[j].ToString()+ "\n ";

}
}


#endregion  
}
label1.Text= "图形处理完毕! ";
}

然后在form_load事件里:
thread=new   Thread(new   ThreadStart(this.UserState));
然后在开始按钮的单击事件里
thread.Start();
问题就出来了只要我把这个窗体最小化或移动
窗体就假死了但程序还在执行并且可以执行完毕。
因为我这个方法处理时间较长所以就用了个单线程。
我想问怎么解决这个问题啊?

[解决办法]
up
[解决办法]
异步调用……

[解决办法]
thread=new Thread(new ThreadStart(this.UserInvoke));
thread.IsBackground= true;
thread.Start();

……

private void UserInvoke()
{
MethodInvoker mi = new MethodInvoker(UserState);
while (true)


{
this.BeginInvoke(mi);
Thread.Sleep(300) ;
}
}
private void UserState()
{

//do your code

}
[解决办法]
异步调用的时候显示label.text的内容需要用代理啊~
[解决办法]
委托. 详见MSDN 使用委托在线程间修改数据.. 或者把要修改对象的线程安全检查关闭, 就可以直接访问.
[解决办法]
关闭CheckForIllegalCrossThreadCalls
[解决办法]
可以使用backgroundWorker组件
[解决办法]
顶上

热点排行