Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Lab1_nowe
- {
- public interface IRunnable
- {
- void Run();
- IEnumerator<float> CoroutineUpdate();
- bool HasFinished
- {
- get;
- set;
- }
- }
- public abstract class Agent : IRunnable
- {
- public int czestotliwosc = 10;
- public int ID;
- public static int IDAgent;
- public abstract void Update();
- public void Run()
- {
- while (!HasFinished)
- {
- double opoznienie = 1000 / czestotliwosc;
- Thread.Sleep((int)opoznienie);
- Update();
- }
- }
- public IEnumerator<float> CoroutineUpdate()
- {
- while(!HasFinished)
- {
- Update();
- yield return 1;
- }
- yield break;
- }
- public bool HasFinished
- {
- get;
- set;
- }
- }
- public class ConstantCountingAgent : Agent
- {
- private int licz;
- public ConstantCountingAgent()
- {
- ID=++IDAgent;
- }
- override public void Update()
- {
- if (licz<=10)
- {
- HasFinished = false;
- // Console.WriteLine("stan licznika: " + licz);
- }else
- {
- Console.WriteLine("ConstantCountingAgent zakończył prace: ID = " + ID);
- HasFinished = true;
- }
- licz++;
- }
- }
- public class CountingAgent : Agent
- {
- public int licz;
- public CountingAgent()
- {
- ID = ++IDAgent;
- }
- override public void Update()
- {
- if (licz <= ID)
- {
- HasFinished = false;
- //Console.WriteLine("stan licznika: " + licz);
- }
- else
- {
- Console.WriteLine("CountingAgent zakończył prace: ID = " + ID);
- HasFinished = true;
- }
- licz++;
- }
- }
- public class SineGeneratingAgent : Agent
- {
- float Output
- {
- get;
- set;
- }
- double czas;
- double czas1;
- int licz;
- public SineGeneratingAgent()
- {
- ID = ++IDAgent;
- }
- override public void Update()
- {
- czas = czas + (1 / Convert.ToDouble(czestotliwosc));
- if (ID%10 >= czas)
- {
- HasFinished = false;
- // Console.WriteLine("{0}", Math.Round(Math.Sin(czas), 2));
- }
- else
- {
- Console.WriteLine("SineGeneratingAgent zakończył prace: ID = " + ID);
- HasFinished = true;
- }
- }
- }
- class Program
- {
- static List<IRunnable> list_ob = new List<IRunnable>();
- static List<IRunnable> list_liczb = new List<IRunnable>();
- static List<IRunnable> GenerateRunnables()
- {
- Random gen = new Random();
- for (int i = 0; i < 1000; i++)
- {
- list_liczb[i] = gen.Next(1, 49);
- Console.Write("{0}; ", list_liczb[i]);
- }
- List<IRunnable> lista = new List<IRunnable>();
- for (int i = 0; i < 10; i++)
- {
- lista.Add(new ConstantCountingAgent());
- lista.Add(new CountingAgent());
- lista.Add(new SineGeneratingAgent());
- }
- return lista;
- }
- public static void RunThreads(List<IRunnable> lista)
- {
- for (int i=0; i<lista.Count; i++)
- {
- Thread watek = new Thread(lista.ElementAt(i).Run);
- watek.Start();
- }
- }
- static public void RunFibres(IEnumerable<IRunnable> wlokna)
- {
- var functions = wlokna.Select(n => n.CoroutineUpdate());
- while (!(wlokna.All(m => m.HasFinished)))
- {
- foreach (var fiber in functions)
- {
- if (!wlokna.All(m => m.HasFinished))
- {
- fiber.MoveNext();
- }
- }
- System.Threading.Thread.Sleep(100);
- }
- }
- static void Main(string[] args)
- {
- list_ob = GenerateRunnables();
- //RunThreads(GenerateRunnables());
- RunFibres(list_ob);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement