Skip to content

Instantly share code, notes, and snippets.

@huanlin
huanlin / gist:5428200
Last active December 16, 2015 11:29
Download web contents asynchronously using Thread class.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Ex01_Console
{
class ThreadDemo
{
public static void Download(string[] urls)
using System;
using System.Threading;
class Program
{
static void Main(strin g[] args)
{
Thread t1 = new Thread(MyBackgroundTask);
t1.Start();
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(MyBackgroundTask);
Thread t2 = new Thread(MyBackgroundTask);
Thread t3 = new Thread(MyBackgroundTask);
static void Main(string[] args)
{
Thread t1 = new Thread(MyBackgroundTask);
Thread t2 = new Thread(MyBackgroundTask);
Thread t3 = new Thread(MyBackgroundTask);
t1.Start("X");
t2.Start("Y");
t3.Start("Z");
class Program
{
static void Main(string[] args)
{
new SharedStateDemo().Run();
Console.ReadLine();
}
}
public class SharedStateDemo
{
private int itemCount = 0;
private object locker = new Object(); // 用於獨佔鎖定的物件
public void Run()
{
var t1 = new Thread(AddToCart);
var t2 = new Thread(AddToCart);
using System;
using System.Threading;
namespace Ex05_BackgroundThread
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(MyWork);
@huanlin
huanlin / Example.cs
Last active January 3, 2024 15:42
Helper classes for change printer settings with P/Invoke.
void GetDefaultPaperSize()
{
var devMode = PInvoke.PrinterHelper.GetPrinterDevMode(null);
string s = String.Format("{0} : {1} x {2}", devMode.dmPaperSize, devMode.dmPaperWidth, devMode.dmPaperLength);
Console.WriteLine(s);
}
void SetDefaultPaperSize()
{
string formName = "User defined";
@huanlin
huanlin / ReadWriteBlob.cs
Last active December 19, 2015 13:38
Read and Write BLOB fields using ADO.NET
private byte[] ReadBlob(int id)
{
byte[] result = null;
var conn = new SqlConnection("Your connection string");
conn.Open();
string sql = String.Format("select Photo from MyTable where ID={0}", id);
var cmd = conn.CreateCommand();
cmd.CommandText = sql;
var reader = cmd.ExecuteReader();
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
namespace HuanlinLib.Drawing
{
public enum ExifOrientations