Skip to content

Instantly share code, notes, and snippets.

View ctigeek's full-sized avatar

Steven Swenson ctigeek

View GitHub Profile
@ctigeek
ctigeek / Install-Chocolatey.ps1
Created August 27, 2015 14:50
Install chocolatey and other stuff...
Invoke-WebRequest https://chocolatey.org/install.ps1 -OutFile C:\Windows\Temp\chocoInstall.ps1
. C:\Windows\Temp\chocoInstall.ps1
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
choco install -y 7zip
choco install -y notepadplusplus
choco install -y google-chrome-x64
##choco install -y nodejs ##broken
choco install -y visualstudiocode
using System;
using System.IO;
using System.Text;
namespace StringBuilderStream
{
public class StringBuilderStream : Stream
{
public StringBuilder stringBuilder { get; private set; }
public Encoding Encoding { get; private set; }
@ctigeek
ctigeek / AesExample.cs
Created May 17, 2014 11:32
AesExample
static void Main(string[] args)
{
try
{
const int AesBlockSize = 128;
const CipherMode AesMode = CipherMode.CBC;
const int AesKeySize = 256;
byte[] IV = new byte[] {148, 22, 160, 30, 29, 229, 95, 124, 57, 3, 236, 225, 83, 164, 109, 89};
byte[] key = new byte[] {151, 171, 230, 106, 79, 228, 203, 65, 11, 229, 238, 157, 170, 26, 6, 75, 205, 122, 87, 11, 134, 161, 159, 17, 93, 140, 66, 222, 216, 158, 23, 73 };
private static void GetValueIObservable2()
{
var repo = new AccountRepository();
var accounts = repo.GetAccountWithSubs("123123123");
//create a cold observable from IEnumerable...
var accountObservable = accounts.ToObservable();
//you can filter an IObservable just like an IEnumerable...
//.Where(a => a.AccountNumber.ToString().StartsWith("2"));
@ctigeek
ctigeek / RunSynchronouslyInThreadPool
Last active August 29, 2015 14:07
Run an Async Task Synchronously in the thread pool. This will not copy any thread context.
private void button2_Click(object sender, EventArgs e)
{
try
{
//both methods do basically the same thing.
//I recommend the first since it's much simpler.
var body = RunSynchronouslyInAnotherTask(GetBody); // method 1
//var body = RunSynchronouslyInThreadPool(GetBody); // method 2
this.textBox1.Text = body;
@ctigeek
ctigeek / IAsyncObservable.cs
Last active August 29, 2015 14:08
AsyncObservable
// This works best for cold observables where you want to run Async methods after you subscribe.
// I may incorporate this into my QueryRunner....Not sure.
using System;
using System.Threading.Tasks;
namespace AsyncObservable
{
public interface IAsyncObserver<in T>
{
@ctigeek
ctigeek / EnumerableRepository.cs
Created October 23, 2014 13:14
Return IEnumerable from a database query....
//Note: this was pretty much an acedemic exercise... I don't recommend using this pattern. IObservable would be much better suited.
class SomeRepository : IDisposable
{
private const string sql = "select * from sometable;";
private const string connString = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=sandbox;Integrated Security=SSPI;";
public void Dispose()
{
if (reader != null) reader.Dispose();
if (connection != null) connection.Dispose();
@ctigeek
ctigeek / WebResponseException.cs
Created October 24, 2014 18:56
An exception that will encapsulate the details of a web call without embedding the specific response object. Using with RestClient.
//boy am I looking forward to read-only classes in c# ver6.
public class WebResponseException : Exception
{
public readonly HttpStatusCode StatusCode;
public readonly string RequestUrl;
public readonly string RequestMethod;
public readonly Dictionary<string, string> RequestHeaders;
public readonly Dictionary<string, string> ResponseHeaders;
public readonly string ResponseBody;
@ctigeek
ctigeek / Cache.cs
Last active August 29, 2015 14:08
Generic .net cache....
class Cache<T> where T : someBaseType
{
public readonly TimeSpan LifeSpan;
public readonly SemaphoreSlim semaphore;
private Dictionary<string, CacheItem<T>> itemDict;
public Cache(TimeSpan lifespan)
{
this.LifeSpan = lifespan;
@ctigeek
ctigeek / AlertInput.ino
Last active August 29, 2015 14:09
Sending an email from Arduino
const int sensorPin = A0; // the number of the pushbutton pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 150; // the debounce time; increase if the output flickers
int sensorValue = 0;
int alreadyAlerted = 0;
void setup() {
Serial.begin(9600);
Serial.println(F("Serial is working.... "));