Skip to content

Instantly share code, notes, and snippets.

View wjkhappy14's full-sized avatar
🎯
Focusing

AngkorW wjkhappy14

🎯
Focusing
View GitHub Profile
@wjkhappy14
wjkhappy14 / nginx-restart.bat
Created May 8, 2019 07:23
Quick startup for NGINX and PHP on Windows
@ECHO OFF
call nginx-stop.bat
call nginx-start.bat
EXIT /b
@wjkhappy14
wjkhappy14 / gist:7efc10385b9612b2a4cb62378a265dbd
Created September 6, 2018 04:30 — forked from leggetter/gist:769688
How to get the body of a HTTP Request using C#
private string GetDocumentContents(System.Web.HttpRequestBase Request)
{
string documentContents;
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
@wjkhappy14
wjkhappy14 / Singleton
Created July 26, 2018 03:48
Lazy Singleton
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton()
{
}
@wjkhappy14
wjkhappy14 / keyboardlistener.cs
Created July 26, 2018 03:42 — forked from Ciantic/keyboardlistener.cs
C# Keyboard listener
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Windows.Threading;
using System.Collections.Generic;
namespace Ownskit.Utils
{
/*
https://connect.microsoft.com/SQLServer/feedback/details/683411
*/
IF NOT EXISTS ( SELECT TOP 1 NULL
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'SinglePrimaryKey' )
BEGIN
CREATE TABLE SinglePrimaryKey
(
@wjkhappy14
wjkhappy14 / XE_Dynamic_Shred__LongRunningQueries
Created March 27, 2017 06:27 — forked from mbourgon/XE_Dynamic_Shred__LongRunningQueries
Using Extended Events to find long-running queries, with a dynamic XML shred and PIVOT to simplify the code
--written by MDB and ALM for TheBakingDBA.Blogspot.Com
-- basic XE session creation written by Pinal Dave
-- http://blog.sqlauthority.com/2010/03/29/sql-server-introduction-to-extended-events-finding-long-running-queries/
-- mdb 2015/03/13 1.1 - added a query to the ring buffer's header to get # of events run, more comments
-- mdb 2015/03/13 1.2 - added model_end events, filtering on hostname, using TRACK_CAUSALITY, and multiple events
-- mdb 2015/03/18 1.3 - changed header parse to dynamic, courtesy of Mikael Eriksson on StackOverflow
-- This runs on at 2008++ (tested on 2008, 2008R2, 2012, and 2014). Because of that, no NOT LIKE exclusion
------------------------------
-- Create the Event Session --
------------------------------
@wjkhappy14
wjkhappy14 / RxJS 5 Operators By Example.md
Created December 12, 2016 03:10 — forked from lyyourc/RxJS 5 Operators By Example.md
「译」RxJS 5 Operators By Example
@wjkhappy14
wjkhappy14 / rxjs_operators_by_example.md
Created December 12, 2016 03:10 — forked from btroncone/rxjs_operators_by_example.md
RxJS 5 Operators By Example
不要使用 Thread.Abort 终止其他线程。 对另一个线程调用 Abort 无异于引发该线程的异常,也不知道该线程已处理到哪个位置。
不要使用 Thread.Suspend 和 Thread.Resume 同步多个线程的活动。 请使用 Mutex、ManualResetEvent、AutoResetEvent 和 Monitor。
不要从主程序中控制辅助线程的执行(如使用事件), 而应在设计程序时让辅助线程负责等待任务,执行任务,并在完成时通知程序的其他部分。 如果不阻止辅助线程,请考虑使用线程池线程。 如果阻止辅助线程,Monitor.PulseAll 会很有帮助。
不要将类型用作锁定对象。 例如,避免在 C# 中使用 lock(typeof(X)) 代码,或在 Visual Basic 中使用 SyncLock(GetType(X)) 代码,或将 Monitor.Enter 和 Type 对象一起使用。 对于给定类型,每个应用程序域只有一个 System.Type 实例。 如果您锁定的对象的类型是 public,您的代码之外的代码也可锁定它,但会导致死锁。 有关其他信息,请参见可靠性最佳做法。
锁定实例时要谨慎,例如,C# 中的 lock(this) 或 Visual Basic 中的 SyncLock(Me)。 如果您的应用程序中不属于该类型的其他代码锁定了该对象,则会发生死锁。
一定要确保已进入监视器的线程始终离开该监视器,即使当线程在监视器中时发生异常也是如此。 C# 的 lock 语句和 Visual Basic 的 SyncLock 语句可自动提供此行为,它们用一个 finally 块来确保调用 Monitor.Exit。 如果无法确保调用 Exit,请考虑将您的设计更改为使用 Mutex。 Mutex 在当前拥有它的线程终止后会自动释放。
一定要针对那些需要不同资源的任务使用多线程,避免向单个资源指定多个线程。 例如,任何涉及 I/O 的任务都会从其拥有其自己的线程这一点得到好处,因为此线程在 I/O 操作期间将阻止,从而允许其他线程执行。 用户输入是另一种可从专用线程获益的资源。 在单处理器计算机上,涉及大量计算的任务可与用户输入和涉及 I/O 的任务并存,但多个计算量大的任务将相互竞争。
对于简单的状态更改,请考虑使用 Interlocked 类的方法,而不是 lock 语句(在 Vis
public static void InsertionSort(int[] array, int from, int to)
{
for (int i = from + 1; i < to; i++)
{
var a = array[i];
int j = i - 1;
while (j >= from && array[j] > a)
{
array[j + 1] = array[j];
j--;