下载app免费领取会员
关于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();
}
}
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。