private void btnAsyncAsync_Click(object sender, EventArgs e) { buttonCaption = btnAsyncAsync.Text; btnAsyncAsync.Text = "In progress"; var loadedLines = Task.Run(async() => { using (var stream = new StreamReader(File.OpenRead("StockPrices_Small.csv"))) { var lines = new List <string>(); string line; while ((line = await stream.ReadLineAsync()) != null) { lines.Add(line); } return(lines.ToArray()); } }); var processStocks = loadedLines.ContinueWith(continuationAction); processStocks.ContinueWith(t => { GuiDelegate del = delegate { btnAsyncAsync.Text = buttonCaption; }; btnAsyncAsync.Invoke(del); }); }
private void checkForStart() { if (GUI.Button(drawRect, "Start Server")) { Network.InitializeServer(4, 9090, false); currentGui = waitForStart; } }
private void btnWithCancel_Click(object sender, EventArgs e) { btnWithCancel.Text = "Cancel"; if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); cancellationTokenSource = null; return; } cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Token.Register(() => { textBox1.Text = "Cancellation requested"; }); var loadedLines = SearchForStocks(cancellationTokenSource.Token); var processStocks = loadedLines.ContinueWith(continuationAction, cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current); processStocks.ContinueWith(t => { GuiDelegate del = delegate { btnWithCancel.Text = "6 With Cancel"; }; btnWithCancel.Invoke(del); }); }
private void btnException_Click(object sender, EventArgs e) { buttonCaption = btnException.Text; btnException.Text = "In progress"; var loadedLines = Task.Run(() => { var lines = File.ReadAllLines("SSSSSStockPrices_Small.csv"); return(lines); }); var processStocks = loadedLines.ContinueWith(continuationAction, TaskContinuationOptions.OnlyOnRanToCompletion); loadedLines.ContinueWith(t => { GuiDelegate del = delegate { textBox1.Text = t.Exception.InnerException.Message; }; textBox1.Invoke(del); }, TaskContinuationOptions.OnlyOnFaulted); processStocks.ContinueWith(t => { GuiDelegate del = delegate { btnException.Text = buttonCaption; }; btnException.Invoke(del); }); }
void Start() { networker = GameObject.FindGameObjectWithTag("Networker").GetComponent <Networker>(); guiRenderer = MenuGUI; // Adding listeners to the networker's events Networker.ClientConnected += Networker_ClientConnected; Networker.ServerInitialized += Networker_ServerInitialized; Networker.Disconnected += Networker_Disconnected; }
private void UpdateView(GuiDelegate func) { if (InvokeRequired) { Invoke(func); } else { func(); } }
public Form1() { InitializeComponent(); continuationAction = t => { // use results from previous Task var lines = t.Result; // may ask for result ONLY when task completed var data = new List <StockPrice>(); foreach (var line in lines.Skip(1)) { data.Add(createStock(line)); } GuiDelegate del = delegate { dataGridView1.DataSource = data.Where(price => price.Ticker == tbTicker.Text).Select(p => new { value = $"{p.Ticker} {p.Volume}" }).ToList(); dataGridView1.Columns[0].Width = dataGridView1.Width; }; dataGridView1.Invoke(del); }; loadFile = () => { var lines = File.ReadAllLines("StockPrices_Small.csv"); var data = new List <StockPrice>(); foreach (var line in lines.Skip(1)) { data.Add(createStock(line)); } GuiDelegate del = delegate { dataGridView1.DataSource = data.Where(price => price.Ticker == tbTicker.Text).Select(p => new { value = $"{p.Ticker} {p.Volume}" }).ToList(); dataGridView1.Columns[0].Width = dataGridView1.Width; }; dataGridView1.Invoke(del);// use invoke to execute delegate in original thread; }; }
public void Write(ICollection messages) { if (Form.InvokeRequired) { var del = new GuiDelegate(Write); Form.BeginInvoke(del, messages); } else { Form.richTextBox2.Clear(); foreach (var message in messages) { var formattedMessage = Formatter.ApplyFormat((Message)message); Form.richTextBox2.SelectionStart = 0; Form.richTextBox2.SelectionLength = 0; Form.richTextBox2.SelectedText = "\r\n" + formattedMessage; } } }
private void btnContinuetion_Click(object sender, EventArgs e) { buttonCaption = btnContinuetion.Text; btnContinuetion.Text = "In progress"; var loadedLines = Task.Run(() => { var lines = File.ReadAllLines("StockPrices_Small.csv"); return(lines); }); var processStocks = loadedLines.ContinueWith(continuationAction); processStocks.ContinueWith(t => { GuiDelegate del = delegate { btnContinuetion.Text = buttonCaption; }; btnContinuetion.Invoke(del); }); }
void InstructionsGUI() { GUILayout.BeginArea(new Rect(0f, 0f, Screen.width, Screen.height)); GUILayout.BeginVertical(skin.GetStyle("mainMenu"), GUILayout.Height(Screen.height), GUILayout.Width(Screen.width)); if (GUILayout.Button("Back")) { guiRenderer = MenuGUI; return; } GUIStyle specialText = new GUIStyle(skin.GetStyle("InstructionsText")); specialText.normal.textColor = new Color(0.15f, 0.15f, 0.15f); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Move your character with", skin.GetStyle("InstructionsText")); GUILayout.Label(" W A S D", specialText); GUILayout.Label(" keys", skin.GetStyle("InstructionsText")); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Jump with the", skin.GetStyle("InstructionsText")); GUILayout.Label(" spacebar", specialText); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Shoot with", skin.GetStyle("InstructionsText")); GUILayout.Label(" Left click", specialText); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label("Change weapon with the", skin.GetStyle("InstructionsText")); GUILayout.Label(" mouse wheel", specialText); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.Label("Items :", skin.GetStyle("InstructionsText")); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Box(textures["Medkit"], GUILayout.Width(48f), GUILayout.Height(35f)); GUILayout.Label(" : Health", skin.GetStyle("InstructionsText")); GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace(); GUILayout.Box(textures["GunAmmo"], GUILayout.Width(48f), GUILayout.Height(35f)); GUILayout.Label(" : Gun Ammo", skin.GetStyle("InstructionsText")); GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace(); GUILayout.Box(textures["RLAmmo"], GUILayout.Width(48f), GUILayout.Height(35f)); GUILayout.Label(" : Rocket Launcher Ammo", skin.GetStyle("InstructionsText")); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.EndArea(); }
void MenuGUI() { GUILayout.BeginVertical(skin.GetStyle("mainMenu"), GUILayout.Height(Screen.height), GUILayout.Width(Screen.width)); if (GUILayout.Button("Instructions")) { guiRenderer = InstructionsGUI; return; } GUILayout.FlexibleSpace(); // Main Title GUILayout.BeginArea(new Rect(0f, 0f, Screen.width, 100f)); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); // Center Horizontally GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); // Center Vertically GUILayout.Label("Very Original FPS", skin.GetStyle("mainTitle")); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndArea(); // Begin Server/Client GUI GUILayout.BeginArea(new Rect(30f, 100f, Screen.width - 60f, Screen.height - 100f)); GUILayout.BeginHorizontal(skin.GetStyle("padding"), GUILayout.ExpandWidth(true)); // This customStyle contains the padding values // Server GUI skin.GetStyle("menuColumn").padding.left = (int)((Screen.width - 60f) / 8f); GUILayout.BeginVertical(skin.GetStyle("menuColumn")); // Server Title GUILayout.Label("Host Game", skin.GetStyle("title")); GUILayout.BeginVertical(GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); // Used to center the fields vertically // Server Port Field GUILayout.BeginHorizontal(); GUILayout.Label("Listen Port", skin.GetStyle("label")); networker.portTF = GUILayout.TextField(networker.portTF, 15); GUILayout.EndHorizontal(); // Launch Button GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Launch server")) { networker.LaunchServer(); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.EndVertical(); // End Server GUI // Client GUI GUILayout.BeginVertical(skin.GetStyle("menuColumn")); // Client Title GUILayout.Label("Join Game", skin.GetStyle("title")); GUILayout.BeginVertical(GUILayout.ExpandWidth(false)); GUILayout.FlexibleSpace(); // Used to center the fields vertically // Server IP Field GUILayout.BeginHorizontal(); GUILayout.Label("Server IP", skin.GetStyle("label")); networker.serverIP = GUILayout.TextField(networker.serverIP, 15); GUILayout.EndHorizontal(); // Server Port Field GUILayout.BeginHorizontal(); GUILayout.Label("Server Port", skin.GetStyle("label")); networker.portTF = GUILayout.TextField(networker.portTF, 15); GUILayout.EndHorizontal(); // Join Button GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Join")) { System.Int32.TryParse(networker.portTF, out networker.serverPort); networker.ConnectToServer(); } GUILayout.EndVertical(); GUILayout.EndVertical(); // End Client GUI GUILayout.EndHorizontal(); GUILayout.EndArea(); // End Server/Client GUI GUILayout.EndVertical(); }
void Networker_Disconnected() { guiRenderer = MenuGUI; }
void Networker_ServerInitialized() { guiRenderer = ServerGUI; }
void Networker_ClientConnected() { guiRenderer = GameGUI; }
// Use this for initialization void Start() { currentGui = checkForStart; }
private async void btnOneByOne_Click(object sender, EventArgs e) { btnOneByOne.Text = "Cancel"; if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); cancellationTokenSource = null; return; } cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Token.Register(() => { textBox1.Text = "Cancellation requested"; }); try { var tickers = tbTickers.Text.Split(',', ' '); var tickerLoadingTasks = new List <Task <IEnumerable <StockPrice> > >(); var service = new StockService(); var stocks = new ConcurrentBag <StockPrice>();//thread safe collection foreach (var item in tickers) { var loadedLines = service.SearchForStocks(item, cancellationTokenSource.Token) .ContinueWith(t => { foreach (var stock in t.Result.Take(5)) { stocks.Add(stock); } GuiDelegate del = delegate { dataGridView1.DataSource = stocks.Select(p => new { value = $"{p.Ticker} {p.Volume}" }).ToList(); dataGridView1.Columns[0].Width = dataGridView1.Width; }; dataGridView1.Invoke(del); return(t.Result); }); tickerLoadingTasks.Add(loadedLines); } var allLoadingTasks = Task.WhenAll(tickerLoadingTasks); await allLoadingTasks; } catch (Exception ex) { textBox1.Text = ex.Message; } finally { cancellationTokenSource = null; } btnOneByOne.Text = "9 One By One"; }
void Start() { networker = GameObject.FindGameObjectWithTag("Networker").GetComponent<Networker>(); guiRenderer = MenuGUI; // Adding listeners to the networker's events Networker.ClientConnected += Networker_ClientConnected; Networker.ServerInitialized += Networker_ServerInitialized; Networker.Disconnected += Networker_Disconnected; }