我正在开发一个SCVMM 2012控制台插件。
SDK文档可以在这里找到:http://msdn.microsoft.com/en-us/library/jj860311.aspx
但是文档中根本没有关于线程或者插件是如何执行的信息。
下面是我所拥有的:
public class SomeAddIn : ViewAddInBase
{
private bool gotServerInfo = false;
private bool gotConnectionString = false;
public override FrameworkElement CreateViewControl()
{
GetServerInfo();
GetConnectionString();
if(gotServerInfo && gotConnectionString)
{
return GetGoodFrameworkElement(); //do some stuff to fill FrameworkElement
}
MessageBox.Show("Can't connect to DB, returning empty screen...");
return new FrameworkElement();
}
private void GetServerInfo()
{
PowerShellContext.ExecuteScript<ServerConnection>("Get-SCVMMServer localhost",
(items, error) =>
{
// code to set server info here
if (error == null)
{
gotServerInfo = true;
MessageBox.Show("Got settings from server.");
}
else{//Error}
});
}
private void GetConnectionString()
{
//PowerShell connect to database, get connection string
gotConnectionString = true; //if got string
}
}看起来不错,但问题是powershell命令需要时间来执行,并且在gotServerInfo和gotConnectionString设置为true之前首先执行"return new FrameworkElement();“。
我的猜测是,VMM为我的方法启动了多个线程,并且这些线程的执行不再是连续的。如何让VMM以正确的顺序执行我的方法?
我想要做的是:
1)对我的方法使用线程,将优先级设置为高,将当前线程的优先级设置为低,甚至设置为后台,但这并没有帮助。Thread.Join也不能工作。
2)将我的方法移动到"public override void OnLoad()或OnShow()。CreateViewControl()无论如何都会先执行。
有什么想法吗?
发布于 2015-02-10 16:19:51
这不是初始化过程中出现的问题的解决方案,但是我转向了MVVM (WPF)和一个单独的线程模型。在你建立了基本的界面之后,你想做什么以及如何同步一切都取决于你。VMM后者充当一个普通的WPF应用程序,为接口提供一个主线程。使用ThreadPool类来分派后台任务-我发现这是最简单的方法。
https://stackoverflow.com/questions/22069378
复制相似问题