windows call command one by one

本文介绍如何在Windows批处理文件中使用call命令来逐个调用另一个批处理程序,而不终止父批处理程序。通过引用微软官方文档和Stack Overflow上的讨论,解释了call命令和命令连接符'&'和'&&'的作用。

windows batch file call command for 200 times.

call: Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call.

REM call command for 200 times
FOR /L %%A IN (1,1,200) DO (
	ECHO %%A
	echo generate done
	REM pause

	call work_bat_0.bat
	REM pause
	call work_bat_1.bat

	echo done.
	REM pause
)
pause

&    separates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatenate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SetLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.

ref:

call | Microsoft Docs
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call
cmd - What does “&&” do in this batch file? - Stack Overflow
https://stackoverflow.com/questions/28889954/what-does-do-in-this-batch-file#:~:text=%26%26%20runs%20the%20second%20command,with%20an%20errorlevel%20of%201.&text=%26%20separates%20commands%20on%20a%20line.

<think>我们正在处理一个Revit二次开发中的特定错误:“A managed exception was thrown by Revit or by one of its external applications”。这个错误通常表明在托管代码(如C#)中发生了未处理的异常。由于用户之前的问题和回答历史中没有直接相关的内容,我们将基于常见的Revit二次开发经验来提供解决方案。 关键点: 1. 该错误通常是由于代码中的异常没有被捕获(try-catch)导致的。 2. 可能的原因包括:无效的对象引用、无效的参数传递给Revit API、在错误的上下文(非UI线程)中访问UI元素、事务处理不当等。 3. 调试此类错误的方法包括:检查事件查看器、使用日志记录、使用调试器捕获异常等。 我们将逐步提供解决方案: ### 1. 使用Try-Catch块捕获异常 在可能引发异常的代码块周围添加try-catch语句,并记录异常信息,这有助于定位问题。 ```csharp try { // 你的Revit API代码 } catch (Exception ex) { // 记录异常信息 TaskDialog.Show("Error", ex.Message); // 或者写入日志文件 } ``` ### 2. 检查事件查看器 Revit通常在Windows事件查看器中记录更详细的错误信息。查看事件查看器(特别是应用程序日志)中来自Revit的条目,可能会提供异常堆栈跟踪。 ### 3. 使用调试器 在Visual Studio中调试时,确保启用“公共语言运行时异常”的抛出中断(在“调试”>“窗口”>“异常设置”中勾选“公共语言运行时异常”),这样当异常发生时可以立即定位到代码位置。 ### 4. 检查常见的API使用错误 - **事务处理**:确保对文档的修改在事务内进行。注意事务的模式(如只读、手动或自动)。 - **元素ID引用**:注意在文档重新生成后,元素的ID可能发生变化,使用`ElementId`时要注意其有效性。 - **空引用**:在使用从API获取的对象之前,检查是否为null。 - **UI线程操作**:在非UI线程中访问UI元素会导致异常。需要使用`Application.Current.Dispatcher.Invoke`(对于WPF)或`IExternalEventHandler`来在UI线程上执行。 ### 5. 更新Revit API版本 确保你的项目引用的Revit API版本与当前使用的Revit版本匹配。版本不匹配可能导致兼容性问题。 ### 6. 检查外部引用 如果使用了第三方库,确保它们与当前Revit版本兼容并且正确引用。 ### 7. 使用RevitLookup工具 使用[RevitLookup](https://github.com/jeremytammik/RevitLookup)工具检查Revit对象的内部状态,这有助于发现对象属性或方法使用中的问题。 ### 8. 隔离问题 通过注释代码块逐步缩小问题范围,直到找到引发异常的代码行。 如果上述步骤仍无法解决问题,请提供更具体的错误上下文(如异常堆栈跟踪或引发异常的代码片段),以便进一步分析。 §§相关问题§§ 1. Revit API中事务的正确使用方法是什么? 2. 如何在Revit二次开发中调试程序? 3. 如何避免Revit二次开发中的常见错误? </think>在Revit二次开发中,"A managed exception was thrown by Revit or by one of its external applications"错误通常由以下原因引起,按优先级提供解决方案: ### 核心解决方案 1. **检查事件查看器日志** Revit会在Windows事件查看器中记录详细异常信息: - 打开`事件查看器 → Windows日志 → 应用程序` - 筛选来源为"Revit"的条目 - 查找完整的调用堆栈(Call Stack),精确锁定异常位置[^1] 2. **验证API调用上下文** 避免在以下非安全上下文中调用API方法: ```csharp // 错误示例:在外部事件处理程序外调用 UIApplication uiApp = commandData.Application; Document doc = uiApp.ActiveUIDocument.Document; // 正确做法:采用IExternalEventHandler接口 public class MyHandler : IExternalEventHandler { public void Execute(UIApplication uiApp) { // 安全上下文内执行API操作 using(Transaction t = new Transaction(doc)) { t.Start("操作"); // API调用 t.Commit(); } } } ``` 3. **事务与锁检查** - 所有修改文档的操作必须包含在事务中 - 使用`TransactionGroup`处理多步骤操作 - 检查事务模式:手动(`Manual`) vs 自动(`Automatic`) 4. **内存与泄漏排查** - 使用`.Dispose()`释放所有`FilteredElementCollector`等非托管资源 - 检查循环引用导致GC无法回收对象 - 通过`GC.GetTotalMemory()`监控内存变化 ### 进阶调试技巧 - **启用托管调试** 在Visual Studio中设置: ``` 调试 → 选项 → 启用托管兼容模式 ``` - **API版本匹配** 确认引用的`RevitAPI.dll`和`RevitAPIUI.dll`版本与目标Revit版本完全一致 - **边界值测试** 特别检查: ```csharp Element.get_Parameter() // 返回null时未处理 ElementTransformUtils.CopyElement() // 无效宿主 Curve.GetEndPoint() // 空曲线 ``` ### 预防措施 ```mermaid graph TD A[异常发生] --> B{诊断步骤} B --> C[事件查看器日志] B --> D[API上下文验证] B --> E[事务完整性检查] C --> F[定位堆栈跟踪] D --> G[重构为IExternalEventHandler] E --> H[添加事务回滚] F --> I[单元测试复现] G --> I H --> I I --> J[修正代码] ``` ### 典型修复案例 > 某插件在升级Revit 2024后出现此错误,事件日志显示: > `NullReferenceException at Autodesk.Revit.DB.Document.GetElement(ElementId)` > **原因**:插件未处理视图刷新期间元素的临时失效 > **修复**: > ```csharp > Element e = doc.GetElement(id); > if (e != null && e.IsValidObject) // 增加有效性验证 > { > // 操作逻辑 > } > ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值