<Label
Content="{Binding ElementName=slider1, Path=Value}"
ContentStringFormat="{}Выбрано {0} единиц"
DockPanel.Dock="Top"
/>
using (var reader = new BinaryReader(File.Open("<file path>", FileMode.Open)))
{
var value1 = reader.ReadDouble();
var value2 = reader.ReadDouble();
...
}
using (var bw = new BinaryWriter(File.Create("<file path>")))
{
double value1 = 2;
double value2 = 17950.015625;
bw.Write(value1);
bw.Write(value2);
}
public sealed class ProgressChangedEventArgs : EventArgs
{
public ProgressChangedEventArgs (int progress) { this.Progress = progress; }
public int Progress { get; }
}
public event EventHandler<ProgressChangedEventArgs> StatusStripProgressChanged;
private void RaiseStatusStripProgressChanged(int progress)
{
StatusStripProgressChanged?.Invoke(new ProgressChangedEventArgs(progress));
}
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);
}
}
for (int i = 0; i < taskCount; i++)
{
Thread thread = new Thread(ParsingDocUserIdAync);
p[1] = docStarId;
p[2] += (int)offset;
thread.Start(param);
docStarId = p[2]+1;
list.Add(thread);
}
p
и param
- суть один и тот же объект). thread.Start(param)
не гарантирует, что поток создастся и начнёт выполняться моментально. 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);