asynchronous

How to handle uncaught exceptions from CompletableFuture.runAsync

你离开我真会死。 提交于 2021-01-28 08:55:49
问题 Our application has some code that runs asynchronously that is failing. Like this: CompletableFuture.runAsync( () -> { throw new RuntimeException("bad"); }, executorService ); We want default exception handling code that can catch these errors, in case specific uses forget to handle exceptions (this came from a production bug). This is apparently tricky. The answer given in Handling exceptions from Java ExecutorService tasks does not work. It relies on the task being a Future<?> and then

How to cancel a task using a CancellationToken and await Task.WhenAny

浪子不回头ぞ 提交于 2021-01-28 07:54:32
问题 I have web application and it calls different web service to perform different work. Some web services will take long time and some short. Application will call all web services parallel to get result when complete or exception. I have started to write code but i need to know how to implement the following works in the best way. A task will be cancelled if any of web service takes more than 5 seconds but remaining tasks will continue to call web service to get either result or excepton. I

How can I write tests that have setup and teardown operations that are asynchronous?

百般思念 提交于 2021-01-28 07:03:09
问题 I am using a library (pouchDB) that does some async operations. To keep things simple, I will keep the details out of it as I think this is a general issue with "unit testing" involving async operations (unit testing in quotation marks because I guess this isn't truly unit testing if I am testing integration with another library. But, using QUnit seems like the most appropriate way to write tests for it). I am using QUnit to do my js unit testing. I have two tests. I am finding that if I run

Vuejs computed properties that depend on other, asynchronous, computed properties

喜夏-厌秋 提交于 2021-01-28 06:50:21
问题 In my Vuejs app I need to pass two computed properties to a component called avatar : an image surce and a string. The problem is that not all items have a picture, and when they don't, vuejs throws an error because it cannot read property apples of undefined (because album_id is undefined). The error is being thrown from that very long-winded src property in the avatar component, below: <template> <div class="apples"> <div id="mast" class="f3 b bb b--black-10"> <h2 class="ttc">Apple's List<

Use async function when consumer doesn't expect a promise

拈花ヽ惹草 提交于 2021-01-28 05:29:38
问题 I'm currently struggling to make an interop call work because I can't synchronously wait for a Promise in javascript. Before you think about closing this as duplicate, note that I have done some searching and I understand that it says everywhere it's not possible to synchronously wait for a Promise. Once async, always async. Source(s): How to wait for a JavaScript Promise to resolve before resuming function?, How can I synchronously determine a JavaScript Promise's state?, Call An

API call on event in react, componentDidUpdate or eventHandler?

℡╲_俬逩灬. 提交于 2021-01-28 05:29:36
问题 Where should we ideally place an api call to be made on occurrence of an event in React Inside the eventHandler or componentDidUpdate ? example: handleItemClick = (item) => (event) => { this.setState({selectedItem: item}); this.props.requestDataActionDispatch(item); } OR componentDidUpdate(prevProps, prevState, snapshot) { if(prevState.item !== this.state.item) { this.props.requestDataActionDispatch(item); } } 回答1: Depends But a simple solution is, if you want to call some API after change of

Waiting for a result without blocking a thread

半城伤御伤魂 提交于 2021-01-28 03:04:15
问题 I have this code doing what I want: TriggerSomeExternalProcess(); double secondsElapsed = 0; DateTime startTime = DateTime.UtcNow; double timeoutInSeconds = 10; while (secondsElapsed < timeoutInSeconds) { // TODO: this seems bad... secondsElapsed = DateTime.UtcNow.Subtract(startTime).TotalSeconds; } CheckStatusOfExternalProcess(); The goal is to TriggerSomeExternalProcess and then CheckStatusOfSomeExternalProcess - but that process runs on the same thread so I can't do Thread.Sleep() . It's

Wait for a task (or tasks) to reach certain milestone, the async/await way

柔情痞子 提交于 2021-01-28 02:01:41
问题 There are tons of examples of how to wait for a thread to exit, but sometimes i need to wait just until several threads are "ready" or have reached certain milestone. After reading: What's the proper way to wait for a .NET thread to start up? and http://www.albahari.com/threading/ If i have understood correctly: Wait for one Child Task to be ready : //Main Method var wh = new AutoResetEvent(false); Task childTask = Task.Run(() => ChildTask(wh); wh.WaitOne(); //Child private void ChildTask

C# Async Socket Server - BeginReceive in Callback

只愿长相守 提交于 2021-01-28 01:45:00
问题 I have a simple asynchronous socket server written in C# (pretty much Microsoft's example), however the issue with this example is that it accepts only one message from a client and then shuts down. I want this server to stay alive after receiving any message. This question has been asked before here, and the answer & comments explain that the resolution to this is simply to call handler.beginReceive within the SendCallback function, however to do this requires passing in a state variable.

C#, Calling async method inside OnStart of Windows service

主宰稳场 提交于 2021-01-27 21:18:45
问题 I am developing a windows service that is able to receive socket connection, so in the OnStart method: protected override void OnStart(string[] args) { start(); } The start function looks like this: public async void Start() { //initialization things ... ... TcpListener listener = new TcpListener(IPAddress.Any, port); listener.Start(); while(true) { TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false); ... } ... } The problem is that no connection is accepted, while