CreateDiskImageForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using DiskAccessLibrary;
  10. namespace ISCSIConsole
  11. {
  12. public partial class CreateDiskImageForm : Form
  13. {
  14. private DiskImage m_diskImage;
  15. private bool m_isWorking = false;
  16. public CreateDiskImageForm()
  17. {
  18. InitializeComponent();
  19. }
  20. private void btnCancel_Click(object sender, EventArgs e)
  21. {
  22. this.DialogResult = DialogResult.Cancel;
  23. this.Close();
  24. }
  25. private void btnOK_Click(object sender, EventArgs e)
  26. {
  27. string path = txtFilePath.Text;
  28. long size = (long)numericDiskSize.Value * 1024 * 1024;
  29. if (path == String.Empty)
  30. {
  31. MessageBox.Show("Please choose file location", "Error");
  32. return;
  33. }
  34. m_isWorking = true;
  35. new Thread(delegate()
  36. {
  37. DiskImage diskImage;
  38. try
  39. {
  40. diskImage = VirtualHardDisk.Create(path, size);
  41. }
  42. catch (IOException ex)
  43. {
  44. this.Invoke((MethodInvoker)delegate()
  45. {
  46. MessageBox.Show("Failed to create the disk: " + ex.Message, "Error");
  47. txtFilePath.Enabled = true;
  48. btnBrowse.Enabled = true;
  49. numericDiskSize.Enabled = true;
  50. btnOK.Enabled = true;
  51. btnCancel.Enabled = true;
  52. });
  53. m_isWorking = false;
  54. return;
  55. }
  56. bool isLocked = diskImage.ExclusiveLock();
  57. if (!isLocked)
  58. {
  59. this.Invoke((MethodInvoker)delegate()
  60. {
  61. MessageBox.Show("Cannot lock the disk image for exclusive access", "Error");
  62. txtFilePath.Enabled = true;
  63. btnBrowse.Enabled = true;
  64. numericDiskSize.Enabled = true;
  65. btnOK.Enabled = true;
  66. btnCancel.Enabled = true;
  67. });
  68. m_isWorking = false;
  69. return;
  70. }
  71. m_diskImage = diskImage;
  72. m_isWorking = false;
  73. this.Invoke((MethodInvoker)delegate()
  74. {
  75. this.DialogResult = DialogResult.OK;
  76. this.Close();
  77. });
  78. }).Start();
  79. txtFilePath.Enabled = false;
  80. btnBrowse.Enabled = false;
  81. numericDiskSize.Enabled = false;
  82. btnOK.Enabled = false;
  83. btnCancel.Enabled = false;
  84. }
  85. private void btnBrowse_Click(object sender, EventArgs e)
  86. {
  87. DialogResult result = saveVirtualDiskFileDialog.ShowDialog();
  88. if (result == DialogResult.OK)
  89. {
  90. txtFilePath.Text = saveVirtualDiskFileDialog.FileName;
  91. }
  92. }
  93. public DiskImage DiskImage
  94. {
  95. get
  96. {
  97. return m_diskImage;
  98. }
  99. }
  100. private void CreateDiskImageForm_FormClosing(object sender, FormClosingEventArgs e)
  101. {
  102. if (m_isWorking)
  103. {
  104. e.Cancel = true;
  105. MessageBox.Show("Please wait until the creation of the disk image is completed.", "Error");
  106. }
  107. }
  108. }
  109. }