none
Thread on visual basic RRS feed

  • Question

  • Hello everyone,

    I am developing a sample wpf application with 2 progess bar, which start independently when the correspond button been clicked.

    I achieve this result using c# thread, but now i run into some problems like many c# instructions aren't implemented on vb.net.

    Surfing on the internet i found the backgroundworkers solution, but in my view this method is heavy and not good for the thing which i want to do.

    Below i attach the working c# and xaml code, and my question is: If this is possible to obtain this in vb.net

    C# code

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private async void Button_prog1_Click(object sender, RoutedEventArgs e)
            {
                button_prog1.IsEnabled = false;
                var progress = new Progress<int>(value => progress1.Value = value);
                await Task.Run(() =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ((IProgress<int>)progress).Report(i);
                        Thread.Sleep(50);
                    }
                });
                button_prog1.IsEnabled = true;
                progress1.Value = 0;
            }
    
            private async void Button_prog2_Click(object sender, RoutedEventArgs e)
            {
                button_prog2.IsEnabled = false;
                var progress = new Progress<int>(value => progress2.Value = value);
                await Task.Run(() =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        ((IProgress<int>)progress).Report(i);
                        Thread.Sleep(50);
                    }
                });
                button_prog2.IsEnabled = true;
                progress2.Value = 0;
            }
    
            private void Button_close_Click(object sender, RoutedEventArgs e)
            {
                System.Windows.Application.Current.Shutdown();
            }
        }

    Xaml code

    <Grid>
            <ProgressBar Name="progress1" Minimum="0" Maximum="100" />
            <ProgressBar Name="progress2" Minimum="0" Maximum="100" />
            <Button x:Name="button_prog1" Content="Start progress 1" Click="Button_prog1_Click"/>
            <Button x:Name="button_prog2" Content="Start progress 2"  Click="Button_prog2_Click"/>       
            <Button x:Name="button_close" Content="Chiudi" Click="Button_close_Click"/>
        </Grid>

    Thanks for help.

    Monday, January 7, 2019 9:20 AM

Answers

  • Hi,

    try the code:

    vb code:

    Imports System.Threading
    
    Class MainWindow
        Private Async Sub Button_prog1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            button_prog1.IsEnabled = False
            Dim progress = New Progress(Of Integer)(Function(value) __Assign(progress1.Value, value))
            Await Task.Run(Sub()
    
                               For i As Integer = 0 To 100 - 1
                                   CType(progress, IProgress(Of Integer)).Report(i)
                                   Thread.Sleep(50)
                               Next
                           End Sub)
            button_prog1.IsEnabled = True
            progress1.Value = 0
        End Sub
    
        Private Async Sub Button_prog2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            button_prog2.IsEnabled = False
            Dim progress = New Progress(Of Integer)(Sub(value) __Assign(progress2.Value, value))
            Await Task.Run(Sub()
    
                               For i As Integer = 0 To 100 - 1
                                   CType(progress, IProgress(Of Integer)).Report(i)
                                   Thread.Sleep(50)
                               Next
                           End Sub)
            button_prog2.IsEnabled = True
            progress2.Value = 0
        End Sub
    
        Private Sub Button_close_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            System.Windows.Application.Current.Shutdown()
        End Sub
    
        Public Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    
    End Class
    


    xmal code:

     <Grid>
            <Grid.RowDefinitions >
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ProgressBar Name="progress1" Minimum="0" Maximum="100" Grid.Row="0"/>
            <ProgressBar Name="progress2" Minimum="0" Maximum="100" Grid.Row="1"/>
            <Button x:Name="button_prog1" Content="Start progress 1" Click="Button_prog1_Click" Grid.Row="2"/>
            <Button x:Name="button_prog2" Content="Start progress 2"  Click="Button_prog2_Click" Grid.Row="3"/>
            <Button x:Name="button_close" Content="Chiudi" Click="Button_close_Click" Grid.Row="4"/>
        </Grid>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 10:08 AM

All replies

  • Hi,

    try the code:

    vb code:

    Imports System.Threading
    
    Class MainWindow
        Private Async Sub Button_prog1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            button_prog1.IsEnabled = False
            Dim progress = New Progress(Of Integer)(Function(value) __Assign(progress1.Value, value))
            Await Task.Run(Sub()
    
                               For i As Integer = 0 To 100 - 1
                                   CType(progress, IProgress(Of Integer)).Report(i)
                                   Thread.Sleep(50)
                               Next
                           End Sub)
            button_prog1.IsEnabled = True
            progress1.Value = 0
        End Sub
    
        Private Async Sub Button_prog2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            button_prog2.IsEnabled = False
            Dim progress = New Progress(Of Integer)(Sub(value) __Assign(progress2.Value, value))
            Await Task.Run(Sub()
    
                               For i As Integer = 0 To 100 - 1
                                   CType(progress, IProgress(Of Integer)).Report(i)
                                   Thread.Sleep(50)
                               Next
                           End Sub)
            button_prog2.IsEnabled = True
            progress2.Value = 0
        End Sub
    
        Private Sub Button_close_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            System.Windows.Application.Current.Shutdown()
        End Sub
    
        Public Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    
    End Class
    


    xmal code:

     <Grid>
            <Grid.RowDefinitions >
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ProgressBar Name="progress1" Minimum="0" Maximum="100" Grid.Row="0"/>
            <ProgressBar Name="progress2" Minimum="0" Maximum="100" Grid.Row="1"/>
            <Button x:Name="button_prog1" Content="Start progress 1" Click="Button_prog1_Click" Grid.Row="2"/>
            <Button x:Name="button_prog2" Content="Start progress 2"  Click="Button_prog2_Click" Grid.Row="3"/>
            <Button x:Name="button_close" Content="Chiudi" Click="Button_close_Click" Grid.Row="4"/>
        </Grid>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 10:08 AM
  • Thanks very much Alex, it works like in c#

    Best regards,

    Leo

    Monday, January 7, 2019 1:28 PM