{"id":86,"date":"2020-06-17T05:31:59","date_gmt":"2020-06-17T05:31:59","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=86"},"modified":"2020-06-17T05:31:59","modified_gmt":"2020-06-17T05:31:59","slug":"reporter-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/17\/reporter-cs\/","title":{"rendered":"Reporter.cs"},"content":{"rendered":"\n<p>Thread Reporter used with IProgress<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Concurrent;\nusing System.Collections.Generic;\nusing System.IO;\nusing System.Runtime.InteropServices;\nusing System.Windows.Forms;\n[ComVisible(true)]\n[Serializable]\n[StructLayout(LayoutKind.Sequential)]\npublic class Reporter\n{\n    public readonly ConcurrentDictionary&lt;string, object> GValues  = new ConcurrentDictionary&lt;string, object>();\n    public          List&lt;string>                         ErrorLog = new List&lt;string>();\n    public Reporter()\n    {\n    }\n    public Reporter(string DescriptorID)\n    {\n        LoadDescriptorFile(DescriptorID);\n    }\n    public string ProgramDir\n    {\n        get\n        {\n            var dir = Services.GetEnvironmentEx(\"Local AppData\") + \"\\\\\" + Application.ProductName + \"\\\\\";\n            if (!Directory.Exists(dir))\n                Directory.CreateDirectory(dir);\n            return dir;\n        }\n    }\n    public void SaveDescriptorFile(string DescriptorID)\n    {\n        var anx = ProgramDir + DescriptorID + \"_file.bin\";\n        var iel = GValues.ToArray();\n        using (var wrt = new Writer(anx))\n        {\n            foreach (var i in iel)\n            {\n                wrt.WriteString(i.Key);\n                wrt.WriteBytes(i.Value.GetBytesObjectSerial());\n            }\n        }\n    }\n    public void LoadDescriptorFile(string DescriptorID)\n    {\n        var anx = ProgramDir + DescriptorID + \"_file.bin\";\n        if (!File.Exists(anx))\n            throw new Exception($\"file {anx} does not exist.\");\n        GValues.Clear();\n        using (var rdr = new Reader(anx))\n        {\n            var key   = rdr.ReadString();\n            var value = rdr.ReadBytes();\n            GValues.TryAdd(key, value);\n        }\n    }\n    public bool DescriptorFileExists(string DescriptorID)\n    {\n        var anx = ProgramDir + DescriptorID + \"_file.bin\";\n        return File.Exists(anx);\n    }\n    public bool CheckDescriptorExists(string DescriptorID)\n    {\n        return GValues.ContainsKey(DescriptorID);\n    }\n    public void BuildDescriptorList(List&lt;string> lst)\n    {\n        foreach (var i in lst)\n            Set(i, default);\n    }\n    public void Set(string DescriptorID, object value)\n    {\n        if (!GValues.ContainsKey(DescriptorID))\n            GValues.TryAdd(DescriptorID, value);\n        else GValues[DescriptorID] = value;\n    }\n    public bool GetBool(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToBoolean(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return false;\n    }\n    public char GetChar(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToChar(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return ' ';\n    }\n    public sbyte GetSByte(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToSByte(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public byte GetByte(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToByte(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public short GetShort(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToInt16(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public ushort GetUShort(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToUInt16(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public int GetInt(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToInt32(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public uint GetUInt(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToUInt32(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public long GetLong(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToInt64(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public ulong GetULong(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToUInt64(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public float GetFloat(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToSingle(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public double GetDouble(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToDouble(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public decimal GetDecimal(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToDecimal(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return 0;\n    }\n    public string GetString(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToString(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return \" \";\n    }\n    public DateTime GetDateTime(string DescriptorID)\n    {\n        if (GValues.TryGetValue(DescriptorID, out var ov))\n            try\n            {\n                return Convert.ToDateTime(ov);\n            }\n            catch (Exception ex)\n            {\n                if (ex.InnerException != null) ErrorLog.Add(ex.InnerException.ToString());\n            }\n        return default;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Thread Reporter used with IProgress<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[32,31,30],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/86"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}