class Program
{
private static bool Unsubscribe = false;
static void Main(string[] args)
{
var ship = new Ship();
Enumerable.Range(0, 100000).ToList().ForEach(i =>
{
Console.Write($"\r{i + 1}\t");
ship.MyTask = new MyTask(i);
ship.MyTask.TaskCompleted += OnTaskCompleted;
ship.MyTask.DoAndRaise();
});
ship.MyTask = null;
Console.WriteLine();
Console.WriteLine("Raised: " + MyTask.EventsRaised);
Console.WriteLine("Collected: " + MyTask.Collected);
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Collected: " + MyTask.Collected);
}
static void OnTaskCompleted(object semder, int taskId)
{
if (Unsubscribe)
{
var mt = semder as MyTask;
mt.TaskCompleted -= OnTaskCompleted;
}
}
}
public class MyTask
{
public static int Collected, EventsRaised;
public MyTask(int id)
{
TaskId = id;
}
public int TaskId { get; }
public event EventHandler<int> TaskCompleted;
public void DoAndRaise()
{
TaskCompleted?.Invoke(this, TaskId);
Interlocked.Increment(ref EventsRaised);
}
~MyTask()
{
Interlocked.Increment(ref Collected);
}
}
public class Ship
{
public MyTask MyTask { get; set; }
}
class Program
{
static readonly string[] Extensions = new string[] { "*.jpg", "*.png" };
static void Main(string[] args)
{
var drives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Fixed);
drives.AsParallel().ForAll(d =>
{
SearchFile(d.RootDirectory.FullName);
});
}
static void SearchFile(string path, int maxDegree = 1)
{
var images = Directory.GetFiles(path).Where(f =>
{
var ext = Path.GetExtension(f);
return Extensions.Any(e => e.Equals(ext, StringComparison.OrdinalIgnoreCase));
});
images.ToList().ForEach(i =>
{
// process image
});
var dirs = Directory.GetDirectories(path);
dirs.AsParallel().WithDegreeOfParallelism(maxDegree).ForAll(d => SearchFile(d));
}
}
var aes = new AesCryptoServiceProvider {Mode = CipherMode.CBC};
var encryptor = aes.CreateEncryptor();
var decryptor = aes.CreateDecryptor();
var image = File.ReadAllBytes("image.png");
var encryptedImagePart = encryptor.TransformFinalBlock(image, 0, 1024);
using (var fs = File.Create("encrypted"))
{
fs.Write(encryptedImagePart, 0 , encryptedImagePart.Length);
fs.Write(image, 1024, image.Length - 1024);
fs.Flush(true);
}
var encryptedImage = File.ReadAllBytes("encrypted");
var decryptedImagePart = decryptor.TransformFinalBlock(encryptedImage, 0, encryptedImagePart.Length);
using (var fs = File.Create("decrypted"))
{
fs.Write(decryptedImagePart, 0, decryptedImagePart.Length);
fs.Write(encryptedImage, encryptedImagePart.Length, encryptedImage.Length - encryptedImagePart.Length);
fs.Flush(true);
}
public partial class Form1 : Form
{
private readonly XDocument _xml;
public Form1()
{
InitializeComponent();
_xml = CreateNewXml();
}
private void button1_Click(object sender, EventArgs e)
{
var docNode = _xml.Root.Element("Document");
var lastTextNode = docNode.Elements("Text").LastOrDefault();
var textNode = CreateNewTextNode();
if (lastTextNode != null)
lastTextNode.AddAfterSelf(textNode);
else
docNode.Add(textNode);
_xml.Save("_s.temp");
}
private XDocument CreateNewXml()
{
var doc = new XDocument(new XDeclaration("1.0", "windows-1251", null));
var fileNode = new XElement("File");
fileNode.Add(new XAttribute(XNamespace.Xmlns + "xsi",
"http://www.w3.org/2001/XMLSchema-instance"));
fileNode.Add(new XAttribute("id", 1),
new XAttribute("version", "2.0"));
XElement docNode = new XElement("Document");
docNode.Add(new XAttribute("DocId", 11512),
new XAttribute("Date", "12.07.15"));
fileNode.Add(docNode);
doc.Add(fileNode);
return doc;
}
private XElement CreateNewTextNode()
{
XElement textNode = new XElement("Text");
textNode.Add(
new XAttribute("a", textBox1.Text),
new XAttribute("b", textBox2.Text));
return textNode;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = null;
textBox2.Text = null;
}
}
var persons = new ObservableCollection<Person>();
var table = new DataTable("Persons");
var nameCol = new DataColumn("name") {DataType = typeof (string), AllowDBNull = true};
table.Columns.Add(nameCol);
var ageCol = new DataColumn("age") {DataType = typeof (int), AllowDBNull = false};
table.Columns.Add(ageCol);
persons.ToList().ForEach(p =>
{
var row = table.NewRow();
row.SetField(nameCol, p.Name);
row.SetField(ageCol, p.Age);
table.Rows.Add(row);
});