.Net Framework
Signaling and Passing Data to the GUI Thread
Non GUI Thread
// This passes an int and a string to the GUI thread
Action action = new Action(DoGuiUpdate);
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, action, 5, "a string");
Handler that will be called on GUI thread
private void DoGuiUpdate(int number, string Str)
{
// Do something
}
Executing a Process from Code
Process process = new Process();
process.StartInfo.Arguments = "/c netstat -noa";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
string strOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
URI Encoding Text
String str = System.Uri.EscapeDataString(""");
// str is: "%3Cthis%20%26%20is%20line1%0D%0Aline2%20it%20%25%20%3E"
using used to Delete an Object automatically when it goes out of scope
Use the using statement
public void foo() {
using( Object obj = new Object) {
obj.DoSomething();
}
// obj gets deleted here
}
Type Conversion System.Convert
Global Exception Handler
Uncaught exceptions can be caught by this.
public static void Main()
{
System.Windows.Forms.Application.ThreadException += System.Thread.ThreadExceptionEventHandler( OnAppException);
}
private static void OnAppException( Object sender, ST.ThreadExceptionEventArgs e)
{
}
WebClient System.Net.WebClient
| Method | Description |
| string DownloadString(Uri) | Downloads data at link. |
| byte[] DownloadData(Uri) |
| DownloadFile(Uri, string Filename) | Downloads the data to a file. |
| stream OpenRead(Uri) |
XML System.XML
XmlDocument
| Method | Description |
| .Load(Stream) | Loads the XmlDocument from an input stream. |
XmlNode
| Method | Description |
| .InnerText | The text between the 2 tags of the node. |
| .ChildNodes | Get a collection of all the child nodes. |
| .SelectNodes(string) | Gets a collection of the child nodes with the given name. |
| .SelectSingleNode(string) | Selects the first node with the give name. |
ArrayList System.Collections.ArrayList
| Method | Description |
Add( Object); | |
- Only contains items of class
Object
- Default size is 16 elements
- Doubles capacity if size is exceeded
Array System.Array
Given:
int [] arInts = new int[10];
| Method | Description |
BinarySearch |
CopyTo( targetArray, 0); | Copy one array to the other,
I don't know what the 2nd parameter is. |
Queue System.Collections.Queue
| Method | Description |
Contains |
Dequeue |
Enqueue | |
Peek |
- Is circular
Object array
- Initial size is 32
- Default growth factor is 2.0 (size is multiplied by this when it needs to grow)
Queue System.Collections.Stack
| Method | Description |
Pop |
Push | |
- Internally is some sort of array (probably an ArrayList)
- Initial size is 10
Hashtable System.Collections.Hashtable
| Method | Description |
Add( Obeject key, Object value); | |
ContainsKey( Object key); |
Collection Keys(); | Returns all the keys in the table |
Object operator[ Object key]; | C# way of accessing the data |
- Expanding the size of the Hashtable is relatively expensive, so if you have a guess
as to how many elements will be in it, set its initial size accordingly
- The hash value is based on the key
System.Object.GetHash( Object)
- loadfactor is how full the table gets before being resized, you can specify
a loadfactor between 0.1 and 1.0, but internally it either (a)max'es out at 0.72
or (b) multiplies the factor times 0.72 - Microsoft decided performance goes down
the drain if the loadfactor is less than 0.72
- Size is appoximately doubled when adding a new value past the loadfactor. The next
size is actually chosen from a list of prime numbers, since the hash table size vrs
an aspect of choosing hash values should be relatively prime to reduce collisions
- On collision an alternate hash value calculation is used to find an empty cell
- Expected number of probes needed when a collision occurs is at most
1 / (1 - loadfactor)
which averages out to 3.5 by default
Data Structures
Data Structures
Generics (like c++ Templates)
Note: Not before .NET 2.0
System.Collections.Generic
System.Collections.Generic.List
MSDN Articles
- An Extensive Examination of Data Structures, Part 1: An Introduction to Data Structures
- An Extensive Examination of Data Structures, Part 2: The Queue, Stack, and Hashtable
- An Extensive Examination of Data Structures, Part 3: Binary Trees and BSTs
- An Extensive Examination of Data Structures, Part 4: Building a Better Binary Search Tree
contains information about "Skip Lists"
- An Extensive Examination of Data Structures, Part 5: From Trees to Graphs
- An Extensive Examination of Data Structures, Part 6: Efficiently Representing Sets
With C++
Making a C++ .NET Forms project by hand http://www.developer.com/net/net/article.php/1378751 "Building a .NET Windows Forms App in Visual C++ .NET"
Commonly Needed .NET libraries
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.Data.dll>
Message Box
#using <system.windows.forms.dll>
using namespace System::Windows::Forms;
namespace TestSceneGraphWrapper
{
public __gc class Wrapper
{
void Test() {
System::Windows::Forms::MessageBox::Show(
"Message",
"title",
MessageBoxButtons::OKCancel,
MessageBoxIcon::Information
);
}
};
}
Dialog
To open a Form as a modal Dialog, call .ShowDialog() to open it non-modal, call .Show()