tag:blogger.com,1999:blog-28096034599685353152024-03-12T17:27:37.935-07:00C#VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-2809603459968535315.post-18643408730258736122021-09-25T12:07:00.005-07:002021-09-25T23:59:42.472-07:00BASTA'21 impressions - not only c#10 features...<p></p><h4 style="text-align: left;"><div style="text-align: left;"> <br /><span style="font-weight: normal;"><span style="font-family: courier;">https://sharplab.io/</span> </span></div><div style="text-align: left;"><span style="font-weight: normal;">-> test c# code on the fly online</span></div><div style="text-align: left;"><span style="font-weight: normal;"><br /></span></div><div style="text-align: left;"><span style="font-weight: normal;"><br /><span style="font-family: courier;">https://source.dot.net/<br /></span>-> observe the source code of dot net. Deep dive into the source of dot net.</span></div><div style="text-align: left;"><span style="font-weight: normal;"><br /></span></div></h4><h4 style="text-align: left;"><div style="text-align: left;"><span style="font-weight: normal;"><span style="font-family: courier;">gith</span></span><span style="font-family: courier; font-weight: normal;">ub.com</span></div><div style="text-align: left;"><span style="font-weight: normal;"><span style="font-family: courier;">githubs1.com <br /></span><span style="font-family: courier;">-> for alternative layout of github.com; you can also push . when browsing in github.com</span></span></div><span style="font-weight: normal;"><span style="font-family: courier;"><div style="text-align: left;"><br /></div><div style="text-align: left;"><br /></div></span></span><div style="text-align: left;"><span style="font-weight: normal;">https://devblogs.microsoft.com/dotnet/<br /></span><span style="font-weight: normal;">-> first class microsoft blogs of ms team leaders, managers, high ranking employees</span></div><div style="text-align: left;"><span style="font-weight: normal;"><br /></span></div><div style="text-align: left;"><span style="font-weight: normal;"><br /></span></div><span style="font-weight: normal;"><span style="font-family: courier;">record struct Vector2d(double X, double Y)<br />{<br /> public static Vector2d operator +(Vector2d first, Vector2d second) =><br /><span> </span><span> </span>new Vector2d(first.X + second.X, first.Y + second.Y);<br />}<br /></span>-> like normal record but value typed (variable) and not referenced typed (pointer)<br />-> see <span> </span><span> </span>https://anthonygiretti.com/2021/08/03/introducing-c-10-record-struct/</span></h4><div style="text-align: left;"><br />readonly record struct<br />-> of course - readonly - makes record struct immutable</div><div style="text-align: left;"><br /></div><p></p><div style="text-align: left;">public string FirstName { get; init; } = "Neo";</div>-> c# class property set init only<br />-> read https://www.thomasclaudiushuber.com/2020/08/25/c-9-0-init-only-properties/<p></p><div><p></p><p><span style="font-family: courier;">with <br /></span>var v2 = v1 with { X = 4d }; // clone v1 and override double property X with new value of 4d <br />-> clones the source to modify it partially<br />-> see : https://docs.microsoft.com/de-de/dotnet/csharp/language-reference/operators/with-expression<br /><br /></p><div style="text-align: left;">Remember<br />4d -> 4.0 double value<br />4m -> 4.0 decimal value</div><div style="text-align: left;">4f -> 4.0 float value</div><div style="text-align: left;"><br /></div><div style="text-align: left;"><div><span style="font-family: courier;">nameof(myObject)</span></div><div>-> get the name description of the type</div><div><br /></div><div><span style="font-family: courier;">is</span></div><div>-> var x = "my text" is String;</div><div>-> predicate operation (true/false result) if object is of type ANY. Here is x true finally.</div><div><br /></div></div><div style="text-align: left;"><div style="text-align: left;"><span style="font-family: courier;">stackalloc</span></div><div style="text-align: left;">-> value types are stack allocates and not pushed to the heap</div><div style="text-align: left;">-> see https://docs.microsoft.com/de-de/dotnet/csharp/language-reference/operators/stackalloc</div></div><p></p><div style="text-align: left;"><div><br /><span style="font-family: courier;">Span<Vector2d> vectors = stackalloc Vector2d[]<br />{<br /> new Vector2d(3d, 4d),</span></div><div><span style="font-family: courier;"><span> </span>new Vector2d(1d, 2d)<br />};</span><br />-> see https://docs.microsoft.com/de-de/dotnet/api/system.span-1?view=net-5.0</div></div><div style="text-align: left;"><br /></div><div style="text-align: left;">and even more....</div><p style="text-align: left;"><br /><br /><br /><br /><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p></div>VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-72957909616797067122020-04-20T23:13:00.001-07:002020-04-20T23:13:12.549-07:00MS SQL Merge upsertmultiple record writing - steps to achieve the fastest update of an table:<div>
<br /></div>
<div>
- create temp database table. This is a copy of the destination table to fill for finally.</div>
<div>
- fille the temp database table with the bulk copy function of System.data.sqlClient.</div>
<div>
- execute the MERGE script.</div>
<div>
<br /></div>
<div>
MERGE example:</div>
<div>
<br /></div>
<div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">MERGE [dbo].[THE_TABLE] as t</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">USING [dbo].[TEMP_THE_TABLE] as s</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">ON t.MY_ID=s.MY_ID AND t.MY_SEQUENCE=s.MY_SEQUENCE</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">WHEN MATCHED THEN </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="white-space: pre;"> </span>UPDATE SET</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> t.MY_VALUE=s.MY_VALUE,</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> t.MY_TIMESTAMP=s.MY_TIMESTAMP</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">WHEN NOT MATCHED THEN </span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="white-space: pre;"> </span>INSERT (MY_ID, MY_SEQUENCE, MY_VALUE, MY_TIMESTAMP)</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><span style="white-space: pre;"> </span>VALUES (s.MY_ID, s.MY_SEQUENCE, s.MY_VALUE, s.MY_TIMESTAMP);</span></div>
</div>
<div>
<br /></div>
<div>
(t=target, s=source)</div>
VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-77698471070615225132020-04-07T13:21:00.005-07:002021-09-25T23:06:44.913-07:00Linq : short basic samples<div class="tr_bq">
<b>Linq - basic how to samples:</b></div>
<br />
<br />using System.Linq;<div>
<br /></div>
<div>
...</div>
<div>
<br />[System.Serializable]<br />public class Item<br />{<br /> public string Name;<br /> public int ItemID;<br /> public int Buff;<br />}</div>
<div>
<br /></div>
<div>
<br />public class Main<br />{</div>
<div>
<br /> public List<Item> Items;<br /></div>
<div>
public void Start()<br />{</div>
<div>
<br /></div>
<div>
string[] names = { "ralf", "Peter", "Thomas", "Christian", "Patrice" };<br /></div>
<div>
<b>/* ANY demo */</b><br /> var nameFound = names.Any(name => name == "Peter");<br /> Debug.Log("Found name : " + nameFound);<br /></div>
<div>
<b>/* Contains */</b><br /> var nameContained = names.Contains("Christian");<br /> Debug.Log("Found conatined : " + nameContained);<br /></div>
<div>
<b>/* Distinct - remove duplicates*/</b><br /> var uniqueNames = names.Distinct();<br /></div>
<div>
<b>/* Where */</b><br /> var result = names.Where(n => n.Length > 5);<br /></div>
<div>
<b>/* select greater than results */</b><br /> int[] grades = { 10, 20, 30, 40, 50, 60, 65, 70, 75, 33, 80, 13, 90, 100 };<br /> var highGrades = grades.Where(i => i > 65);<br /> foreach (int grade in highGrades)<br /> {<br /> Debug.Log("selected grade : " + grade.ToString());<br /> }<br /></div>
<div>
<b>/* select ORDER BY .. DESCENDING */</b><br /> var descGrades = grades.OrderByDescending(i => i);<br /> foreach (var item in descGrades)<br /> {<br /> Debug.Log("Int item = " + item);<br /> }<br /></div>
<div>
<b>/* precondition before moving on - image the Items collection including 5x Item with various values */</b><br /></div>
<div>
<b>/* check if ItemID 3 exists in the List of Item */</b><br /> var containsId3 = Items.Where(i => i.ItemID == 3);<br /> var itemId3found = Items.Any(i=>i.ItemID == 3);<br /> Debug.Log("is Items including ItemId 3 : " + itemId3found);<br /></div>
<div>
<b>/* select all items with buff greater then 60 and print them out */</b><br /> var itemsGT60 = Items.Where(i => i.Buff > 60);<br /> foreach (var buff in itemsGT60)<br /> {<br /> Debug.Log("buff Items > 60 : " + buff);<br /> }<br /></div>
<div>
<b>/* calculate the average of all the buff stats */</b><br /> var resultAverage = Items.Average(i => i.Buff);<br /> Debug.Log("Average : " + resultAverage);<br /> }</div><div>}</div><div><br /></div><div>...</div><div><br /></div><div><b>// find the entry in an array of objects where each object got the property 'Name'</b></div><div><br /></div><div>using System;</div><div>string searchForName = "Patrice"</div><div>var foundItem = Array.Find(CollectionOfItems, x=> x.Name==searchForName);</div><div><br /></div>
VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-2560433176819363582020-03-18T09:58:00.003-07:002020-04-20T23:04:17.114-07:00json - newtowsoft - JArray - SelectToken<b>how to fill JArray and select all and specific items? </b><br />
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">private Newtonsoft.Json.Linq.JArray UFOS = new jsonlib.Linq.JArray();</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(myUFOjsonSourceString);</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">var list = json.list; // List of UFO items - 'list' is the root element of the source JSON structure</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><br /></span>
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">foreach (var item in list)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">{</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">..</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">UFOS.Add(item);</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">...</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">}</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">IEnumerable<jsonlib.Linq.JToken> allUFOS= UFOS.SelectToken("$"); // okay, all UFOs</span><br />
<br />
IEnumerable<jsonlib.Linq.JToken> oneUFO = UFOS.SelectToken("$[?(@.Pilot=='E.T.')]"); // okay, if each item of UFO would contain the property 'Pilot', select the one where Pilot=E.T. (you know, this small odd alien who wants to get home again)<br />
<br />
<b>Sample II:</b><br />
<b><br /></b>
<b>Parse XML using Newtonsoft.Json</b><br />
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><root></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <light></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <color>red</color></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <intensity>bright</intensity></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> </light></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <light></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <color>green</color></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> <intensity>light</intensity></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> </light></span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"></root></span><br />
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">XDocument doc = XDocument.Parse(mySampleXmlString); //or XDocument.Load(path)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">string jsonText = JsonConvert.SerializeXNode(doc);</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">dynamic pseudoXml = JsonConvert.DeserializeObject(jsonText);</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">Newtonsoft.Json.Linq.JArray lights = (Newtonsoft.Json.Linq.JArray)pseudoXml["root"]["light"];</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">foreach (var light in lights)</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">{</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> string color = (string)light.color;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> string intensity = (string)light.intensity;</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">}</span><br />
<br />VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-47274246399616651812020-03-17T07:02:00.001-07:002020-03-17T07:02:24.834-07:00Pass string parameter plus optional placeholder fill up arguments to a method - method signature for String.Format(..)how to setup a method getting args to be applied using String.Format(....)?<br />
<br />
Example:<br />
<br />
private void log(object text, params object[] param)<br />
{<br />
string line;<br />
if (param != null && param.Length > 0)<br />
line = String.Format(text.ToString(), param);<br />
else<br />
line = text.ToString();<br />
if (Environment.UserInteractive==true)<br />
Console.WriteLine(line);<br />
}<br />
<br />VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-73817375309372174872020-03-17T06:05:00.001-07:002020-03-17T06:05:00.373-07:00text to enum valueclassic request how to convert a text entry to an enum value? See..<br />
<br />
<span style="background-color: #fafafa; color: #171717; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;">Colors orange = (Colors) Enum.Parse(</span><span class="hljs-keyword" style="background-color: #fafafa; box-sizing: inherit; color: #0101fd; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;">typeof</span><span style="background-color: #fafafa; color: #171717; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;">(Colors), </span><span class="hljs-string" style="background-color: #fafafa; box-sizing: inherit; color: #a31515; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;">"Red, Yellow"</span><span style="background-color: #fafafa; color: #171717; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;">);</span><br />
<span style="background-color: #fafafa; color: #171717; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; white-space: pre;"><br /></span>VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-57429909700496719892018-12-11T01:35:00.003-08:002018-12-11T01:35:53.587-08:00how to get the current assembly version<blockquote class="tr_bq">
var assembly = Assembly.GetExecutingAssembly();<br />
string version = assembly.GetName().Version.ToString();</blockquote>
<br />VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-79560964130837044462018-12-11T00:23:00.001-08:002018-12-11T00:45:54.862-08:00how to avoid multiple executions of a programDefine any unique ID / UID like 'a2c204cc-50a1-4fde-b47f-5b712821edf3' . If this UID isn't free, abort the application. Otherwise block it with the current process.<br />
<br />
prepare application<br />
<br />
<blockquote class="tr_bq">
public static System.Threading.Mutex mutex;<br />
public static bool createdNewMutex;</blockquote>
<br />
startup application<br />
<br />
<blockquote class="tr_bq">
mutex = new System.Threading.Mutex(true, "a2c204cc-50a1-4fde-b47f-5b712821edf3", out createdNewMutex);<br />
if (!createdNewMutex) base.Shutdown(0);</blockquote>
<br />
final exit of the application:<br />
<br />
<blockquote class="tr_bq">
if (App.createdNewMutex) App.mutex.ReleaseMutex();</blockquote>
VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-52699638109465857522016-08-28T10:04:00.003-07:002016-08-28T10:05:58.668-07:00String to Double value conversionIn different cultures differs the decimal delimiter. Therfore you could profit by such an extension method like this one. So it doesn't matter if you are using a english or german client, when converting for instance "4711.10" or "4711,10" to a double value.<br />
<br />
//english/Invariant system . = decimal delimiter<br />
//DE, NL system , = decimal delimiter<br />
public static double ToDoubleEx<T>(this T obj)<br />
{<br />
if (obj == null) return 0;<br />
double result = 0;<br />
string v = Convert.ToString(obj);<br />
if (v != null && v.Length > 0)<br />
{<br />
var point = v.IndexOf('.');<br />
var komma = v.IndexOf(',');<br />
if (point != -1 && komma != -1)<br />
{<br />
if (komma > point)<br />
v = v.Replace(".", "");<br />
else<br />
v = v.Replace(",", "");<br />
}<br />
v = v.Replace(",", ".");<br />
result = Double.Parse(v, System.Globalization.CultureInfo.InvariantCulture); //Invariant behavior like english default behavior<br />
}<br />
return result;<br />
}<br />
<br />
<i><span style="font-size: xx-small;">Benefit from the best Windows Desktop app in the world and use </span><a href="http://www.strokey.net/"><span style="font-size: xx-small;">Strokey.Net</span></a><span style="font-size: xx-small;">!</span></i><br />
<div>
<i><span style="font-size: xx-small;"><br /></span></i></div>
<div>
<i><span style="font-size: xx-small;"><br /></span></i></div>
VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-85192492685483485972014-11-30T10:25:00.002-08:002016-08-24T12:52:23.116-07:00How to cancel a parallel task<span style="font-family: "arial" , "helvetica" , sans-serif;">a) THREAD A: The method for the starting of the parallel task instantiates a new <b>System.Threading.CancellationTokenSource</b> Object<br /></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;">b) THREAD A: This <b>CancellationTokenSource </b>is given to the parallel task</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">c) THREAD B: The underlying method of the parallel task includes the type of <b>CancellationTokenSource</b> in its signature to take it over.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">If a "cancel" button was pushed in THREAD A, then <b>CancellationTokens </b>method <b>Cancel </b>must be raised. This is done like this:</span><br />
<b><span style="font-family: "courier new" , "courier" , monospace;">CancellationTokenSource.Token.Cancel();</span></b><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The routine inside THREAD B (expecting while...wend loop for instance) is checking the <b>CancellationTokenSource.Token.<span style="font-size: 13px; line-height: 17.5499992370605px;">IsCancellationRequested = true</span></b></span><br />
<span style="font-size: 13px; line-height: 17.5499992370605px;"><span style="font-family: "arial" , "helvetica" , sans-serif;">state and aborts its work in this case.</span></span><br />
<span style="font-size: 13px; line-height: 17.5499992370605px;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></span>
<span style="font-size: 13px; line-height: 17.5499992370605px;"><span style="font-family: "arial" , "helvetica" , sans-serif;">done!</span></span><br />
<span style="font-size: 13px; line-height: 17.5499992370605px;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></span>
<span style="font-size: 13px; line-height: 17.5499992370605px;"><span style="font-family: "arial" , "helvetica" , sans-serif;">based information : <a href="http://msdn.microsoft.com/de-de/library/dd997364%28v=vs.110%29.aspx">http://msdn.microsoft.com/de-de/library/dd997364%28v=vs.110%29.aspx</a></span></span><br />
<br />
<i><span style="font-size: xx-small;">Benefit from the best Windows Desktop app in the world and use </span><a href="http://www.strokey.net/"><span style="font-size: xx-small;">Strokey.Net</span></a><span style="font-size: xx-small;">!</span></i><br />
<i><span style="font-size: xx-small;"><br /></span></i>VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-8823440610697367862014-02-08T05:40:00.003-08:002014-11-30T10:25:56.777-08:00shorter term of exception handling with the help of the Ternary operatorFor example:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #999999;">catch (System.ServiceModel.FaultException<AnyContract.ValidationError> ex)</span><br />
<span style="color: #999999;"> {</span><br />
<span style="color: #999999;"> Console.WriteLine(</span>ex.Detail != null ? ex.Detail.Message : ex.InnerException != null ? ex.InnerException.Message : ex.Message<span style="color: #999999;">);</span><br />
<span style="color: #999999;"> }</span></blockquote>
<br />
or<br />
<br />
<blockquote class="tr_bq">
<span style="color: #999999;">catch (Exception ex)</span><br />
<span style="color: #999999;"> {</span><br />
<span style="color: #999999;"> Console.WriteLine(</span>ex.InnerException != null ? ex.InnerException.Message : ex.Message<span style="color: #999999;">);</span><br />
<span style="color: #999999;"> } </span></blockquote>
<br />
<i><span style="font-size: xx-small;">Benefit from the best Windows Desktop app in the world and use </span><a href="http://www.strokey.net/"><span style="font-size: xx-small;">Strokey.Net</span></a><span style="font-size: xx-small;">!</span></i> VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-82062704966681858472013-10-11T13:56:00.003-07:002016-08-24T12:55:05.036-07:00How to capture a mouseclick system-wide using System.Runtime.InteropServicesIf you need a short sample to handle this rare issue, consider the following steps...<br />
<br />
* create a new WPF Application<br />
* add a reference to System.Runtime.InteropServices<br />
* add a public static class called "hook.cs"<br />
* in App.xaml.cs add a static variable to buffer the hook result code<br />
<blockquote class="tr_bq">
public static int hHook =0 ;</blockquote>
<br />
* in hook.cs add the namespace System.Runtime.InteropServices<br />
* in hook cs add a static public method called Register() and save the hook value in the hHook of App.xaml.cs<br />
* in hook.cs add a static public method called Unregister()<br />
* in hook.cs add a static method as the MouseProcedure to handle the global messages<br />
* in hook.cs add 2x Imports to register, unregister the hook and add 1x Import to pass the hook-Event. (DllImport user32.dll)<br />
* in hook.cs -> MouseProcedure reacts on the mouseclick signal WM_LBUTTONDOWN<br />
* register the hook in the startup of the application through calling the static method register() of the class in the hook.cs<br />
* by leaving the application unregister such hook again through the appropriate method in hook.cs<br />
<br />
download the short sample project <a href="http://www.vanden.de/downloads/WpfApplication1_GlobalClick.zip">here</a> and check it in detail!<br />
<br />
(based on <a href="http://www.codeproject.com/Articles/17969/Mouse-Operations">http://www.codeproject.com/Articles/17969/Mouse-Operations</a>)<br />
<br />
<em><span style="font-size: x-small;">Benefit from the best Windows Desktop app in the world and use </span><a href="http://www.strokey.net/"><span style="font-size: x-small;">Strokey.Net</span></a><span style="font-size: x-small;">!</span></em><br />
<br />VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0tag:blogger.com,1999:blog-2809603459968535315.post-38825274226996687332013-08-20T12:37:00.001-07:002013-10-24T12:55:14.127-07:00variant how to read a text-stream blockwise using StreamReader ReadBlock<br />
<br />
<span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">string Textfile = @"C:\hello.txt";</span></span></span><br />
<span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">Encoding</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> enc = </span></span><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">Encoding</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">.Default;</span></span><br />
<span style="font-family: Consolas; font-size: x-small;"></span><br />
<span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">FileInfo</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> fi = </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">new</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">FileInfo</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">(textfile);</span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">long</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> remainingTillEnd = fi.Length;</span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
</span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">int</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> readTotalUntilNow = 0;</span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">int</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> blocksize = 4096;</span></span><br />
<span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">char</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">[] buffer = </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">new</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">char[blocksize];</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span><br />
</span><br />
<blockquote class="tr_bq">
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">using</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> (</span></span><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">StreamReader</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> sr = </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">new</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;"><span style="color: #2b91af; font-family: Consolas; font-size: x-small;">StreamReader(Textfile,</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">enc))</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
{<br />
</span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: black;"> </span>bool</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> readon = </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">true</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">;</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
</span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">while</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> (readon)</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
{<br />
</span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">if</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> (remainingTillEnd > blocksize)</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
{<br />
readTotalUntilNow += sr.ReadBlock(buffer, 0, blocksize);<br />
}<br />
</span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">else</span></span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">
</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> {<br />
</span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> readTotalUntilNow += sr.ReadBlock(buffer, 0, (</span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">int</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">)remainingTillEnd);</span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">
</span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> readon = </span></span><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;"><span style="color: blue; font-family: Consolas; font-size: x-small;">false</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;">; </span></span><span style="color: green; font-family: Consolas; font-size: x-small;"><span style="color: green; font-family: Consolas; font-size: x-small;"><span style="color: green; font-family: Consolas; font-size: x-small;">// last block indicator </span></span></span></span><br />
<span style="font-family: Consolas; font-size: x-small;"><span style="color: green; font-family: Consolas; font-size: x-small;"><span style="color: green; font-family: Consolas; font-size: x-small;"><span style="color: green; font-family: Consolas; font-size: x-small;">
</span></span></span><span style="font-family: Consolas; font-size: x-small;"><span style="font-family: Consolas; font-size: x-small;"> }<br />
remainingTillEnd = fi.Length - readTotalUntilNow;<br />
}<br />
}</span></span> </span></blockquote>
<br />
<span style="font-family: Consolas; font-size: x-small;">
<br />
<em><span style="font-size: x-small;">Benefit from the best Windows Desktop app in the world and use </span><a href="http://www.strokey.net/"><span style="font-size: x-small;">Strokey.Net</span></a><span style="font-size: x-small;">!</span></em></span>VANDENhttp://www.blogger.com/profile/01984349297850792175noreply@blogger.com0