mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 20:54:55 +00:00
feature: write file is now on a low priority thread.
feature: add asynchronous read file methods and event
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using Crestron.SimplSharp;
|
using Crestron.SimplSharp;
|
||||||
using Crestron.SimplSharp.CrestronIO;
|
using Crestron.SimplSharp.CrestronIO;
|
||||||
using PepperDash.Core;
|
using PepperDash.Core;
|
||||||
|
using Crestron.SimplSharpPro.CrestronThread;
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
@@ -12,6 +13,8 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
|
|
||||||
static CCriticalSection fileLock = new CCriticalSection();
|
static CCriticalSection fileLock = new CCriticalSection();
|
||||||
|
public delegate void GotFileEventHandler(object sender, FileEventArgs e);
|
||||||
|
public static event GotFileEventHandler GotFileEvent;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the full file info from a path/filename, can include wildcards.
|
/// Get the full file info from a path/filename, can include wildcards.
|
||||||
@@ -100,42 +103,118 @@ namespace PepperDash.Essentials.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void ReadDataFromFileASync(string fileName)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ReadDataFromFileASync(GetFile(fileName));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ReadDataFromFileASync(FileInfo file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CrestronInvoke.BeginInvoke(o => _ReadDataFromFileASync(file));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void _ReadDataFromFileASync(FileInfo file)
|
||||||
|
{
|
||||||
|
string data;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DirectoryInfo dirInfo = new DirectoryInfo(file.Name);
|
||||||
|
Debug.Console(2, "FileIO Getting Data {0}", file.FullName);
|
||||||
|
|
||||||
|
|
||||||
|
if (File.Exists(file.FullName))
|
||||||
|
{
|
||||||
|
using (StreamReader r = new StreamReader(file.FullName))
|
||||||
|
{
|
||||||
|
data = r.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.Console(2, "File {0} Does not exsist", file.FullName);
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
|
GotFileEvent.Invoke(null, new FileEventArgs(data));
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e);
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <param name="filePath"></param>
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <param name="filePath"></param>
|
/// <param name="filePath"></param>
|
||||||
public static void WriteDataToFile(string data, string filePath)
|
public static void WriteDataToFile(string data, string filePath)
|
||||||
|
{
|
||||||
|
Thread _WriteFileThread;
|
||||||
|
_WriteFileThread = new Thread((O) => _WriteFileMethod(data, filePath), null, Thread.eThreadStartOptions.CreateSuspended);
|
||||||
|
_WriteFileThread.Priority = Thread.eThreadPriority.LowestPriority;
|
||||||
|
_WriteFileThread.Start();
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WriteFile Thread");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static object _WriteFileMethod(string data, string filePath)
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath);
|
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (fileLock.TryEnter())
|
if (fileLock.TryEnter())
|
||||||
{
|
{
|
||||||
using (StreamWriter sw = new StreamWriter(filePath))
|
using (StreamWriter sw = new StreamWriter(filePath))
|
||||||
{
|
{
|
||||||
sw.Write(data);
|
sw.Write(data);
|
||||||
sw.Flush();
|
sw.Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
else
|
||||||
{
|
{
|
||||||
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e);
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock");
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
}
|
||||||
if (fileLock != null && !fileLock.Disposed)
|
catch (Exception e)
|
||||||
fileLock.Leave();
|
{
|
||||||
|
Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (fileLock != null && !fileLock.Disposed)
|
||||||
|
fileLock.Leave();
|
||||||
|
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -163,4 +242,10 @@ namespace PepperDash.Essentials.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public class FileEventArgs
|
||||||
|
{
|
||||||
|
public FileEventArgs(string data) { Data = data; }
|
||||||
|
public string Data { get; private set; } // readonly
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user