/// <summary> /// Sets the callback function of the BackgroundWorker. /// </summary> /// <param name="task">The callback function that contains the task that should be performed asynchronously.</param> /// <param name="finished">The callback function that should be called with the result of the task function.</param> /// <param name="progressChanged">The callback function for the progress changed handling.</param> /// <param name="supportCancellation">Flag to determine if the asynchronous task could be canceled.</param> public void SetCallbackFunctions(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false) { if (task == null) { throw new ArgumentNullException(); } mResultCall = finished; mRunCall = task; mProgressChangedCall = progressChanged; mException = null; mWorker = new BackgroundWorker(); mWorker.WorkerSupportsCancellation = supportCancellation; if (mResultCall != null) { mWorker.DoWork += new DoWorkEventHandler(Run); } if (mResultCall != null) { mWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Finish); } if (progressChanged != null) { mWorker.WorkerReportsProgress = true; mWorker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged); } }
public void Select(string url, string collection, string where, string sortKey, int rangeStart, int rangeCount, AsyncResultHandler callback) { JProperty propWhere = new JProperty("where", where); JProperty propSort = new JProperty("sortKey", sortKey); JProperty propRange = new JProperty("range", new JArray(rangeStart, rangeCount)); JObject jsonRequest = new JObject() { { "cmd", "select" }, { "collection", collection }, propWhere, propSort, propRange }; if (where == null) { propWhere.Remove(); } if (sortKey == null) { propSort.Remove(); } SendAsync(url, jsonRequest, callback); }
public void Hello(string url, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "hello" } }; SendAsync(url, jsonRequest, callback); }
public void SchemeList(string url, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "schemeList" } }; SendAsync(url, jsonRequest, callback); }
public void CollectionList(string url, string scheme, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "collectionList" }, { "scheme", scheme } }; SendAsync(url, jsonRequest, callback); }
public void DropScheme(string url, string schemeName, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "dropScheme" }, { "scheme", schemeName } }; SendAsync(url, jsonRequest, callback); }
/// <summary> /// Starts a async download of a mod from KSP Spaceport. /// </summary> /// <param name="modInfo"></param> /// <param name="finished"></param> /// <param name="progressChanged"></param> public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null) { // get save path int start = downloadURL.LastIndexOf("/") + 1; string filename = downloadURL.Substring(start, downloadURL.Length - start); modInfo.LocalPath = Path.Combine(downloadURL, filename); AsyncTask <bool> asyncJob = new AsyncTask <bool>(); asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged); asyncJob.RunDownload();; }
public void SendAsync(string url, JToken requestToken, AsyncResultHandler callback) { WebExceptionHandling(() => { var uri = new Uri(url); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri); httpRequest.Method = "POST"; httpRequest.ServicePoint.Expect100Continue = false; httpRequest.Timeout = RequestTimeout; httpRequest.ContentType = "application/json; charset=UTF-8"; string request = requestToken.ToString(Newtonsoft.Json.Formatting.None); byte[] byteArray = Encoding.UTF8.GetBytes(request); var dataStream = httpRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); httpRequest.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult result) { WebExceptionHandling(() => { using (var response = (HttpWebResponse)httpRequest.EndGetResponse(result)) using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"), true)) { string resultString = reader.ReadToEnd(); _workQueue.Enqueue(() => { try { callback(new RoseResult(resultString)); } catch (Exception e) { callback(new RoseResult(RoseResult.UnknownError, e.Message)); } }); } }, (e, errorCode) => { callback(new RoseResult(errorCode, e.Message)); }); }), httpRequest); }, (e, errorCode) => { callback(new RoseResult(errorCode, e.Message)); }); }
public void Delete(string url, string collection, string where, AsyncResultHandler callback) { JProperty propWhere = new JProperty("where", where); JObject jsonRequest = new JObject() { { "cmd", "delete" }, { "collection", collection }, propWhere }; if (where == null) { propWhere.Remove(); } SendAsync(url, jsonRequest, callback); }
/// <summary> /// Sets the download callback function of the BackgroundWorker. /// </summary> /// <param name="url">Download link</param> /// <param name="downloadPath">Path and filename to download to.</param> /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName<bool>(bool result, Exception ex)")</param> /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param> public void SetDownloadCallbackFunctions(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged = null) { mURL = url; mDownloadPath = downloadPath; mDownloadFinished = finished; mProgressChangedCall = progressChanged; mException = null; mWebClient = new WebClient(); mWebClient.Credentials = CredentialCache.DefaultCredentials; if (finished != null) { mWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFinished); } if (progressChanged != null) { mWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged); } }
public void Insert(string url, string collection, string uniqueFor, string onDuplicate, JToken data, AsyncResultHandler callback) { JProperty propUniqueFor = new JProperty("uniqueFor", uniqueFor); JProperty propOnDuplicate = new JProperty("onDuplicate", onDuplicate); JObject jsonRequest = new JObject() { { "cmd", "insert" }, { "collection", collection }, propUniqueFor, propOnDuplicate, { "data", data } }; if (uniqueFor == null) { propUniqueFor.Remove(); } if (onDuplicate == null) { propOnDuplicate.Remove(); } SendAsync(url, jsonRequest, callback); }
public void Update(string url, string collection, string where, string data, AsyncResultHandler callback) { Update(url, collection, where, JToken.Parse(data), callback); }
public void Insert(string url, string collection, string uniqueFor, string onDuplicate, string data, AsyncResultHandler callback) { Insert(url, collection, uniqueFor, onDuplicate, JToken.Parse(data), callback); }
/// <summary> /// Instantiates the AsyncTask class /// </summary> /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName<T_RETURN_VALUE>()")</param> /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName<T_RETURN_VALUE>(T_RETURN_VALUE result, Exception ex)")</param> /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param> /// <param name="supportCancellation">Flag to determine if cancellation is needed.</param> public AsyncTask(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished, AsyncProgressChangedHandler progressChanged = null, bool supportCancellation = false) { SetCallbackFunctions(task, finished, progressChanged, supportCancellation); }
public void DropCollection(string url, string schemeName, string collectionName, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "dropCollection" }, { "collection", $"{schemeName}.{collectionName}" } }; SendAsync(url, jsonRequest, callback); }
public void CreateCollection(string url, string schemeName, string collectionName, AsyncResultHandler callback) { JObject jsonRequest = new JObject() { { "cmd", "createCollection" }, { "collection", $"{schemeName}.{collectionName}" }, { "justInCache", false } }; SendAsync(url, jsonRequest, callback); }
/// <summary> /// Starts a asynchrony download. /// </summary> /// <param name="url">Download link</param> /// <param name="downloadPath">Path and filename to download to.</param> /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName<bool>(bool result, Exception ex)")</param> /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param> public static void RunDownload(string url, string downloadPath, AsyncResultHandler <bool> finished = null, AsyncProgressChangedHandler progressChanged = null) { AsyncTask <bool> asyncTask = new AsyncTask <bool>(url, downloadPath, finished, progressChanged); asyncTask.RunDownload(); }
/// <summary> /// Instantiates the AsyncTask class for a async download. /// </summary> /// <param name="url">Download link</param> /// <param name="downloadPath">Path and filename to download to.</param> /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName<bool>(bool result, Exception ex)")</param> /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param> public AsyncTask(string url, string downloadPath, AsyncResultHandler <bool> finished, AsyncProgressChangedHandler progressChanged) { SetDownloadCallbackFunctions(url, downloadPath, finished, progressChanged); }
/// <summary> /// Starts a async download of a mod from KSP Spaceport. /// </summary> /// <param name="modInfo"></param> /// <param name="finished"></param> /// <param name="progressChanged"></param> public static void DownloadModAsync(string downloadURL, ref ModInfo modInfo, AsyncResultHandler<bool> finished = null, AsyncProgressChangedHandler progressChanged = null) { // get save path int start = downloadURL.LastIndexOf("/") + 1; string filename = downloadURL.Substring(start, downloadURL.Length - start); modInfo.LocalPath = Path.Combine(downloadURL, filename); AsyncTask<bool> asyncJob = new AsyncTask<bool>(); asyncJob.SetDownloadCallbackFunctions(modInfo.SpaceportURL, modInfo.LocalPath, finished, progressChanged); asyncJob.RunDownload(); ; }
/// <summary> /// This function calls the passed task function asynchronously. /// </summary> /// <param name="task">The task function to execute asynchrony. (A function with the signature "T_RETURN_VALUE FunctionName<T_RETURN_VALUE>()")</param> /// <param name="finished">The finished callback function. (A function with the signature "void FunctionName<T_RETURN_VALUE>(T_RETURN_VALUE result, Exception ex)")</param> /// <param name="progressChanged">The progress change callback function. (A function with the signature "void FunctionName(int percentage)")</param> public static void DoWork(AsyncHandler <T_RETURN_VALUE> task, AsyncResultHandler <T_RETURN_VALUE> finished = null, AsyncProgressChangedHandler progressChanged = null) { AsyncTask <T_RETURN_VALUE> asyncTask = new AsyncTask <T_RETURN_VALUE>(task, finished, progressChanged); asyncTask.Run(); }