/MicroFrameworkPK_v4_2/Framework/Tools/MFDeploy/Application/DeploymentStatusDialog.cs

https://bitbucket.org/pmfsampaio/netmf-lpc · C# · 236 lines · 199 code · 32 blank · 5 comment · 31 complexity · 1d8dd9115b4c2c7e5624e880eb78b22d MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Collections.ObjectModel;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using System.Threading;
  12. using System.IO;
  13. using Microsoft.NetMicroFramework.Tools.MFDeployTool.Engine;
  14. namespace Microsoft.NetMicroFramework.Tools.MFDeployTool
  15. {
  16. public partial class DeploymentStatusDialog : Form
  17. {
  18. private MFDevice m_dev;
  19. private bool m_fEraseCmd = false;
  20. private ReadOnlyCollection<string> m_files;
  21. private string [] m_sigFiles;
  22. AutoResetEvent m_evtDevice = null;
  23. ManualResetEvent m_evtShutdown = new ManualResetEvent(false);
  24. AutoResetEvent m_evtDeviceFinished = new AutoResetEvent(false);
  25. Thread m_thread;
  26. EraseOptions[] m_eraseBlocks;
  27. string m_pwd;
  28. private void OnStatus(long value, long total, string status)
  29. {
  30. if (m_evtShutdown.WaitOne(0, false)) return;
  31. textBox1.Invoke((MethodInvoker)delegate
  32. {
  33. textBox1.Text = status;
  34. textBox1.Invalidate();
  35. progressBar1.Maximum = (int)total;
  36. progressBar1.Value = (int)value;
  37. progressBar1.Invalidate();
  38. this.Update();
  39. });
  40. }
  41. public DeploymentStatusDialog(MFDevice dev, ReadOnlyCollection<string> files, string[] sig_files)
  42. : this(dev, files, sig_files, "")
  43. {
  44. }
  45. public DeploymentStatusDialog(MFDevice dev, ReadOnlyCollection<string> files, string[] sig_files, string pwd)
  46. {
  47. m_dev = dev;
  48. m_files = files;
  49. m_sigFiles = sig_files;
  50. m_pwd = pwd;
  51. InitializeComponent();
  52. }
  53. public DeploymentStatusDialog(MFDevice dev, params EraseOptions[] eraseBlocks)
  54. : this(dev, null, null)
  55. {
  56. m_fEraseCmd = true;
  57. m_eraseBlocks = eraseBlocks;
  58. this.Text = Properties.Resources.DeploymentStatusTitleErase;
  59. }
  60. private void ThreadProc()
  61. {
  62. bool fAbort = false;
  63. try
  64. {
  65. m_dev.OnProgress += new MFDevice.OnProgressHandler(OnStatus);
  66. if (m_evtShutdown.WaitOne(0, false)) return;
  67. m_evtDevice = m_dev.EventCancel;
  68. bool fMicroBooter = (m_dev.Ping() == PingConnectionType.MicroBooter);
  69. if (!fMicroBooter)
  70. {
  71. m_dev.DbgEngine.PauseExecution();
  72. }
  73. if (m_fEraseCmd)
  74. {
  75. if (!m_dev.Erase(m_eraseBlocks))
  76. {
  77. throw new MFDeployEraseFailureException();
  78. }
  79. }
  80. else
  81. {
  82. int cnt = 0;
  83. List<uint> executionPoints = new List<uint>();
  84. foreach (string file in m_files)
  85. {
  86. uint entry = 0;
  87. if (Path.GetExtension(file).ToLower() == ".nmf")
  88. {
  89. if (!m_dev.DeployUpdate(file)) throw new MFDeployDeployFailureException();
  90. return;
  91. }
  92. else if (!m_dev.Deploy(file, m_sigFiles[cnt++], ref entry))
  93. {
  94. throw new MFDeployDeployFailureException();
  95. }
  96. if (entry != 0)
  97. {
  98. executionPoints.Add(entry);
  99. }
  100. }
  101. executionPoints.Add(0);
  102. if (!fMicroBooter)
  103. {
  104. for (int i = 0; i < 10; i++)
  105. {
  106. if (m_dev.Connect(500, true)) break;
  107. }
  108. }
  109. OnStatus(100, 100, "Executing Application");
  110. foreach (uint addr in executionPoints)
  111. {
  112. if (m_dev.Execute(addr)) break;
  113. }
  114. }
  115. //if (!m_dev.CheckForMicroBooter() && m_dev.DbgEngine != null && m_dev.DbgEngine.IsConnected &&
  116. // m_dev.DbgEngine.ConnectionSource == SPOT.Debugger.ConnectionSource.TinyCLR)
  117. //{
  118. // m_dev.DbgEngine.ResumeExecution();
  119. //}
  120. }
  121. catch (ThreadAbortException)
  122. {
  123. fAbort = true;
  124. }
  125. catch (MFUserExitException)
  126. {
  127. fAbort = true;
  128. }
  129. catch (Exception e)
  130. {
  131. if (!m_evtShutdown.WaitOne(0, false))
  132. {
  133. this.Invoke((MethodInvoker)delegate
  134. {
  135. MessageBox.Show(this, Properties.Resources.ErrorPrefix + e.Message, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
  136. });
  137. }
  138. }
  139. finally
  140. {
  141. m_evtDeviceFinished.Set();
  142. if (m_dev.DbgEngine != null)
  143. {
  144. try
  145. {
  146. if (m_dev.IsConnected && m_dev.DbgEngine.ConnectionSource == SPOT.Debugger.ConnectionSource.TinyCLR)
  147. {
  148. m_dev.DbgEngine.ResumeExecution();
  149. }
  150. }
  151. catch
  152. {
  153. }
  154. }
  155. m_dev.OnProgress -= new MFDevice.OnProgressHandler(OnStatus);
  156. if (!fAbort && !m_evtShutdown.WaitOne(100, false))
  157. {
  158. this.Invoke((MethodInvoker)delegate
  159. {
  160. Close();
  161. });
  162. }
  163. }
  164. }
  165. private void DeploymentStatusDialog_Load(System.Object sender, System.EventArgs e)
  166. {
  167. m_thread = new Thread(new ThreadStart(ThreadProc));
  168. m_thread.Start();
  169. }
  170. private void Shutdown()
  171. {
  172. if (m_evtDevice != null)
  173. {
  174. m_evtDevice.Set();
  175. }
  176. if (!m_evtDeviceFinished.WaitOne(5000, false))
  177. {
  178. if (m_thread != null && m_thread.IsAlive)
  179. {
  180. m_thread.Abort();
  181. m_thread = null;
  182. }
  183. }
  184. }
  185. private void button1_Click(System.Object sender, System.EventArgs e)
  186. {
  187. m_evtShutdown.Set();
  188. Shutdown();
  189. }
  190. private void DeploymentStatusDialog_FormClosing(System.Object sender, FormClosingEventArgs e)
  191. {
  192. }
  193. }
  194. [Serializable]
  195. public class MFDeployEraseFailureException : Exception
  196. {
  197. public override string Message { get { return Properties.Resources.ErrorErase; } }
  198. }
  199. [Serializable]
  200. public class MFDeployDeployFailureException : Exception
  201. {
  202. public override string Message { get { return Properties.Resources.ErrorDeployment; } }
  203. }
  204. }