using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ThreadsTests
{
public class WorkData
{
public int Id;
}
public abstract class ProtoBase<T>
{
private static int NextId;
private TaskCompletionSource<T> _dataTaskCompletionSource = new TaskCompletionSource<T>();
public int give_count = 1;
protected ProtoBase()
{
Id = ++NextId;
new Task(Parse).Start();
}
public int Id { get; }
public bool IsFinish { get; private set; }
protected void Finish()
{
IsFinish = true;
}
public async Task PushData(T data)
{
_dataTaskCompletionSource.SetResult(data);
await Task.Yield();
}
protected async Task<T> GetNextData()
{
var taskResult = await _dataTaskCompletionSource.Task;
_dataTaskCompletionSource = new TaskCompletionSource<T>();
return taskResult;
}
protected abstract void Parse();
}
public class TestWorker : ProtoBase<WorkData>
{
protected override async void Parse()
{
WorkData data = await GetNextData();
Trace.TraceInformation("{0:N0} take 1 [id={1:N0}]", Id, data.Id);
data = await GetNextData();
Trace.TraceInformation("{0:N0} take 2 [id={1:N0}]", Id, data.Id);
data = await GetNextData();
Trace.TraceInformation("{0:N0} take 3 [id={1:N0}]", Id, data.Id);
Finish();
Trace.TraceInformation("{0:N0} Finish ххх", Id);
}
}
public class Program
{
public static async Task Main(string[] args)
{
var schedulerPair = new ConcurrentExclusiveSchedulerPair();
await await Task.Factory.StartNew(
AsyncWorkersTest,
CancellationToken.None,
TaskCreationOptions.None,
schedulerPair.ExclusiveScheduler);
Console.WriteLine("FINISHED");
Console.ReadKey();
}
public static async Task AsyncWorkersTest()
{
//workers count
const int testCount = 1000; // 100, 1000, 10000, 100000
var Workers = new List<TestWorker>();
for (int i = 0; i < testCount; i++)
{
Workers.Add(new TestWorker());
}
Random rnd = new Random();
int getDataCount = testCount * 3;
for (int i = 0; i < getDataCount; i++)
{
int ind = rnd.Next(0, Workers.Count);
WorkData wd = new WorkData() { Id = i };
if (Workers[ind].IsFinish) continue;
Trace.TraceInformation("{0:N0} push {1} [id={2:N0}]", Workers[ind].Id, Workers[ind].give_count++, wd.Id);
await Workers[ind].PushData(wd);
}
}
}
}
ConsoleApp1 Information: 0 : 1 push 1 [id=0]
ConsoleApp1 Information: 0 : 1 take 1 [id=0]
ConsoleApp1 Information: 0 : 2 push 1 [id=1]
ConsoleApp1 Information: 0 : 2 take 1 [id=1]
ConsoleApp1 Information: 0 : 1 push 2 [id=2]
ConsoleApp1 Information: 0 : 1 take 2 [id=2]
ConsoleApp1 Information: 0 : 1 push 3 [id=3]
ConsoleApp1 Information: 0 : 1 take 3 [id=3]
ConsoleApp1 Information: 0 : 1 Finish ххх
ConsoleApp1 Information: 0 : 2 push 2 [id=4]
ConsoleApp1 Information: 0 : 2 take 2 [id=4]
ConsoleApp1 Information: 0 : 2 push 3 [id=5]
ConsoleApp1 Information: 0 : 2 take 3 [id=5]
ConsoleApp1 Information: 0 : 2 Finish ххх
public interface ISignable
{
ISignature Signature { get; set; }
byte[] Serialize();
}
public interface ISignature
{
}
public interface ISignatureService
{
bool Validate(ISignable signable);
void Sign(ISignable signable);
}
class MyObject : ISignable
{
public ISignature Signature { get; set; }
public long Param1 { get; set; }
public string Param2 { get; set; }
public string InnerData { get; set; }
public byte[] Serialize()
{
return Encoding.UTF8.GetBytes(Param1 + Param2 + InnerData);
}
}
public class HashCodeSignatureService : ISignatureService
{
public void Sign(ISignable signable)
{
var signature = CalculateSignature(signable);
signable.Signature = signature;
}
public bool Validate(ISignable signable)
{
var s1 = CalculateSignature(signable);
var s2 = signable.Signature as SimpleHashCodeSignature;
return s1?.HashCode == s2?.HashCode;
}
private static SimpleHashCodeSignature CalculateSignature(ISignable signable)
{
var body = signable.Serialize();
var signature = new SimpleHashCodeSignature(body.Aggregate(0, (a, b) => a + b.GetHashCode()));
return signature;
}
}
public class SimpleHashCodeSignature : ISignature
{
public int HashCode { get; }
public SimpleHashCodeSignature(int hashCode)
{
HashCode = hashCode;
}
}
class Program
{
static void Main(string[] args)
{
var obj = new MyObject {Param1 = 1, Param2 = "asd", InnerData = "some data"};
var signatureService = new HashCodeSignatureService();
signatureService.Sign(obj);
// Passing the object across untrusted boundary
signatureService.Validate(obj);
}
}
class Program
{
static void Main(string[] args)
{
string scriptFileName = Path.GetTempFileName() + ".cs";
var scriptBody = GenerateScript(scriptFileName);
var script = CSharpScript.Create(
scriptBody,
globalsType: typeof(Globals),
options: ScriptOptions
.Default
.WithEmitDebugInformation(true)
.WithFilePath(scriptFileName)
.WithFileEncoding(Encoding.UTF8));
var sum = script.RunAsync(new Globals {X = 56, Y = 42}).Result.ReturnValue;
Console.Out.WriteLine(sum);
Console.ReadKey();
File.Delete(scriptFileName);
}
private static string GenerateScript(string fileName)
{
var body =
@"var result = X + Y;
return result;
";
File.WriteAllText(fileName, body, Encoding.UTF8);
return body;
}
}
public class Globals
{
public int X { get; set; }
public int Y { get; set; }
}
class Program
{
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
Console.ReadKey();
}
static async Task MainAsync(string[] args)
{
HardWorker hardWorker1 = new HardWorker();
HardWorker hardWorker2 = new HardWorker();
HardWorker hardWorker3 = new HardWorker();
HardWorker hardWorker4 = new HardWorker();
HardWorker hardWorker5 = new HardWorker();
Task t1 = Task.CompletedTask;
Task t2 = Task.CompletedTask;
Task t3 = Task.CompletedTask;
Task t4 = Task.CompletedTask;
Task t5 = Task.CompletedTask;
Random rand = new Random();
for (int i = 0; i < 50; i++)
{
t1 = t1.ContinueWith(task => hardWorker1.DoSomething(1, rand.Next(1, 10000)));
t2 = t2.ContinueWith(task => hardWorker2.DoSomething(2, rand.Next(1, 10000)));
t3 = t3.ContinueWith(task => hardWorker3.DoSomething(3, rand.Next(1, 10000)));
t4 = t4.ContinueWith(task => hardWorker4.DoSomething(4, rand.Next(1, 10000)));
t5 = t5.ContinueWith(task => hardWorker5.DoSomething(5, rand.Next(1, 10000)));
}
await Task.WhenAll(t1, t2, t3, t4, t5);
}
}
public class HardWorker : AbstractHardWorker
{
public void DoSomething(int task, int time)
{
Console.Out.WriteLine("Start: " + task);
Thread.Sleep(time);
Console.Out.WriteLine("End: " + task);
}
}
public interface AbstractHardWorker
{
void DoSomething(int task, int time);
}
var bytes1 = new byte[writeStream.Length];
writeStream.Read(bytes1, 0, bytes1.Length);
writer.Write(bytes1, 0, bytes1.Length);
writeStream.Position = 0;
writeStream.CopyTo(writer);
int count;
dictionary.TryGetValue(word, out count);
dictionary[word] = count + 1;