S
S
skvoshiz2015-07-27 04:04:23
Programming
skvoshiz, 2015-07-27 04:04:23

How to remove the frieze of a c# WPF program?

Hello, I ran into a problem that I can’t solve myself =( I
use the xNet library for C #
I call the T.VK () method in the thread, which is in another class "T"
When an error is received, an exception occurs and then the program sometimes freezes for about a second 5. I'm calling on a thread, why does the app freeze, how to fix it, .NET Framework 4.0
1c61ed72d16c42f78e7f1aa452a8e309.JPG07fef76db6a14e28a51f804582260c55.JPG

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris the Animal, 2015-07-27
@skvoshiz

You for authorization create the whole flow. Why the brakes have already been told to you, you call authVK.join () in the main thread and thereby stop it yourself until the second thread is executed. In general, thanks to this code, you spend more resources and cause even more brakes if you didn’t use the stream at all.
In this case, you need to use Task. Or a lot of other ways to execute code not in the main thread and not create a new one. Esteem about a pool of flows and tasks (Task).
I see you created an Empty constant with an empty string!? So there is a standard string.Empty.

<Window x:Class="Wpf_Task.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350"
        WindowStartupLocation="CenterScreen">
   <Window.Resources>
      <Storyboard x:Key="OnLoaded1"
                  RepeatBehavior="Forever">
         <DoubleAnimationUsingKeyFrames Storyboard.TargetName="border"
                                        Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
            <EasingDoubleKeyFrame KeyTime="0"
                                  Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:1"
                                  Value="360" />
         </DoubleAnimationUsingKeyFrames>
      </Storyboard>
   </Window.Resources>

   <Window.Triggers>
      <EventTrigger RoutedEvent="FrameworkElement.Loaded">
         <BeginStoryboard Storyboard="{StaticResource OnLoaded1}" />
      </EventTrigger>
   </Window.Triggers>

   <Grid>

      <Button x:Name="executeButton"
              Width="75"
              Margin="0,216,0,0"
              HorizontalAlignment="Center"
              VerticalAlignment="Top"
              Click="Button_Click"
              Content="Выполнить"
              TextOptions.TextFormattingMode="Display" />

      <Rectangle x:Name="border"
                 Width="100"
                 Height="100"
                 Margin="208,57,208,161"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 RenderTransformOrigin="0.5,0.5">
         <Rectangle.Fill>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
               <GradientStop Offset="0" Color="#FFD1BD69" />
               <GradientStop Offset="1" Color="#FFFFF5C9" />
            </LinearGradientBrush>
         </Rectangle.Fill>
         <Rectangle.RenderTransform>
            <TransformGroup>
               <ScaleTransform />
               <SkewTransform />
               <RotateTransform />
               <TranslateTransform />
            </TransformGroup>
         </Rectangle.RenderTransform>
      </Rectangle>

      <TextBlock x:Name="_result"
                 Margin="0,187,0,0"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Top"
                 Text="..."
                 TextOptions.TextFormattingMode="Display"
                 TextWrapping="Wrap" />

   </Grid>
</Window>

using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Wpf_Task
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            executeButton.IsEnabled = false;
            Task<int>.Factory.StartNew(() =>
            {
               return Auth.Login();
            })
            .ContinueWith(task => //Выполнить код в основном потоке (TaskScheduler.FromCurrentSynchronizationContext())
            {
                executeButton.IsEnabled = true;
                _result.Text = task.Result.ToString();
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }

    public static class Auth
    {
        private static int _counter;

        public static int Login()
        {
            Thread.Sleep(1000);
            return ++_counter;
        }
    }
}

D
dordzhiev, 2015-07-27
@dordzhiev

You run everything on a separate thread and "join" it to the UI thread. The join blocks the current thread until the other thread finishes, so your code is essentially synchronous. Use async\await.

S
Sumor, 2015-07-27
@Sumor

In this case, the preparation of InteliSense to show the exception is most likely "freezed". See if there is the same delay when running the exe without studio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question