task

Using Await in BackgroundWorker causes WorkerComplete event to be raised

有些话、适合烂在心里 提交于 2019-12-24 21:16:33
问题 I have this weird problem. I need to invoke a process from within a background worker Private Shared _process As Process Private Shared _StartInfo As ProcessStartInfo Private WithEvents _bwConvertMedia As New BackgroundWorker Here is the work in DoWorkAsync Private Async Sub _bwConvertMedia_DoWorkAsync(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _bwConvertMedia.DoWork For AI = 1 To 100 _StartInfo = New ProcessStartInfo(".\mycmd.exe", "-1") _StartInfo

Proper use of Task.Delay to delay key presses

老子叫甜甜 提交于 2019-12-24 19:33:01
问题 The code below works for what i'm doing however I'm curious if my use of Task.Delay() is not best practice. I need a delay to ensure that the GameHandler has enough time to process my Key presses. However, i'm wondering if there is a better approach to doing something like this. async public Task<bool> CloseMenusAsync(CancellationToken token) { while (GameHandler.Menu.IsOpen && !token.IsCancellationRequested) { if (GameHandler.Menu.IsOpen && GameHandler.Menu.DialogText.Question == "Open") {

Wait for task return on session.dataTask

北城以北 提交于 2019-12-24 18:28:22
问题 I have a project where I send and get data back from an API. Sending and getting it works fine. My goal is to wait a response (ie: yes|no) from the server and depending on it, proceed with the segue or not and show an alert if no. For this part I have: a tableview view with a list of clients and a button to add new client. a detail view where I add/edit a client on detail view there is a save button with a segue to the tableview view The function saveClient() gets the data and makes a request

WPF Thread Delay

那年仲夏 提交于 2019-12-24 17:24:56
问题 I have a method which I call in a new task with // get the dispatcher for the UI thread var uiDispatcher = Dispatcher.CurrentDispatcher; Task.Factory.StartNew(() => BackgroundThreadProc(uiDispatcher)); In the method BackgroundThreadProc() I need a delay of few seconds. I tried it with the DispatcherTimer and the task.delay function but it didn't work. The only thing which worked was the System.Threading.Thread.Sleep(1) but I think the Thread.Sleep() function isn't the best solution. This is

Task vs async Task

余生颓废 提交于 2019-12-24 14:27:33
问题 Ok, I've been trying to figure this out, I've read some articles but none of them provide the answer I'm looking for. My question is: Why Task has to return a Task whilst async Task doesn't? For example: public override Task TokenEndpoint(OAuthTokenEndpointContext context) { // Code removed for brevity. return Task.FromResult<object>(null); } As you can see there, that method isn't async , so it has to return a Task. Now, take a look at this one: public override async Task

Why my task work only once?

半世苍凉 提交于 2019-12-24 12:02:41
问题 I want to print every 2 sec number but and in the end i get only 0 what i need to do to get this every 2 sec? result: 0 1 . . 49 private static void Main(string[] args) { Send(); } public static async Task Send() { for (int i = 0; i < 50; i++) { Console.WriteLine(i); await Task.Delay(2000); } } 回答1: well, simply because your Main method won't wait for the send method to finish and you can't use the await keyword in order for that to happened since the compiler won't allow an async Main , in

Making code that runs automatically within a given interval

允我心安 提交于 2019-12-24 11:35:01
问题 I have a few questions about having code that runs automatically within a given interval. I'm programming a kind of game mode where it checks if the players have killed all the monsters in the map (I have my methods for these). I was wondering what's the best way to program this check? I've looked up ways where a person made a ScheduledExecutorService during a class constructor.... private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); But I saw online where

Synchronization context for Task.Delay

徘徊边缘 提交于 2019-12-24 10:28:12
问题 I could find out that, Task.Run executes always on threads from .NET Framework threads pool ( TaskScheduler.Default ). I suppose, that it is the same with Task.Delay , but I'm not sure. MSDN says for Task.Delay only: Creates a task that will complete after a time delay Therefore the question: Where (in which synchronization context) runs Task.Delay ? 回答1: Task.Delay doesn't run anywhere . It just creates a task that completes after the specified time. Unlike Task.Run it's not accepting a

Cancel specific child thread or task after unsubscribing socket stream

筅森魡賤 提交于 2019-12-24 10:11:02
问题 I'm trying to run multi-threaded tasks under main task using following code but not working expectedly. CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationToken = cancellationTokenSource.Token; using (var socketClient = new SocketClient()) { Task.Factory.StartNew(() => { Console.WriteLine("Starting child task..."); if (cancellationToken.IsCancellationRequested) { Console.WriteLine("Task cancellation requested"); throw new

WinRT: blocking Task

做~自己de王妃 提交于 2019-12-24 07:54:17
问题 I'm implementing network layer of my application, that is using an asynchronous JSON-RPC protocol. In order to communicate with the server, i'd like to make a method that will send a proper request, wait until the server sends response, and return it. Everything with use of async/await keywords. Here's simplified sample code: string response; Task<string> SendRequest(string methodName, string methodParams) { string request = generateRequest(methodName, methodParams); await Send(request); //