Saturday, September 25, 2010

Across threads in C # Winform control access



CAD files in the program needs to read the list of file information, it is a very time-consuming operation, so I hope through multi-threaded approach to achieve, and can be suspended at any time. Since the initial use of multiple threads, beginning with an independent thread class that return data, Thread.Join () method to wait for the thread to write data to the form after the control, no difference between the results and single-threaded; later view information through the use of MethodInvoker solve this problem, but for the concept of threading is very vague, all who experience this regard, data and other shared learning to learn.

public partial class frmMain: Form

(

private volatile bool _stoped = false;

private void btFilter_Click (object sender, EventArgs e)

(

_stoped = false;

Thread t = new Thread (new ThreadStart (StartNewThread));

t.Start ();

)

private void btStop_Click (object sender, EventArgs e)

(

_stoped = true;

)

private void GetFileSize ()

(

btFilter.Enabled = false;

using (AutoCADConnector CAD = new AutoCADConnector ())

(

foreach (ListViewItem t in lvSource.Items)

(

if (! _stoped)

(

AutoCAD.AcadDocument doc = CAD.Application.Documents.Open (t.SubItems [2]. Text, true, "");

double [] p1 = new double [] (0, 0, 0); / / upper right corner coordinates

double [] p2 = new double [] (0, 0, 0); / / lower left corner

double scale = 0; / / scaling

p1 = (double []) doc.GetVariable ("EXTMAX");

p2 = (double []) doc.GetVariable ("EXTMIN");

scale = Convert.ToDouble (doc.GetVariable ("DIMSCALE"));

double w = Math.Ceiling ((p1 [0] - p2 [0]) / scale);

double h = Math.Ceiling ((p1 [1] - p2 [1]) / scale);

doc.Close (false, "");

t.SubItems [1]. Text = string.Format ("(0) * (1) (2)", w, h, scale);

/ / LvSource.Refresh ();

/ / Thread.Sleep (100);

Application.DoEvents ();

)

else

break;

)

)

btFilter.Enabled = true;

)

private void StartNewThread ()

(

MethodInvoker me = new MethodInvoker (this.GetFileSize);

this.BeginInvoke (me);

)

)







Recommended links:



Evaluate Audio Presentation Tools



"Warriors serpent Z" property and injury Xiangjie



Looking For A Breakthrough Signing Strangers



Evaluate XML Or CSS Tools



Make rootfs problem records



MKV To MOV



ASF To MPEG



Operation Of Supermarkets To Dealers Suggestions



Expensive PS3 late



Multimedia courseware five "tips"



Comment Download MANAGERS



VOB to Flash



Catalogs Password MANAGERS



Png Files



Across threads in C # Winform control access



Hot industry, "Painted Skin" phenomenon



No comments:

Post a Comment