Posts
[C#]平行處理網路傳輸時因連線數不足發生連線Timeout的解決方案
namespace ConsoleApplication15 { class Program { public static string url = “https://www.google.com/images/srpr/logo3w.png"; private static List<WaitHandle> finished = new List<WaitHandle>(); public static void Main() { ServicePointManager.DefaultConnectionLimit = 200;
finished.Clear(); for (var idx = 0; idx < 5; ++idx ) { finished.Add(new ManualResetEvent(false)); Download(idx, url); } WaitHandle.WaitAll(finished.ToArray()); Console.WriteLine(“Done…”);
}
private static void Download(int idx, string url) { var wc = new WebClient(); wc.OpenReadCompleted += wc_OpenReadCompleted; wc.OpenReadAsync(new Uri(url), idx); } private static void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { Console.
read morePosts
[C#]從PE檔中讀取組件資訊
/// <summary> /// AssemblyDetails is used in the update builder, wyBuild (http://wyday.com/wybuild/), /// to automagically detect .NET assemblies properties, namely: /// - if they are compiled with a Strong Name, /// - CPUVersion the assembly was compiled as, /// - .NET Framework the assembly was compiled for (.NET 2.0 or 4.0) /// </summary> public class AssemblyDetails { /// <summary> /// The CPUVersion the assembly was compiled with. /// </summary> public CPUVersion CPUVersion;
read morePosts
[C#]忽略在 HTTP 剖析期間發生的驗證錯誤
if (anInstance != null) { //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); if (aUseUnsafeHeaderParsing != null) { aUseUnsafeHeaderParsing.SetValue(anInstance, true); return true; } } } } return false; }</pre>
read morePosts
[C#]擷取Picasa資料庫(_.PMP)內現有的資料
var type = br.ReadInt16(); if (0x1332 != br.ReadInt16()) { throw new Exception("Incorrect format"); } if (0x00000002 != br.ReadInt32()) { throw new Exception("Incorrect format"); } if (type != br.ReadInt16()) { throw new Exception("Incorrect format"); } if (0x1332 != br.ReadInt16()) { throw new Exception("Incorrect format"); } var number = br.ReadInt32(); switch (type) { case 0x00: DumpStringField(br, number); break; case 0x01: Dump4ByteField(br, number); break; case 0x02: DumpDateField(br, number); break; case 0x03: DumpByteField(br, number); break; case 0x04: Dump8ByteField(br, number); break; case 0x05: Dump2ByteField(br, number); break; case 0x06: DumpStringField(br, number); break; case 0x07: Dump4ByteField(br, number); break; default: throw new Exception("Incorrect format"); } } } .
read morePosts
[C#]簡易的Backoff window實現類別
namespace ConsoleApplication12 { public class BackOff { #region Var private int _level; private Random _random; #endregion
#region Private Property private ReadOnlyCollection<int> m_BackOffWindows { get; set; } private Random m_Random { get { if (_random == null) _random = new Random(Guid.NewGuid().GetHashCode()); return _random; } } #endregion #region Public Property /// <summary> /// Gets or sets the level. /// </summary> /// <value>The level.</value> public int Level { get { return _level; } set { if (value <= 0) throw new Exception("Level value must be bigger than zero!
read morePosts
[C#]設定WebBrowser Control運行的User Agent版本
64 bit: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
read morePosts
[C#]透過PerformanceCounter取得特定Process的CPU使用率
var instances = category.GetInstanceNames(); foreach (var instance in instances) { using (var counter = new PerformanceCounter(category.CategoryName, "ID Process", instance, true)) { int val = (int)counter.RawValue; if (val == pid) { return instance; } } } throw new ArgumentException("Invalid pid!"); }</pre> var lastUpdateTime = default(DateTime); m_UpdateTimePool.TryGetValue(pid, out lastUpdateTime); var interval = DateTime.Now - lastUpdateTime; if (interval.TotalSeconds > 1) { m_CpuUsagePool[pid] = (int)(m_CounterPool[pid].NextValue() / Environment.ProcessorCount); } return m_CpuUsagePool[pid]; }</pre> public static class ProcessExtension { #region Private Static Var private static Dictionary<int, PerformanceCounter> _counterPool; private static Dictionary<int, DateTime> _updateTimePool; private static Dictionary<int, int> _cpuUsagePool; #endregion
read morePosts
[C++]使用GetAdaptersAddresses API取得本地IP
GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &outBufLen); GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST, NULL, pAddresses, &outBufLen);
pCurrAddresses = pAddresses; while (pCurrAddresses) { if(pCurrAddresses->OperStatus != IfOperStatusUp) { pCurrAddresses = pCurrAddresses->Next; continue; } pUnicast = pCurrAddresses->FirstUnicastAddress; while (pUnicast) { addr = pUnicast->Address.lpSockaddr; ZeroMemory(buff, bufflen); if (addr->sa_family == AF_INET6) { sockaddr_in6 *sa_in6 = (sockaddr_in6 *)addr; inet_ntop(AF_INET6, &(sa_in6->sin6_addr), buff, bufflen); }else { sockaddr_in *sa_in = (sockaddr_in *)addr; inet_ntop(AF_INET, &(sa_in->sin_addr), buff, bufflen); } localIPs.push_back(buff); pUnicast = pUnicast->Next; } pCurrAddresses = pCurrAddresses->Next; } free(pAddresses); .
read more