我正在使用 Autodesk AutoCad API C# 。我創建了 2 行。如何在創建 Line1 和 Line 4 之間設定延遲。我想在為程式中的行創建這些行之間有延遲。
namespace ClassLibrary2
{
public class Class1
{
[CommandMethod("DrawLine1")]
public static void CreateLine()
{
Document Mydoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = Mydoc.Database;
Editor edit = Mydoc.Editor;
using (Transaction Trans = db.TransactionManager.StartTransaction())
{
BlockTable block;
block = Trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
btr = Trans.GetObject(block[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//Line1
Point3d p1 = new Point3d(0, 0, 0);
Point3d p2 = new Point3d(1000, 0, 0);
Line line_1 = new Line(p1, p2);
line_1.ColorIndex = 2;
line_1.SetDatabaseDefaults();
btr.AppendEntity(line_1);
Trans.AddNewlyCreatedDBObject(line_1, true);
//Line2
Point3d p3 = new Point3d(1200, 0, 0);
Point3d p4 = new Point3d(2000, 0, 0);
Line line_2 = new Line(p3, p4);
line_2.ColorIndex = 2;
line_2.SetDatabaseDefaults();
btr.AppendEntity(line_2);
Trans.AddNewlyCreatedDBObject(line_2, true);
Trans.Commit();
}
}
}
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD .NET Developer's Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-41a5.htm,topicNumber=d0e
uj5u.com熱心網友回復:
您可以在 C# 中簡單地引入延遲,通過添加Thread.Sleep(TimeSpan.FromSeconds(5)).
但是,這可能不會有幫助,因為我認為所有更改都是按Trans.Commit();命令發送的。所以如果你想添加一行,等待然后添加另一行,你可能需要這樣的東西:
/// this is only pseudocode
using (Transaction Trans = db.TransactionManager.StartTransaction())
{
//Add a line
Trans.Commit();
}
Thread.Sleep(TimeSpan.FromSeconds(5));
using (Transaction Trans = db.TransactionManager.StartTransaction())
{
//Add another line
Trans.Commit();
}
uj5u.com熱心網友回復:
您可以使用Thread.Sleep(1000);暫停當前??執行緒指定的時間量。1000 以毫秒為單位。
正如@karolgro 提到的(見他的例子)在你的情況下,你應該將行創建拆分為單獨的事務并設定它們之間的延遲,因為資料庫中的實際保存是在trans.Commit().
uj5u.com熱心網友回復:
有兩種方法可以做到這一點。
- 使用
System.Threading.Tasks(推薦)
int t = 1000; // milliseconds - 1 second in this example
Task.Sleep(t);
- 使用
System.Threading
int t = 1000; // same as above
Thread.Sleep(1000);
我建議您使用,System.Threading.Tasks因為您很可能Task在某個時候使用s。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331405.html
標籤:C#
