Write your answer Related questions. Is syncsta gay? What happened to syncsta? Why did backstreet-boys break up? Why did Backstreet Boys break up? Why does boys have to break up the girls heart? When did the green mountain boys break up? Why did the beastie boys break up? Did The Beastie Boys break up? Did Beastie Boys break up? What happen to the Backstreet Boys?
Why are boys afraid to show their girlfriend to their friends? Did the Friday night boys break up? Why do boys breakup? Why do girls and boys break up? How old are the guys in syncsta? What are B-Boys and B-Girls?
Did the fall out boys split up? How do you know when a boys going to break up with you? Why do boys flirt with other girls when they have a girlfriend? When did the Backstreet Boys break up? Do any of the boys want to break up with there girlfriends? Did the Backstreet Boys break up? Did back street boys break up? Please do NOT edit other people's answers to insert a link to your own.
If you believe your answer is better, leave it as a comment instead. Show 2 more comments. Then you call task. The data type is simply Task. Let's assume that DoSomethingAsync is long-running async method as whole internally it awaits a long-running task , but it yields back a flow control to its caller quickly, thus the lambda argument work ends also quickly.
The result of Tusk. Conclusions are that we probably need to use Unwrap approach as was done in J. Lennon post to achieve synchronous behaviour of async method. Run is different than Task. StartNew in that it automatically unwraps the result already. Can I just write Task. Run DoSomethingAsync instead?
This removes one level of delegates. The correct overload of Task. Run is selected either way, but the async delegate makes your intent obvious. Show 1 more comment.
There's almost always a better answer than to do sync-over-async. In that case, you would need to use one of the hacks described in my article on brownfield async development , specifically: Blocking e. Note that this can cause deadlocks as I describe on my blog.
Running the code on a thread pool thread e. Note that this will only work if the asynchronous code can be run on a thread pool thread i. NET context. Nested message loops. Note that this will only work if the asynchronous code only assumes a single-threaded context, not a specific context type a lot of UI and ASP. NET code expect a specific context. Community Bot 1 1 1 silver badge. Stephen Cleary Stephen Cleary k 69 69 gold badges silver badges bronze badges.
AlexeiLevenkov: I don't feel right doing that, for a few reasons: 1 The answer on the linked question is fairly out-of-date. So, closing this as a dup of that would be an abuse of power; closing that as a dup of this or merging would empower a dangerous answer even more. I let it be, and leave it to the community. This answer goes a long way over my head. A program with an async Main method doesn't compile; at some point you've got to bridge the gap between the sync and async worlds.
It's not a " very rare situation" , it's necessary in literally every program that calls an async method. There is no option to not "do sync-over-async" , just an option to shunt that burden up to the calling method instead of shouldering it in the one you're currently writing.
MarkAmery: Sync-over-async is necessary in the Main method of console apps. NET, unit test frameworks, and every UI system all support async natively. Even if all your apps are console apps, you'd only need to do sync-over-async once per app. I'm about to put async on all of the methods in my application now. And that's a lot. Can't this just be the default? It worth to mention that as you replied me and based on your blog here , the deadlock is not a problem for ASP.
NET Core! Task's implementation of the await pattern is "smart" in that it defers to the SynchronizationContext , which basically causes the following to happen: If the thread entering the await is on a Dispatcher or WinForms message loop thread, it ensures that the chunks of the async method occurs as part of the processing of the message queue.
If the thread entering the await is on a thread pool thread, then the remaining chunks of the async method occur anywhere on the thread pool. For example, if your callstack was something like the following: [Top] WebRequest. GetResponse YourCode. HelperMethod YourCode. AnotherMethod YourCode. GetResponseAsync YourCode. HelperMethodAsync YourCode. AnotherMethodAsync YourCode. RunEx GetCustomers. Morse 6, 5 5 gold badges 32 32 silver badges 58 58 bronze badges.
Theo Yaung Theo Yaung 3, 1 1 gold badge 16 16 silver badges 14 14 bronze badges. Result hangs the application when it gets run on a suspended dispatcher thread. Also, the GetCustomers method is normally run async, however in one situation it needs to run synchronously, so I was looking for a way to do that without building a sync version of the method. Add a comment. Clement Clement 3, 35 35 silver badges 43 43 bronze badges.
You need to also use Task. Unwrap method, because your Task. Wait statement causes waiting for outer Task created by Task. Run , not for inner await t Task passed as parameter of extension method. Your Task. In some simple scenarios your solution may works because of TaskScheduler optimizations, for example using TryExecuteTaskInline method to execute Tasks within current thread during Wait operation.
Please look at my comment to this answer. That is not correct. The Task. See this overload msdn. How is this supposed to be used? RunTaskSynchronously ; — ygoe. This only works for platforms without synchronization contexts console apps, ASP. NET Core apps etc. For tasks that has already started, there is no point in wrapping it in Task. In other words, in normal usages like GetFromNetworkAsync. RunTaskSynchronously hangs for UI apps. Unwrap ; Trace.
Wait ; Trace. Lennon J. Lennon 3, 4 4 gold badges 31 31 silver badges 63 63 bronze badges. This is the only one that didn't deadlock for me. AndreFeijo I dont know what it is, but this is essentially Task. Wait with slight tweaks. Both should work. RunSynchronously ; In my case, I have an event that fires when something occurs.
But how could we use this method when the async code returns something we need? This works for cold tasks, not tasks that has begun. I found this code at Microsoft. Core component, and it works.
None, TaskCreationOptions. None, TaskContinuationOptions. None, TaskScheduler. CurrentCulture; return AsyncHelper. Tested in. It can also avoid deadlock. For async method returning Task. Task DoSomeWork ; Task. Result; Edit : If the caller is running in the thread pool thread or the caller is also in a task , it may still cause a deadlock in some situation. Palle Due 4, 3 3 gold badges 19 19 silver badges 30 30 bronze badges.
Liang Liang 9 9 silver badges 13 13 bronze badges. Result is perfect for the job if you want a synchronous call, and downright dangerous otherwise. There is nothing in the name Result or in the intellisense of Result that indicates it is a blocking call.
It really should be renamed. Wait works for WinRT. WriteLine customer. WaitOne TimeSpan. RredCat RredCat 4, 3 3 gold badges 56 56 silver badges 93 93 bronze badges. I explained this solution, check EDIT section. This can very easily result in deadlocks when called in asynchronous situations.
Servy make sense. So as I get correct using Wait timeOut can help, right? Then you need to worry about having the timeout being reached when the operation isn't actually done, which is very bad, and also the time spent waiting until the timeout in the cases where it deadlocks and in that case you're still continuing on when it's not done.
So no, that doesn't fix the problem. Servy Looks like I have to implement CancellationToken for my solution. Show 3 more comments. RunSynchronously ; Edit: You say that you get an exception. RunSynchronously ; Assert. Dan Abramov Dan Abramov k 77 77 gold badges silver badges bronze badges. I adjusted my question to make the code I had attempted a bit clearer.
RunSynchronously returns an error of RunSynchronously may not be called on a task unbound to a delegate. Google is no help since all the results for that are in chinese I think the difference is that I don't create the Task and then try to run it. Instead, the task is created by the async method when the await keyword is used.
The exception posted in my earlier comment is the exception I get, although it is one of the few that I cannot Google and find a cause or resolution for. As for exception, you only posted exception message, which is useless without exception type and stack trace.
Call exception's ToString method and post output in the question. The discussion is about async-await methods, not about simple Task-returning methods.
Moreover, in my opinion, async-await mechanism is a syntax sugar, but not so trivial - there is continuation , context capturing, local context resuming, enhanced local exceptions handling, and more. Then, you shouldn't invoke RunSynchronously method on result of the async method, because by definition asynchronous method should return Task that is currently at least scheduled, and more than once is in the running state.
Why not create a call like: Service. GetCustomers ; that isn't async. These extra frames of timecode sometimes cause Nuendo to restart suddenly. When the Jam-Sync option is activated, Nuendo will ignore any changes in timecode once it has started playback. This can be useful in special situations, such as synchronizing to broken timecode. Nuendo can notify the user when the frame rate of timecode changes at any point. This is helpful in diagnosing problems with timecode and external devices.
0コメント