Example:
class Base
{
...
virtual ~Base();
...
};
class Derived : public Base
{
...
~Derived();
};
class Base
{
...
virtual ~Base();
...
};
class Derived : public Base
{
...
~Derived();
};
{
auto_ptr<T> ptr( new T );
...
} // when ptr goes out of scope it deletes the underlying object!
std::map<XXX*, DataAveragerPtr> averagerCache; const XXX* xxx = ... DataAveragerPtr averager; if(averagerCache.find(xxx) == averagerCache.end()) { // Not found so create a new one averager.reset(new SomeDataAverager(...)); averager->DoSomethingExpensive(); averagerCache.insert (std::pair<XXX*, DataAveragerPtr>( xxx, averager)); } else // found it { // for parametric types <A, B> use an A to get a B, // B b = mymap[a]; where a is of type A averager = averagerCache[xxx]; // lookup } ... if (!averagerCache.empty()) averagerCache.clear();When passing it as a parameter, the best option is to pass it by reference and just create it on the stack.
public class SomethingChangedEventArgs : EventArgs
{
...
}
The following steps take place in the event publisher class
class SomethingChangedEventPublisher
{
Step 2: Expose your custom EventHandler public event EventHandler<SomethingChangedEventArgs> SomethingChanged; // for events with custom arguments public event EventHandler SomethingChanged; // otherwise use this for standard eventsStep 3: Define an event publishing helper method (this is agood practice)
void PublishSomethingChangedEvent(SomethingChangedEventArgs ea)
{
EventHandler<SomethingChangedEventArgs> eh = this.SomethingChanged
if (eh != null)
eh(this, ea);
}
Step 4: Publish/Fire/Trigger the event in the appropriate places ... PublishSomethingChangedEvent(this, new SomethingChangedEventArgs(...)); ... }
class SomethingChangedEventSubscriber
{
Step 5: Subscribe to the event
// VS Intellisense will do most of the work for you here! SomethingChangedEventSource.SomethingChanged += new EventHandler<SomethingChangedEventArgs>(OnSomethingChanged); ...Step 6: Handle the event
// The method where the SomethingChangedEvent is processed
private void OnSomethingChanged(object sender, SomethingChangedEventArgs eargs)
{
// SomethingChanged detected, now do something with it here
}
...
Step 7: Unsubscribe to the event. This is in my experience the biggest cause of memory leaks in C# programs
// VS Intellisense will do most of the work for you here! SomethingChangedEventSource.SomethingChanged -= new EventHandler<SomethingChangedEventArgs>(OnSomethingChanged); ... }Alternatively, use an inline delegate to handle the event
SomethingChangedEventSource.SomethingChanged += delegate (object sender, SomethingChangedEventArgs e)
{
...
};
But there is a danger to doing this see here
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
...
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
// Use mutex to ensure only single instance is running
bool mutexOwnershipGranted = true;
using (Mutex mutex = new Mutex(
true,
"$SOMEUNIQUESTRING$", // Use GUID or string id here
out mutexOwnershipGranted))
{
// If this is the only running instance
if (mutexOwnershipGranted)
{ // Then run app
DevHelperWpf.App app = new DevHelperWpf.App();
app.InitializeComponent();
app.Run();
}
else
{ // Bring current running instance to the front
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(
current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
...
ShowEnvironmentVariables(EnvironmentVariableTarget.User);
ShowEnvironmentVariables(EnvironmentVariableTarget.Machine);
ShowEnvironmentVariables(EnvironmentVariableTarget.Process);
...
private static void ShowEnvironmentVariables(EnvironmentVariableTarget targ)
{
IDictionary ret = Environment.GetEnvironmentVariables(targ);
string[] keys = new string[ret.Count];
string[] values = new string[ret.Count];
ret.Keys.CopyTo(keys, 0);
ret.Values.CopyTo(values, 0);
string targStr = targ.ToString().ToUpper();
string underlineStr = "=======";
Debug.WriteLine(targStr);
Debug.WriteLine(underlineStr);
for (int ix = 0; ix < ret.Count; ix++)
{
Console.WriteLine(keys[ix] + " = " + values[ix]);
}
Debug.WriteLine("");
}
To write an Environment variable use "Environment.SetEnvironmentVariable"
// by default the environment variable is set on the "Process"
Environment.SetEnvironmentVariable("TestEnvVar", "change1");
// but can be set for the user
Environment.SetEnvironmentVariable("TestEnvVar", "andagain", EnvironmentVariableTarget.User);
// or the system (if the program is executed through an account
// with the appropriate permissions)
Environment.SetEnvironmentVariable("TestEnvVar", "andagain", EnvironmentVariableTarget.Machine);
...
saveFilePath = System.IO.Path.ChangeExtension(saveFilePath, "zip");
StringBuilder fileList = new StringBuilder();
string comment = "File changes from: " +
DateTime.UtcNow.ToLongDateString() +
Environment.NewLine +
"Original path: " + rootDir;
using (ZipStorer zipStore = ZipStorer.Create(saveFilePath, comment))
{
foreach (CheckedListItem cli in listBoxFilesChanged.SelectedItems)
{
if (cli.FileSystemInfo.Exists)
{
zipStore.AddFile(ZipStorer.Compression.Deflate, // Compress
cli.FullName, // Full path of file to be added
cli.RelativeName, // Stored as a relative path in zip file
cli.FullName); // Comment for stored file (source of the original)
fileList.AppendLine(cli.FullName);
}
}
}
...