![]() The issue comes in that it tries to be too accommodating, and control everything. So if you create a ship line, for instance, that delivers fuel to a port, it will walk the line to that port and check if a valid dock is available, and if the fuel can get to its end point from there. It has to do with the games need to walk each downstream dependency, on any line you create. Now I'm not in any way familiar with the internal workings of this game, so this is only a guess at this point. I have the same issue, but I think I've nailed down what exacerbates the issue. Real bug for me is to close game and go back to autosave frequently when using ships. ![]() So find that I need more separate routes and need too many vessels. There are differences in exception handling, executing thread, etc., but these are not relevant to this question.Originally posted by Occidental:I am finding that set up for Shipping Route is always slow and causes program to freeze. Nested loops are quite advanced technique (I actually never used it) and I don't recommend using it unless you have to. Task.ContinueWith(_ => nested.Continue = false, TaskScheduler.Default) Stephen Toub's WaitWithNestedMessageLoop extension method: static T WaitWithNestedMessageLoop(this Task task) The second approach is using nested loops, eg. And it will get much worse when you add exception handling blocks, using blocks, etc. Problem is that unlike await (which produces nice and clean code), your code will be full of these continuation lambdas. ![]() This is similar to what await does under a hood. So you basicly split synchronous method (one split instead of each await) into several parts (continuation lambda methods) linked by. Method can exit before DoSomething().Result becomes TaskScheduler.FromCurrentSynchronizationContext()) Otherwise this argument can be omitted. TaskScheduler argument is needed only if the continuation task First is to manually set up a continuation tasks like this: private void Button_Click(object sender, RoutedEventArgs e) While using async all the way down solution (as shown above) is IMO the preferred way, if you really can't change calling code to async, I can think of two ways to call async method from synchronous method without blocking UI. Private async void Button_Click(object sender, RoutedEventArgs e) If you make Button_Click handler async, you can then simply await DoSomething() directly: private TaskCompletionSource TaskCompletion = null It looks like you don't need StartSomething() method at all. Also DoSomething() is already asynchronous so you don't need to start a new Task for it. Result) of this task, which is effectively useless - it is nearly the same as calling DoSomething() directly. It starts a new Task and then just synchronously waits for the result (. Method StartSomething() doesn't make sense to me. TaskCompletion = new TaskCompletionSource() Īwait Task.WhenAny(TaskCompletion.Task, Task.Delay(3000)) Return Task.Run(() => DoSomething()).Result Private void Button_Click(object sender, RoutedEventArgs e) Plus I want to return the value from the async method to the top level that is on Button Click.Ĭan this code be improved or is there any other solution? private TaskCompletionSource TaskCompletion = null So basically I need a asynchronous method to be called by a synchronous method, without freezing the UI. I want the await to happen at DoSomething(). I don't want the Method StartSomething to be async because I don't want to await the method StartSomething. I need Result because i want to return the value. I am calling Task.Run(() => DoSomething()).Result which causes the UI to freeze and it happens because am using ".Result".
0 Comments
Leave a Reply. |