---
title: "CodeSmith 使用教程(9): Progress对象"
author: "lyunabc"
date: "2013-01-15T08:17:00Z"
tags: []
slug: "codesmith-9-progress"
draft: false
lastmod: "2013-02-03T22:56:56Z"
id: 1786921
url: "https://lyunabc.iteye.com/blog/1786921"
---

使用Progress对象可以为CodeSmith生成代码的过程显示一个进度条，这对于生成比较费时的模板操作是非常有用的，如果你使用Visual Studio，可以在状态栏中显示一进度条：

[![20130106001](http://www.imobilebbs.com/wordpress/wp-content/uploads/2013/01/20130106001.png)](http://www.imobilebbs.com/wordpress/archives/4219/attachment/20130106001)

使用进度条的方法是通过CodeTemplate对象的Progress属性对象，首先是设置Progress对象的最大值和步长，本例通过一个简单的循环来模拟一个费时的操作：

```
<%@ Template Language="C#" TargetLanguage="Text" Debug="False" %>

<%@ Import Namespace="System.Threading" %>
This is a progress demo.

<% SimulateProgress(); %>

<script runat="template">
public void SimulateProgress(){

Progress.MaximumValue = 25;
Progress.Step = 1;

    for(int i=0;i<25;i++){
        Progress.PerformStep();
        Thread.Sleep(100);
        Response.WriteLine("step {0} ",i);
    }
}
</script>
```

让进度条前进一步是通过Progress对象的PerfStep方法来实现的。

本例[下载](http://www.imobilebbs.com/download/codesmith/ProgressDemo.zip)