how to setup a method getting args to be applied using String.Format(....)?
Example:
private void log(object text, params object[] param)
{
string line;
if (param != null && param.Length > 0)
line = String.Format(text.ToString(), param);
else
line = text.ToString();
if (Environment.UserInteractive==true)
Console.WriteLine(line);
}
Dienstag, 17. März 2020
text to enum value
classic request how to convert a text entry to an enum value? See..
Colors orange = (Colors) Enum.Parse(typeof(Colors), "Red, Yellow");
Colors orange = (Colors) Enum.Parse(typeof(Colors), "Red, Yellow");
Dienstag, 11. Dezember 2018
how to get the current assembly version
var assembly = Assembly.GetExecutingAssembly();
string version = assembly.GetName().Version.ToString();
how to avoid multiple executions of a program
Define 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.
prepare application
startup application
final exit of the application:
prepare application
public static System.Threading.Mutex mutex;
public static bool createdNewMutex;
startup application
mutex = new System.Threading.Mutex(true, "a2c204cc-50a1-4fde-b47f-5b712821edf3", out createdNewMutex);
if (!createdNewMutex) base.Shutdown(0);
final exit of the application:
if (App.createdNewMutex) App.mutex.ReleaseMutex();
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!
//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!
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:
or
Benefit from the best Windows Desktop app in the world and use Strokey.Net!
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!
Abonnieren
Posts (Atom)