Blocking a stream in .Net

I have a class that has purely static methods and properties. I call the async method in the Load class, which requests a web service for a piece of data that then fires an event that fires the LoadCompleted return method. I don’t know how long the call will take (the difference is between calling the Load method, then calling LoadCompleted).

I would like to block the application from continuing until the callback method is raised (since the application will try to get material from this class, which is not populated until the LoadComplete method sets the data). How should I do it?

+3
source share
3 answers

Avoid blocking the main UI thread with extreme prejudice.

I would use the control BusyIndicatorfrom the Silverlight Toolkit: -

<UserControl x:Class="StackoverflowSpikes.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

    <toolkit:BusyIndicator x:Name="busyInd" >
        <Grid x:Name="LayoutRoot">
          <!-- The rest of your content -->
        </Grid>
    </toolkit:BusyIndicator>

</UserControl>

Before calling Loaduse: -

busyInd.IsBusy = true;

and then LoadCompleteuse: -

busyInd.IsBusy = false;

This will block user input in the user interface without blocking the main thread, and will give you the opportunity to use some feedback on why they can’t click anything right now. You can provide your own content for a busy message using the property BustContent. Of course, if you do not like how it looks, you can customize it as you wish.

MVVM, IsBusy VM, , VM , - .

+6

ManualResetEvent , . WaitOne Set - asyc. , , .

+2

. , . .

+1

Source: https://habr.com/ru/post/1759519/