Revit开发之多线程


关于Revit开发其实是可以使用多线程的,但是是有限制的,目前发现只要在其他线程里启用Transaction,基本Revit就崩溃了,

但是在其他线程里不启用Transaction还是可以使用的,比如说我们要在Revit里检索一些东西,但这些东西又很多,需要的时间

比较长,这种情况我们就可以把检索的任务给其他线程,然后用户先可以先进行其他操作,

下面说一个简单的例子,在Task里检索建筑柱的数量,然后显示到Window里,但是检索数量的时候,用户可以在Window里进行

其他数据的输入:


namespace MultiThreading
{
    [Transaction(TransactionMode.Manual)]
    public class Class1:IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            ViewModel vm = new ViewModel(doc);
            if (vm.ShowWindow() ?? false)
            { 
                
            }
            return Result.Succeeded;
        }
    }


    public class ViewModel:ViewModelBase
    {
        public MainWindow win = null;
        public ViewModel(Document doc)
        {
            Task task = new Task(() =>
            {
                Thread.Sleep(10000);//由于检索太快,所以让Task等待10秒
                FilteredElementCollector temc = new FilteredElementCollector(doc);
                temc.OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilyInstance));
                I = temc.Count();
                CanExecute = true;
            });
            task.Start();
            win = new MainWindow();
            win.DataContext = this;
        }


        private bool canExecute = false;
        public bool CanExecute
        {
            get
            {
                return canExecute;
            }
            set
            {
                canExecute = value;
                base.RaisePropertyChanged(() => CanExecute);
                base.RaisePropertyChanged(() => OK_Command);
            }
        }


        private int? i = null;
        public int? I
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
                base.RaisePropertyChanged(() => I);
            }
        }
        public ICommand OK_Command
        {
            get
            {
                return new RelayCommand(() => {
                    win.DialogResult = true;
                    win.Close();
                },()=>CanExecute);
            }
        }
        public ICommand Cancel_Command
        {
            get
            {
                return new RelayCommand(() =>
                {
                    win.DialogResult = false;
                    win.Close();
                });
            }
        }
        public bool? ShowWindow()
        {
            return win.ShowDialog();
        }
    }
}


<Window x:Class="MultiThreading.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Grid>
        <Label Content="柱子的数量为:" HorizontalAlignment="Left" Margin="86,97,0,0" VerticalAlignment="Top"/>
        <Button Content="确定"  Command="{Binding Path=OK_Command}" HorizontalAlignment="Left" Margin="76,229,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="取消" Command="{Binding Path=Cancel_Command}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="191,229,0,0"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="180,101,-8,0" IsReadOnly="True" TextWrapping="Wrap" Text="{Binding Path=I,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="156,169,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Label Content="其他输入:" HorizontalAlignment="Left" Margin="86,165,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>


    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值