Jump to content

Search the Community

Showing results for tags 'async'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office


  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start



Member Title




Found 3 results

  1. So asynchronous programming is something quite difficult to do in JavaScript, but since we now have Promises and async/await, it's becoming increasingly less complicated. Plus there is no need for "callback hell". So I will assume you understand what Promise.all() is and why it can sometimes be problematic, as it will not wait for all requests to complete and just fails on the first rejected Promise. The following function will still reject on a failed Promise, but the difference being it will wait for all the Promises to complete before resolving or rejecting. If no failure occurred, then the resolved value is the same as Promise.all(), an array of resolved values; otherwise, it returns a completed object (see below for details). Also note that the array might contain empty slots, this is so it's easier to debug which Promise failed in the array, as they're inserted in the same index slot Promise.allComplete = (iterable) => { if (!Array.isArray(iterable)) { throw new TypeError('Invalid argument, expected "iterable" to be an array'); } const completed = { resolved: [], rejected: [], }; const wrapResolutionOrRejection = (type, index) => valueOrReason => (completed[type][index] = valueOrReason); const wrappedIterable = iterable.map((value, index) => Promise.resolve(value) // The rejected wrapper function could be put in the catch, but it's wasteful for our purposes .then(wrapResolutionOrRejection('resolved', index), wrapResolutionOrRejection('rejected', index)) ) return Promise.all(wrappedIterable) .then(() => completed.rejected.length === 0 ? Promise.resolve(completed.resolved) : Promise.reject(completed)); }; // Example const requests = [ createPromise(true, 10), createPromise(false, 10), createPromise(true, 200), createPromise(true, 1000), ]; // Rejects on the first Promise which fails, but if you check in the console, it didn't wait // for the third Promise to successfully complete, as the console log came after the error log was displayed // Promise.all(requests) // .then(values => console.log('Successfully completed', values)) // .catch(err => console.error('Not successfully completed', err)) // "allComplete" is different, in that it will wait for all the Promises to be completed i.e. resolve and reject, // then resolve if all Promises were successful or reject if one Promise failed. // It returns the following data structure: // { // resolved: [...Promises which resolved, and inserted by the associated Promise's index], // rejected: [..Promises which rejected, and inserted by the associated Promise's index], // } Promise.allComplete(requests) .then(completed => console.log('Successfully completed', completed)) .catch(completed => console.log('Not successfully completed', completed)) function createPromise(isResolved, delay) { return new Promise((resolve, reject) => { setTimeout(() => { console.log(`Promise: "${delay}"`); if (isResolved) { resolve(delay); } else { reject(new Error('An unexpected error occurred')); } }, delay); }); }
  2. I have some nested async methods calling each other and it is confusing. I am trying to convert a project which downloads the files in an async download. On the click of the download button this is the method triggered: private async void enableOfflineModeToolStripMenuItem_Click(object sender, EventArgs e) { for(int i = 0; i < _playlists.Count; i++) { DoubleDimList.Add(new List<String>()); for(int j = 0; j < 5; j++) { string sMp3 = IniReadValue(_playlists[i], "Track " + j); DoubleDimList[i].Add(sMp3); } await Task.Run(() => _InetGetHTMLSearchAsyncs(DoubleDimList[i])); } } It creates a 2d List which at the end looks like this DoubleDimList[3][20]. At the end of each sublist I am doing an async download as you can see. The method looks like this private async Task _InetGetHTMLSearchAsyncs(List<string> urlList) { foreach (var url in urlList) { await Task.Run(() => _InetGetHTMLSearchAsync(url)); } } the _InetGetHTMLSearchAsync method looks like this and here is where it gets tricky private async Task _InetGetHTMLSearchAsync(string sTitle) { Runs++; if (AudioDumpQuery == string.Empty) { //return string.Empty; } string sResearchURL = "http://www.audiodump.biz/music.html?" + AudioDumpQuery + sTitle.Replace(" ", "+"); try { using (var client = new WebClient()) { client.Headers.Add("Referer", @"http://www.audiodump.com/"); client.Headers.Add("user-agent", "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A"); client.DownloadStringCompleted += Client_DownloadStringCompleted; await Task.Run(() => client.DownloadStringAsync(new Uri(sResearchURL))); } } catch (Exception ex) { Console.WriteLine("Debug message: " + ex.Message + "InnerEx: " + ex.StackTrace); Console.WriteLine("Runs: " + Runs); //throw exception return; } } On Client_DownloadStringCompleted there is another async method called. Here it is private async void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { string[] sStringArray; string aRet = e.Result; string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline); if (aTable != null) { string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\""); if (aInfos != null) { for (int i = 0; i < 1; i++) { sStringArray = aInfos[i].Split('*'); sStringArray[0] = sStringArray[0].Replace("&#39;", "'"); aLinks.Add(sStringArray[0]); } await Task.Run(() => DownloadFile(aLinks[FilesDownloaded])); } } } From there, surprise! Another async call. private async Task DownloadFile(string url) { try { using (var client = new WebClient()) { client.Headers.Add("Referer", @"http://www.audiodump.biz/"); client.Headers.Add("user-agent", "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A"); client.DownloadFileCompleted += Client_DownloadFileCompleted; await Task.Run(() => client.DownloadFileTaskAsync(url, mp3Path + "\\" + count + ".mp3")); } } catch (Exception Ex) { Console.WriteLine("File download error: " + Ex.StackTrace); //throw exception } } Now the first part after the creation of the 2d List is to retrieve the download links of the mp3s. The second part is to download the mp3 as soon as a valid URL was provided. It works but in a bizarre way. Instead of downloading the file normally(1st, 2nd, 3rd...), it will download randomly the files(1st, 5th, 8th...). Where am I messing this up because I definitely am!?
  3. It's been requested in the past to have multithreading to which the response was "It would take too much redesigning of Autoit" but what about Async? Multithreading and Async are two different things. This way we could put tasks in the background without having to fork processes. Right? Also better event handling would be nice rather than throwing everything in a while loop we could have some functionality like javascript which seems to be far more responsive and reliable as the more you add to your while loop the less change there is of your "event" getting caught for some reason (At least in my experience).
  • Create New...