Sunday, January 26, 2014
Saturday, January 25, 2014
C#: Safely set the CheckBox.Checked property of a control across threads.
/// <summary>
/// Safely set the <see cref="CheckBox.Checked" /> of the control across threads.
/// </summary>
/// <param name="control"></param>
/// <param name="value"></param>
public static void Checked( [CanBeNull] this CheckBox control, Boolean value ) {
if ( null == control ) {
return;
}
if ( control.InvokeRequired ) {
control.BeginInvoke( new Action( () => {
control.Checked = value;
control.Refresh();
} ) );
}
else {
control.Checked = value;
control.Refresh();
}
}
C#: Generate a random Decimal (also generate a random Int32)
public static Decimal NextDecimal() {
return new Decimal( new[] { NextInt32(), NextInt32(), NextInt32(), NextInt32() } );
}
public static int NextInt32() {
unchecked {
var firstBits = Instance.Next( 0, 1 << 4 ) << 28;
var lastBits = Instance.Next( 0, 1 << 28 );
return firstBits | lastBits;
}
}
/// <summary>
/// A thread-local (threadsafe) <see cref="Random" />.
/// </summary>
public static Random Instance { get { return ThreadSafeRandom.Value; } }
private static readonly ThreadLocal<SHA256Managed> Crypts = new ThreadLocal<SHA256Managed>( valueFactory: () => new SHA256Managed(), trackAllValues: false );
/// <summary>
/// Provide to each thread its own <see cref="Random" /> with a random seed.
/// </summary>
public static readonly ThreadLocal<Random> ThreadSafeRandom = new ThreadLocal<Random>( valueFactory: () => {
var hash = Crypts.Value.ComputeHash( Guid.NewGuid().ToByteArray() );
var newSeed = BitConverter.ToInt32( hash, 0 );
return new Random( newSeed );
}, trackAllValues: false );
Tuesday, January 21, 2014
FIX for MSB3247 warning message
you can get the level of verbosity you need to diagnose the issue by looking at the output window. To do this, follow these steps:
[1] Bring up the Tools/Options dialog (Tools->Options...).
[2] In the left-hand tree, select the Projects/Solutiosn node, and then select Build and Run. Note: if this node doesn't show up, make sure that the checkbox at the bottom of the dialog ("Show all settings") is checked.
[3] In the tools/options page that appears, select the MSBuild project build output verbosity level to "Diagnostic."
[4] Build the project and look in the output window.
MSBuild will have spewed out a lot of diagnostic messages. The ResolveAssemblyReferences task, which is the task from which MSB3247 originates, is probably our best task for logging diagnostic information -- those logging messages should help you debug this particular issue. Please reactivate the bug and let us know if they do not help.
Sincerely,
-- Izzy
Izzy Gryko
Lead Software Design Engineer
Microsoft Visual Studio Platform Development
izzyg@microsoft.com
[1] Bring up the Tools/Options dialog (Tools->Options...).
[2] In the left-hand tree, select the Projects/Solutiosn node, and then select Build and Run. Note: if this node doesn't show up, make sure that the checkbox at the bottom of the dialog ("Show all settings") is checked.
[3] In the tools/options page that appears, select the MSBuild project build output verbosity level to "Diagnostic."
[4] Build the project and look in the output window.
MSBuild will have spewed out a lot of diagnostic messages. The ResolveAssemblyReferences task, which is the task from which MSB3247 originates, is probably our best task for logging diagnostic information -- those logging messages should help you debug this particular issue. Please reactivate the bug and let us know if they do not help.
Sincerely,
-- Izzy
Izzy Gryko
Lead Software Design Engineer
Microsoft Visual Studio Platform Development
izzyg@microsoft.com
Monday, January 20, 2014
Programmers, Teach Non-Geeks The True Cost of Interruptions | DaedTech
"Interruptions are one of the biggest sources of inefficiency for programmers. "
Wednesday, January 15, 2014
Friday, January 10, 2014
C# Linq: Find Missing Values in a Sequence of Numbers and Other Sequence Related lists as IEnumerable Extensions « OmegaMan's Musings
DEC 202013
I recently had a need to determine if a sequence of integers was solid, not broken, and whether it contained a gap of any missing numbers to report to the end user. I researched the issue and was dismayed that the examples found on the internet seemed to have unnecessary overheads of copied arrays or other artifacts and were too cumbersome for my needs. From that I wrote these C# extensions which work for any .Net above 3.5. Below I document the extension methods to get the missing numbers of a sequence, quickly determine if a sequence is broken and finally report where the existing numbers before the break of the sequence. See the end for the whole extension class for easier copying.
Problem Definition
If one has a set of numbers say
{ 2, 4, 7, 9 }
it has a broken sequence because there are missing numbers from the set 2-9. Those missing numbers are:
{ 3, 5, 6, 8 }
Find Missing Numbers In a Sequence
This method is the one I created first. It uses the Linq Aggregate extension to enumerate over the numbers in the set. If there is a gap which is greater than 1 between the numbers (the difference below is > 0 but same concept) then it reports the numbers missing between the two.
public static IEnumerable< int > SequenceFindMissings( this IList< int > sequence) { var missing = new List< int >(); if ((sequence != null ) && (sequence.Any())) { sequence.Aggregate((seed, aggr) => { var diff = (aggr - seed) - 1; if (diff > 0) missing.AddRange(Enumerable.Range((aggr - diff), diff)); return aggr; }); } return missing; } |
Quickly Determine Broken Sequence
Is the sequence broken from the first number to the last in the set?
public static bool IsSequenceBroken( this IEnumerable< int > sequence) { bool broken = false ; if (sequence != null ) { var sequenceAsList = sequence.ToList(); if (sequenceAsList.Any()) { int lastValue = sequence.First(); broken = sequence.Any(value => { if ((value - lastValue) > 1) return true ; lastValue = value; return false ; }); } } return broken; } |
Report Last Valid Number Before The Break
This is useful in situations where one needs to report where the break happens, say the user is editing in a grid and one highlights the existing number which precedesthe missing number(s).
Example here returns a 2 and 5 which are the numbers which precede the break.
( new List() { 1, 2, 4, 5, 7, 8}).SequenceReportMissingsBreakStarts() |
Here is the method:
public static IEnumerable< int > SequenceReportMissingsBreakStarts( this IList< int > sequence) { var breaks = new List< int >(); if ((sequence != null ) && (sequence.Any())) { sequence.Aggregate((seed, aggr) => { var diff = (aggr - seed) - 1; if (diff > 0) breaks.Add(seed); return aggr; }); } return breaks; } |
Full Extension Source With Comments
Here is the code for an easier copy
public static class SequenceExtensions { /// /// Take a sequence of numbers and if there are any gaps greater than 1 between the numbers, /// report true. /// |
/// A set of numbers to check.
/// True if the there is a break in the sequence of numbers.
public
static
bool
IsSequenceBroken(
this
IEnumerable<
int
> sequence)
{
bool
broken =
false
;
if
(sequence !=
null
)
{
var sequenceAsList = sequence.ToList();
if
(sequenceAsList.Any())
{
int
lastValue = sequence.First();
broken = sequence.Any(value =>
{
if
((value - lastValue) > 1)
return
true
;
lastValue = value;
return
false
;
});
}
}
return
broken;
}
///
/// Take a sequence of numbers and report the missing numbers. Stop at first break found.
///
/// Set of Numbers
/// True of sequence has missing numbers
public
static
IEnumerable<
int
> SequenceFindMissings(
this
IList<
int
> sequence)
{
var missing =
new
List<
int
>();
if
((sequence !=
null
) && (sequence.Any()))
{
sequence.Aggregate((seed, aggr) =>
{
var diff = (aggr - seed) - 1;
if
(diff > 0)
missing.AddRange(Enumerable.Range((aggr - diff), diff));
return
aggr;
});
}
return
missing;
}
///
/// A missing break start in a sequence is where the drop off occurs in the sequence.
/// For example 3, 5, has a missing break start of the #3 for #4 is the missing.
///
/// Set of Numbers
/// The list of break numbers which exist before the missing numbers.
public
static
IEnumerable<
int
> SequenceReportMissingsBreakStarts(
this
IList<
int
> sequence)
{
var breaks =
new
List<
int
>();
if
((sequence !=
null
) && (sequence.Any()))
{
sequence.Aggregate((seed, aggr) =>
{
var diff = (aggr - seed) - 1;
if
(diff > 0)
breaks.Add(seed);
return
aggr;
});
}
return
breaks;
}
}
Hope this helps!
Subscribe to:
Posts (Atom)