从分析Main函数开始

最BT的就是这里了,连个Application.Run()都没有——而全都是从AddIns读取。

 1        [STAThread()]
 2        public static void Main(string[] args)
 3        {
 4            commandLineArgs = args;
 5            bool noLogo = false;
 6            
 7            foreach (string arg in args) {
 8                if (arg.ToUpper().EndsWith("NOLOGO")) {
 9                    noLogo = true;
10                }

11            }

12            
13            if (!noLogo) {
14                splashScreen = new SplashScreenForm();
15                splashScreen.Show();
16            }

17            
18            ArrayList commands = null;
19            try {
20                ServiceManager.Services.InitializeServicesSubsystem("/Workspace/Services");
21            
22                commands = AddInTreeSingleton.AddInTree.GetTreeNode("/Workspace/Autostart").BuildChildItems(null);
23                
24                for (int i = 0; i < commands.Count - 1++i) {
25                    ((ICommand)commands[i]).Run();
26                }

27            }
 catch (XmlException e) {
28                MessageBox.Show("Could not load XML :\n" + e.Message);
29                return;
30            }
 catch (Exception e) {
31                MessageBox.Show("Loading error, please reinstall :\n" + e.ToString());
32                return;
33            }
 finally {
34                if (splashScreen != null{
35                    splashScreen.Close();
36                    splashScreen.Dispose();
37                }

38            }

39            
40            // run the last autostart command, this must be the workbench starting command
41            if (commands.Count > 0{
42                ((ICommand)commands[commands.Count - 1]).Run();
43            }

44            
45            // unloading services
46            ServiceManager.Services.UnloadAllServices();            
47        }

逐行分析如下:
18行之前是“每日提示”——splashScreen的现实与否,33行在最后一个命令(激活IDE主窗口)启动前关闭这个Form。
18行以后是关键:
ServiceManager这个单件,先初始化所有服务(包括Core中的4个核心服务,Base中的11个服务,其中Core中的服务调用是写死的,Base中调用的服务是动态加载SharpDevelop.Addin的节点"/Workspace/Services。

接着获取AddIn树结点"Workspace/Autostart"上所有Codons——BuildChildItems()方法,因为每个Codons都是一个命令,所以都有Run()方法作各自的事情。通过for循环依次执行这些命令。
其中,最后一个命令是"激活IDE主窗口",在确保所有Codons都正常运行时,才执行这个命令。在这个命令中,才可以看到Application.Run()这样的语句。

最后,ServiceManager这个单件卸载所有服务。


补注:另一个BT的地方是SharpDevelop中没有把编译dll目录放在各自工程的bin下,而是所有的project共享外部的同一个bin目录,于是每个需要反射dll的类中都写死了一个静态只读常量,记录这个相对路径——这套做法完全有别于VS2003,所以在把cmbx转为proj工程时,会发生dll引用丢失的问题。配置文件也是这个问题。


posted @ 2007-07-24 11:12  包建强  Views(1090)  Comments(3Edit  收藏  举报