/// <summary> /// This "assert" is executed in DEBUG and RELEASE modes. Use it in code that that won't suffer from more work (e.g. loading), not in frequently used loops /// </summary> /// <param name="condition"></param> public static void AssertRelease(bool condition, string assertMessage) { if (condition == false) { MyMwcLog.WriteLine("Assert: " + assertMessage); } System.Diagnostics.Trace.Assert(condition, assertMessage); }
public static void DumpMemoryUsage() { m_events.Sort(m_managedComparer); MyMwcLog.WriteLine("\n\n"); MyMwcLog.WriteLine("Managed MemoryUsage: \n"); float totalMB = 0; for (int i = 0; i < m_events.Count && i < 30; i++) { float sizeInMB = m_events[i].ManagedDelta * 1.0f / (1024 * 1024); totalMB += sizeInMB; MyMwcLog.WriteLine(m_events[i].Name + sizeInMB.ToString()); } MyMwcLog.WriteLine("Total Managed MemoryUsage: " + totalMB + " [MB]"); ////////////////////////////////////////////////////////////////////////// m_events.Sort(m_nativeComparer); MyMwcLog.WriteLine("\n\n"); MyMwcLog.WriteLine("Process MemoryUsage: \n"); totalMB = 0; for (int i = 0; i < m_events.Count && i < 30; i++) { float sizeInMB = m_events[i].ProcessDelta * 1.0f / (1024 * 1024); totalMB += sizeInMB; MyMwcLog.WriteLine(m_events[i].Name + sizeInMB.ToString()); } MyMwcLog.WriteLine("Total Process MemoryUsage: " + totalMB + " [MB]"); ////////////////////////////////////////////////////////////////////////// m_events.Sort(m_timeComparer); MyMwcLog.WriteLine("\n\n"); MyMwcLog.WriteLine("Load time comparison: \n"); float totalTime = 0; for (int i = 0; i < m_events.Count && i < 30; i++) { float deltaTime = m_events[i].DeltaTime; totalTime += deltaTime; MyMwcLog.WriteLine(m_events[i].Name + " " + deltaTime.ToString()); } MyMwcLog.WriteLine("Total load time: " + totalTime + " [s]"); }
// Download string from specified url using specified timeout. If url doesn't exist or // timeout is raised, method returns null. Otherwise content of the url. // Method ignores any exception that occurs during its execution! public static string DownloadStringSynchronously(string url, int timeoutInMilliseconds, string userAgent, Encoding fileEncoding) { MyMwcLog.WriteLine("DownloadStringSynchronously - START"); MyMwcLog.IncreaseIndent(); MyMwcLog.WriteLine("Url: " + url); MyMwcLog.WriteLine("TimeoutInMilliseconds: " + timeoutInMilliseconds); string pageContent = null; int retryCount = 3; while (retryCount-- > 0) { try { // Open a connection HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); // You can also specify additional header values like the user agent or the referer: webRequest.UserAgent = userAgent; webRequest.Timeout = timeoutInMilliseconds; // Setting PROXY to null is very important, because if it is null, this web request will not // use any proxy. It will connect to our update server directly, no matter what is specified // in Internet Explorer proxy settings. // If we leave there default values, it works on 99% of computers. But 1% has some weird proxy // settings in IE - they don't use proxy, but this web request thinks they use proxy... // E.g. http://dotnetdreaming.wordpress.com/2009/03/20/httpwebrequest-and-proxies/ webRequest.Proxy = null; // Request response: WebResponse response = null; try { response = webRequest.GetResponse(); // Open data stream: Stream stream = null; try { stream = response.GetResponseStream(); // Create reader object: StreamReader reader = null; try { reader = new StreamReader(stream, fileEncoding); // Read the entire stream content: pageContent = reader.ReadToEnd(); } catch (Exception ex) { pageContent = null; MyMwcLog.WriteLine(ex); // Ignore exception! } finally { if (reader != null) { reader.Close(); } } } catch (Exception ex) { pageContent = null; MyMwcLog.WriteLine(ex); // Ignore exception! } finally { if (stream != null) { stream.Close(); } } } catch (Exception ex) { pageContent = null; MyMwcLog.WriteLine(ex); // Ignore exception! } finally { if (response != null) { response.Close(); } } } catch (Exception ex) { pageContent = null; MyMwcLog.WriteLine(ex); // Ignore exception! } if (pageContent != null) { break; } else { Thread.Sleep(3000); MyMwcLog.WriteLine("Retrying..."); } } //MyMwcLog.AddToLog("Page content: " + pageContent); MyMwcLog.WriteLine("Characters count: " + (pageContent == null ? "null" : pageContent.Length.ToString())); MyMwcLog.DecreaseIndent(); MyMwcLog.WriteLine("DownloadStringSynchronously - END"); return(pageContent); }