Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some easy async/await changes #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions CTRE_Phoenix_GUI_Dashboard/ExportPackager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CTRE_Phoenix_GUI_Dashboard
Expand All @@ -26,7 +27,7 @@ public ExportPackager(DataGridView gridDiagnosticLog,
this.cboHostSelectorAddr = cboHostSelectorAddr;
this.cboHostSelectorPrt = cboHostSelectorPrt;
}
public void Export()
public async Task Export()
{
/* If directory doesn't exist, create it */
if (!Directory.Exists(".\\Exports"))
Expand All @@ -35,35 +36,35 @@ public void Export()
}

/* Create directory for this specific Export */
string newExportPath = String.Format(".\\Exports\\Export {0}", DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss"));
string newExportPath = $".\\Exports\\Export {DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")}";
Directory.CreateDirectory(newExportPath);

using (FileStream diagLog = new FileStream(newExportPath + "\\Diagnostic Log.txt", FileMode.Create))
{
/* Export data to a file */
ExportDiagnosticLog(diagLog);
await ExportDiagnosticLog(diagLog);

}
using (FileStream devLog = new FileStream(newExportPath + "\\Devices Log.txt", FileMode.Create))
{
/* Export data to a file */
ExportDevices(devLog);
await ExportDevices(devLog);
}
using (FileStream info = new FileStream(newExportPath + "\\Client Info.txt", FileMode.Create))
{
/* Export data to a file */
ExportClientInfo(info);
await ExportClientInfo(info);
}
/* Zip up all files */
ZipEverythingUp(newExportPath, newExportPath + ".zip");
await ZipEverythingUp(newExportPath, newExportPath + ".zip");

/* Delete original directory to save space */
new DirectoryInfo(newExportPath).Delete(true);

System.Diagnostics.Process.Start(".\\Exports");
}

private void ExportDiagnosticLog(FileStream diagLog)
private async Task ExportDiagnosticLog(FileStream diagLog)
{
string content = "";
foreach (DataGridViewRow row in gridDiagnosticLog.Rows)
Expand All @@ -74,10 +75,10 @@ private void ExportDiagnosticLog(FileStream diagLog)
row.Cells[2].Value + "\r\n";
}
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(content);
diagLog.Write(contentBytes, 0, contentBytes.Length);
await diagLog.WriteAsync(contentBytes, 0, contentBytes.Length);
}

private void ExportDevices(FileStream devLog)
private async Task ExportDevices(FileStream devLog)
{
string content = "";
foreach (ListViewItem item in lstDevices.Items)
Expand All @@ -94,10 +95,10 @@ private void ExportDevices(FileStream devLog)
device.jsonStrings.HardwareRev + "\r\n";
}
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(content);
devLog.Write(contentBytes, 0, contentBytes.Length);
await devLog.WriteAsync(contentBytes, 0, contentBytes.Length);
}

private void ExportClientInfo(FileStream connectionInfo)
private async Task ExportClientInfo(FileStream connectionInfo)
{
string a, b, c;
BackEnd.Instance.GetStatus(out a, out b, out c);
Expand All @@ -110,12 +111,15 @@ private void ExportClientInfo(FileStream connectionInfo)
content += "Client Version is: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\r\n";

byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(content);
connectionInfo.Write(contentBytes, 0, contentBytes.Length);
await connectionInfo.WriteAsync(contentBytes, 0, contentBytes.Length);
}

private void ZipEverythingUp(string sourceDestination, string zipDestination)
private Task ZipEverythingUp(string sourceDestination, string zipDestination)
{
ZipFile.CreateFromDirectory(sourceDestination, zipDestination);
return Task.Run(() =>
{
ZipFile.CreateFromDirectory(sourceDestination, zipDestination);
});
}
}
}
4 changes: 2 additions & 2 deletions CTRE_Phoenix_GUI_Dashboard/frmDashboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,14 +1137,14 @@ private void enableDisabledSftpDataTransfer_Click(object sender, EventArgs e) {

private void txtDeviceCRFPath_TextChanged(object sender, EventArgs e) { SaveStickySettings(); }

private void captureAllToolStripMenuItem_Click(object sender, EventArgs e)
private async void captureAllToolStripMenuItem_Click(object sender, EventArgs e)
{
var exportPackager = new ExportPackager(gridDiagnosticLog,
_deviceListContainer,
lstDevices,
cboHostSelectorAddr,
cboHostSelectorPrt);
exportPackager.Export();
await exportPackager.Export();
}
}
}