Skip to content

Instantly share code, notes, and snippets.

@adsl99801
Last active March 21, 2018 02:34
Show Gist options
  • Save adsl99801/043d2076f5cd91ad0049b4d1ced17368 to your computer and use it in GitHub Desktop.
Save adsl99801/043d2076f5cd91ad0049b4d1ced17368 to your computer and use it in GitHub Desktop.
backgroundWorker
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security;
using WordAddIn1.Tool;
using WordAddIn1.Model;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Flurl.Http;
using RestSharp;
namespace WordAddIn1
{
public partial class UploadPage : UserControl
{
private delegate void HttpDoneDelegate(string text);
private static readonly HttpClient client = new HttpClient();
public UploadPage()
{
InitializeComponent();
progressBar1.Visible = false;
}
private void button2_ClickAsync(object sender, EventArgs e)
{
progressBar1.Visible = true;
var bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(get);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(get_done);
bw.RunWorkerAsync();
}
private void get(object sender, DoWorkEventArgs e)
{
var client = new RestClient("https://slack.com/api/");
var request = new RestRequest("api.test", Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
Console.Write(content);
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = content;
}
private void get_done(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
return;
}
if(e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
var result = e.Result as string;
//way1:
//progressBar1.Invoke((MethodInvoker)(() =>
// GetResult(result)
//));
//way2
HttpDoneDelegate dele1 = new HttpDoneDelegate(GetResult);
this.BeginInvoke(dele1,new object[] { result});
}
private void GetResult(string text)
{
button2.Text = text;
progressBar1.Visible = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment