马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Display Forms inside async AutoCAD commands
By Philippe Leefsma
You may have given a try already at the very cool async .Net 4.5 feature.
When displaying a form inside an async AutoCAD 2015 command, it happens that the synchronization context is not restored properly after closing the form, which provoke some undesirable behavior, for example the output of Editor.WriteMesage not being displayed in the command line.
A simple way to work around it is to save the current context prior to show the form and reset it once it closes.
An example of what I’m talking about just below:
// simulates an async function private static async Task<string> AsyncTask(string name) { await Task.Delay(5000); return "Hello " + name; } [CommandMethod("ADN", "SyncContextCmd", CommandFlags.Transparent)] async static public void SyncContextCmd() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // saves current synchronization context var syncContext = SynchronizationContext.Current; AdnForm form = new AdnForm(); var dialogResult = Application.ShowModalDialog(form); // restores synchronization context SynchronizationContext.SetSynchronizationContext( syncContext); if (dialogResult != System.Windows.Forms.DialogResult.OK) return; // process form result asynchronously string result = await AsyncTask(form.UserName); // editor.WriteMessage works OK now ed.WriteMessage("\nResult: " + result + "\n"); }
|