private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value != null && row.Cells[0].Value.ToString() == textBox1.Text)
{
var ticketsCell = row.Cells[2];
var count = int.Parse(ticketsCell.Value.ToString());
if (count == 0) MessageBox.Show("Нет билетов!");
else ticketsCell.Value = count - 1;
}
}
}
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);
}