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

多线程有关问题,敬

2012-02-21 
多线程问题,敬请指教!usingSystemusingSystem.DrawingusingSystem.CollectionsusingSystem.ComponentMo

多线程问题,敬请指教!
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   WatchCenter.DataAccess;
using   WatchCenter.DataModel;
using   System.Threading;
using   mo=ESRI.MapObjects2.Core;
using   System.IO;
using   System.Diagnostics;
using   DBConnSetup;
namespace   WatchCenter
{
///   <summary>
///   Form1   的摘要说明。
///   </summary>
public   class   frmMain   :   System.Windows.Forms.Form
{
private   System.Windows.Forms.ListView   listView1;
private   System.Windows.Forms.Splitter   splitter1;
private   System.Windows.Forms.Panel   panel1;
private   ESRI.MapObjects2.Core.AxMap   axMap1;
private   WatchCenterStatusAction   wcsa;
private   mo.Line   lines;
private   mo.Line   fullextent;  
private   ArrayList   lst;
private   Thread   t;
private   MapCtrl   mapctrl;
///   <summary>
///   必需的设计器变量。
///   </summary>
private   System.ComponentModel.Container   components   =   null;

public   frmMain()
{
//
//   Windows   窗体设计器支持所必需的
//
InitializeComponent();
wcsa=new   WatchCenterStatusAction();
mapctrl=new   MapCtrl();
InitializeTrackingLayer();

//
//   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()
{

}
#endregion

///   <summary>
///   应用程序的主入口点。
///   </summary>
[STAThread]
static   void   Main()  
{
try
{
string   appPath   =   Application.StartupPath;
if(!File.Exists( "key.dat "))
{
frmDBConnect   frm=new   frmDBConnect();
frm.ShowDialog();
//Process   process   =   new   Process();
//process.StartInfo.FileName   =   "DBConnSetup.exe ";
//process.Start();
//process.WaitForInputIdle();
if(null!=frm.Tag)
{
File.Create(Application.StartupPath+ "\\key.dat ");
Application.Run(new   frmMain());
}
else
{
Application.Exit();
}
}
else
{
Application.Run(new   frmMain());
}
}
catch(Exception   ee)
{
MessageBox.Show(ee.Message);
}
}

private   void   frmMain_Load(object   sender,   System.EventArgs   e)
{
mapctrl.loadLayer(this.axMap1);


listView1.Columns.Add( "供电区域 ",   80,   HorizontalAlignment.Left);
listView1.Columns.Add( "供电状态 ",   80,   HorizontalAlignment.Left);
listView1.Columns.Add( "状态开始时间 ",   120,   HorizontalAlignment.Left);
listView1.HeaderStyle=System.Windows.Forms.ColumnHeaderStyle.Clickable;
listView1.View=System.Windows.Forms.View.Details;
this.Show();
t   =   new   Thread(new   ThreadStart(Run));
t.Start();
Thread.Sleep(1000);
this.axMap1.Extent=FlashShape().Extent;
this.axMap1.CtlRefresh();
}
private   void   BuildListview()
{
bool   flag=false;
lst=wcsa.GetALLWatchCenterStatusInfo();
this.listView1.Items.Clear();
ArrayList   list=new   ArrayList();
IEnumerator   it=lst.GetEnumerator();
while(it.MoveNext())
{
WatchCenterStatus   ws=(WatchCenterStatus)it.Current;
ListViewItem   lvi=new   ListViewItem();
lvi.Text=ws.CableNum;
if(!ws.CurrentlyStatus)
{
flag=true;
list.Add(ws.CableNum);
lvi.ForeColor=System.Drawing.Color.Red;
lvi.UseItemStyleForSubItems=true;
}
ListViewItem.ListViewSubItem   lvsi=new   ListViewItem.ListViewSubItem();
lvsi.Text=ws.CurrentlyStatus   ?   "正常供电 "   :   "断电 "   ;
lvi.SubItems.Add(lvsi);
ListViewItem.ListViewSubItem   lvsii=new   ListViewItem.ListViewSubItem();
lvsii.Text=ws.BeginTime.ToString();
lvi.SubItems.Add(lvsii);
this.listView1.Items.Add(lvi);
}
this.listView1.Refresh();
if(!flag)
{
fullextent=FlashShape();
lines=null;
}
else
{
lines=FlashShape((string)list[0]);
for(int   i=1;i <list.Count;i++)
{
lines=(mo.Line)lines.Union(FlashShape((string)list[i]),this.axMap1.FullExtent);
}
}
this.BeginInvoke(new   MethodInvoker(UpdateUI));
}

//this.axMap1.Extent=lines.Extent;
//this.axMap1.FlashShape(lines,10);


//   This   method   will   update   ui   element
private   void   UpdateUI()
{
this.axMap1.TrackingLayer.ClearEvents();
if(null!=lines)
{
this.axMap1.Extent=lines.Extent;
this.axMap1.Refresh();
this.axMap1.FlashShape(this.lines,18);
this.axMap1.TrackingLayer.AddEvent(this.lines,1);
}
else
{
this.axMap1.Extent=fullextent.Extent;
this.axMap1.Refresh();
}
}


private   mo.Line   FlashShape(string   cablename)
{
mo.MapLayer   layer=(mo.MapLayer)(this.axMap1.Layers.Item( "触线网线层 "));
mo.Line   lines=null;
if(null!=layer)
{
mo.Recordset   record=layer.SearchExpression( "电缆编号= ' "+cablename+ " ' ");
if(!record.EOF)
{
lines=(mo.Line)(record.Fields.Item( "shape ").Value);  
record.MoveNext();
while(!record.EOF)
{
mo.Line   subline=(mo.Line)(record.Fields.Item( "shape ").Value);
lines=(mo.Line)(lines.Union(subline,this.axMap1.FullExtent));
record.MoveNext();
}
}  
}
return   lines;
}

private   mo.Line   FlashShape()
{
mo.MapLayer   layer=(mo.MapLayer)(this.axMap1.Layers.Item( "触线网线层 "));


mo.Line   lines=null;
if(null!=layer)
{
mo.Recordset   record=layer.Records;
if(!record.EOF)
{
lines=(mo.Line)(record.Fields.Item( "shape ").Value);  
record.MoveNext();
while(!record.EOF)
{
mo.Line   subline=(mo.Line)(record.Fields.Item( "shape ").Value);
lines=(mo.Line)(lines.Union(subline,this.axMap1.FullExtent));
record.MoveNext();
}
}  
}
return   lines;
}

private   void   listView1_MouseDown(object   sender,   System.Windows.Forms.MouseEventArgs   e)
{
//ListViewItem   lvi=this.listView1.GetItemAt(e.X,e.Y);
//if(null!=lvi)
//{
//t.Suspend();
//}

}
private   void   Run()
{
while(true)
{
BuildListview();
Thread.Sleep(3000);
}
}

private   void   frmMain_Closing(object   sender,   System.ComponentModel.CancelEventArgs   e)
{
t.Abort();
Application.ExitThread();
}

}
}

t   =   new   Thread(new   ThreadStart(Run));
t.Start();
此线程是否为工作线程?如果是为什么它可以访问BuildListview()中的代码?尤其是不通过Contrl.invoke方法就可以访问UI控件listview1?谢谢!


[解决办法]
太长了,
[解决办法]
lz:
你把问题描述清楚一点,代码太长了,你只要把出问题的几行贴出来就行了。这么长,考验人的耐心啊。。。。。
[解决办法]
t = new Thread(new ThreadStart(Run));
t.Start();
此线程是否为工作线程?如果是为什么它可以访问BuildListview()中的代码?尤其是不通过Contrl.invoke方法就可以访问UI控件listview1?谢谢!

t是工作线程
你用的是VS2003的话就可以。你用的2005的话现在就不可以。

若用2005,除了你说的invoke+委托外,还可以设置项目的Form.CheckForIllegalCrossThreadCalls = false;
[解决办法]
以上所说的可以和不可以都是指是否可以访问窗体控件。
[解决办法]
Listview1是在buildlistview()中生成的,属于t线程
[解决办法]
用Contrl.invoke方法才是最好的,也是最安全的..

热点排行