private void button1_Click(object sender, EventArgs e) { if (this.calcBtn.Text == "Calculate") { timer1.Start(); // Just a simple object to hold some values to be accessed globally pi = new PIObject { calculatedValue = "", num = (int)_digits.Value, prog = 0 }; backgroundWorker1.RunWorkerAsync(pi); progressBar1.Maximum = pi.num - 1; calcBtn.Text = "Cancel"; //calcBtn.Text = "Completed"; } else { timer1.Stop(); backgroundWorker1.CancelAsync(); } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { PIObject piObj = e.Argument as PIObject; BackgroundWorker bw = sender as BackgroundWorker; StringBuilder pi = new StringBuilder("3", piObj.num + 2); if (piObj.num > 0) { pi.Append("."); for (int i = 0; i < piObj.num; i += 9) { if (!this.backgroundWorker1.CancellationPending) { int nineDigits = NineDigitsOfPi.StartingAt(i + 1); int digitCount = Math.Min(piObj.num - i, 9); String ds = String.Format("{0:D9}", nineDigits); pi.Append(ds.Substring(0, digitCount)); piObj.calculatedValue = pi.ToString(); piObj.prog = i + 1; } else { e.Cancel = true; } } } }
private async Task <Cities> GetCitiesDataNoBatch() { //Exercise 1 //First HTTP request: Get the WebId of the AF database Weather string url = baseUrl + @"assetdatabases?path=\\pisrv01\Weather"; string response = await DownloadWebData(url); PIObject dbData = JsonConvert.DeserializeObject <PIObject>(response); //Second HTTP request: Find all the 50 attributes called Wikipedia Thumbnail Url //HINT: https://pisrv01.pischool.int/piwebapi/help/controllers/element/actions/findelementattributes //HINT: Use JsonConvert.DeserializeObject to convert a JSON string to C# object PIListObject imageAttributes; //Retrieve the response and deserialize into imageAttributes //Third HTTP request: Get the values of those 50 attributes by making a bulk call //HINT: https://pisrv01.pischool.int/piwebapi/help/controllers/streamset/actions/getvaluesadhoc PIListObject imagesValues; //Retrieve the response and deserialize into imagesValues //Using both PIListObjects as inputs, the Cities object is created. //Cities cities = new Cities(imageAttributes, imagesValues); //return cities; return(null); }
private async Task <Cities> GetCitiesDataNoBatch() { //Exercise 1 //First HTTP request against PI Web API to get the WebId of the AF database Weather //Generate the url for making the first HTTP request string url = baseUrl + @"assetdatabases?path=\\pisrv01\Weather"; //Retrieve the string response from URL string response = await DownloadWebData(url); //JsonConvert.DeserializeObject from JSON.NET converts the JSON string into a PIObject. PIObject dbData = JsonConvert.DeserializeObject <PIObject>(response); //Second HTTP request against PI Web API to get the WebId from all attributes "Wikipedia Thumbnail Url" (one per each city). //HINT: https://pisrv01.pischool.int/piwebapi/help/controllers/element/actions/findelementattributes url = baseUrl + "assetdatabases/" + dbData.WebId + "/elementattributes?attributeNameFilter=*Wikipedia%20Thumbnail%20Url*"; response = await DownloadWebData(url); PIListObject imageAttributes = JsonConvert.DeserializeObject <PIListObject>(response); //Third HTTP request against PI Web API to get the current values from all attributes "Wikipedia Thumbnail Url" using the WebId retrieved from the previous call. //Get the WebIds from the PIListObject List <string> webIds = imageAttributes.GetItemsWebIds(); //Using the GetValuesAdHoc method from StreamSet Controller //HINT: https://pisrv01.pischool.int/piwebapi/help/controllers/streamset/actions/getvaluesadhoc url = baseUrl + "streamsets/value?"; foreach (string webId in webIds) { url = url + string.Format("webId={0}&", webId); } url = url.Substring(0, url.Length - 1); response = await DownloadWebData(url); PIListObject imagesValues = JsonConvert.DeserializeObject <PIListObject>(response); //Using both PIListObjects as inputs, the Cities object is created. Cities cities = new Cities(imageAttributes, imagesValues); return(cities); }