Sonntag, 28. August 2016

String to Double value conversion

In 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.

        //english/Invariant system . = decimal delimiter
        //DE, NL system            , = decimal delimiter
        public static double ToDoubleEx<T>(this T obj)
        {
            if (obj == null) return 0;
            double result = 0;
            string v = Convert.ToString(obj);
            if (v != null && v.Length > 0)
            {
                var point = v.IndexOf('.');
                var komma = v.IndexOf(',');
                if (point != -1 && komma != -1)
                {
                    if (komma > point)
                        v = v.Replace(".", "");
                    else
                        v = v.Replace(",", "");
                }
                v = v.Replace(",", ".");
                result = Double.Parse(v, System.Globalization.CultureInfo.InvariantCulture); //Invariant behavior like english default behavior
            }
            return result;
        }

Benefit from the best Windows Desktop app in the world and use Strokey.Net!


Sonntag, 30. November 2014

How to cancel a parallel task

a) THREAD A:  The method for the starting of the parallel task instantiates a new System.Threading.CancellationTokenSource Object

b) THREAD A: This CancellationTokenSource is given to the parallel task

c) THREAD B: The underlying method of the parallel task includes the type of CancellationTokenSource in its signature to take it over.


If a "cancel" button was pushed in THREAD A, then CancellationTokens method Cancel must be raised. This is done like this:
CancellationTokenSource.Token.Cancel();

The routine inside THREAD B (expecting while...wend loop for instance) is checking the CancellationTokenSource.Token.IsCancellationRequested = true
state and aborts its work in this case.

done!

based information : http://msdn.microsoft.com/de-de/library/dd997364%28v=vs.110%29.aspx

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Samstag, 8. Februar 2014

shorter term of exception handling with the help of the Ternary operator

For example:

catch (System.ServiceModel.FaultException<AnyContract.ValidationError> ex)
            {
                Console.WriteLine(ex.Detail != null ? ex.Detail.Message : ex.InnerException != null ? ex.InnerException.Message : ex.Message);
            }

or

catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException != null ? ex.InnerException.Message : ex.Message);
            }

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Freitag, 11. Oktober 2013

How to capture a mouseclick system-wide using System.Runtime.InteropServices

If you need a short sample to handle this rare issue, consider the following steps...

* create a new WPF Application
* add a reference to System.Runtime.InteropServices
* add a public static class called "hook.cs"
* in App.xaml.cs add a static variable to buffer the hook result code
public static int hHook =0 ;

* in hook.cs add the namespace System.Runtime.InteropServices
* in hook cs add a static public method called Register() and save the hook value in the hHook of App.xaml.cs
* in hook.cs add a static public method called Unregister()
* in hook.cs add a static method as the MouseProcedure to handle the global messages
* in hook.cs add 2x Imports to register, unregister the hook and add 1x Import to pass the hook-Event. (DllImport user32.dll)
* in hook.cs -> MouseProcedure reacts on the mouseclick signal WM_LBUTTONDOWN
* register the hook in the startup of the application through calling the static method register() of the class in the hook.cs
* by leaving the application unregister such hook again through the appropriate method in hook.cs

download the short sample project here and check it in detail!

(based on http://www.codeproject.com/Articles/17969/Mouse-Operations)

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Dienstag, 20. August 2013

variant how to read a text-stream blockwise using StreamReader ReadBlock



string Textfile = @"C:\hello.txt";
Encoding enc = Encoding.Default;

FileInfo fi = new FileInfo(textfile);long remainingTillEnd = fi.Length;

int readTotalUntilNow = 0;int blocksize = 4096;
char[] buffer = new char[blocksize];
 

using (StreamReader sr = new StreamReader(Textfile,enc))
{
bool readon = true;

while (readon)
{
if (remainingTillEnd > blocksize)

{
readTotalUntilNow += sr.ReadBlock(buffer, 0, blocksize);
}
else

{
readTotalUntilNow += sr.ReadBlock(buffer, 0, (int)remainingTillEnd);

readon = false; // last block indicator
}
remainingTillEnd = fi.Length - readTotalUntilNow;
}
}
 


Benefit from the best Windows Desktop app in the world and use Strokey.Net!