原文转自:http://www.cnblogs.com/wuhenke/archive/2009/12/10/1621421.html
4.解析Main(string[] args)中参数args。主要是在控制台中启动程序时同时赋予了参数的形式。
// ParseArgs may set values that are used elsewhere,
// such as startFullScreen and CurrentSettingsDirectory.
ParseArgs(args);
args中参数可能是:
"worldwind://":加载定位显示球体某处。
“/f” :全屏启动。
“/s=……”:指定加载“配置”的文件夹路径。
这里要注意的事,Main函数一般是没有参数的,如果我们以后要写可以在控制台下给启动程序传入参数,可以借鉴一下。
5.加载上次使用的配置信息,包括上次使用的WorldWind主窗体使用信息和上次使用的World球体显示信息
加载配置
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if(CurrentSettingsDirectory == null)
{
// load program settings from default directory
LoadSettings();
World.LoadSettings();
}
else
{
LoadSettings(CurrentSettingsDirectory);
World.LoadSettings(CurrentSettingsDirectory);
}
这里有几个知识点可以学习一下:
(1)C#对象的序列化为XML文件和反序列化Xml文件为对象,参看:http://www.cnblogs.com/wuhenke/archive/2009/12/10/1621437.html
SettingsBase类的 Load方法中的反序列化:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->XmlSerializer ser = new XmlSerializer(defaultSettings.GetType());
using(TextReader tr = new StreamReader(fileName))
{
settings = (SettingsBase)ser.Deserialize(tr);
settings.m_fileName = fileName; // remember where we loaded from for a later save
}
(2)C#中的StreamWriter类的学习使用
Log类主要用于输出Error Bug Warning等输出记录。主WorldWind.log是保存所有的记录,然后对Debug信息单独输出单个的.log文件。
logWriter负责写WorldWind.log
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->logPath = DefaultSettingsDirectory();
Directory.CreateDirectory(logPath);
// TODO: do not hardcode logfile name?
logFilePath = Path.Combine( logPath, "WorldWind.log" );
logWriter = new StreamWriter(logFilePath, true);
logWriter.AutoFlush = true;
public static void Write( Exception caught )负责写单独每个Error的输出。
单个Log文件输出
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->string functionName = "Unknown";
string[] stacktrace = null;
if (caught.StackTrace != null)
{
stacktrace = caught.StackTrace.Split('\n');
string firstStackTraceLine = stacktrace[0];
functionName = firstStackTraceLine.Trim().Split(" (".ToCharArray())[1];
}
string logFileName = string.Format("DEBUG_{0}.txt", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff")); //以DEBUG+日期作为文件名
string logFullPath = Path.Combine(logPath, logFileName);
using (StreamWriter sw = new StreamWriter(logFullPath, false))
{
sw.WriteLine(caught.ToString());
}
6.启动主程序,还有对线程异常事件处理和程序空闲处理(防止过度休眠)
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
MainApplication app = new MainApplication();
Application.Idle += new EventHandler(app.WorldWindow.OnApplicationIdle);
Application.Run(app);
7.程序启动后,将状态配置保存起来
// Save World settings
World.Settings.Save();
其中用到了,将World的配置对象WorldSettings(注意:不是其父类SettingsBase,只是调用其父类的Save方法)序列化为XML文件,并保存,以备下次启动前读取World(即球体)上次状态配置。
将WorldSettings序列化
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> public virtual void Save(string fileName)
{
XmlSerializer ser = null;
try
{
ser = new XmlSerializer(this.GetType());
using(TextWriter tw = new StreamWriter(fileName))
{
ser.Serialize(tw, this);
}
}
catch(Exception ex)
{
throw new System.Exception(String.Format("Saving settings class '{0}' to {1} failed", this.GetType().ToString(), fileName), ex);
}
}
8.保存程序整体的该次状态配置。(此处类似7,不详讲,只是这次序列化的对象是WorldWindSettings)
// Save program settings
Settings.Save();
总结:主函数内容分析学习到此完成。我们之后会针对各功能分别分析,个个突破。