GoGreen PC Tune-Up™
Learn More

Insta-Install™
this is how ssl encrypt our websites
MTNCOMP | List View | Table View | myBlog (1777 Entries)
myBlog Home

Blog


GoGreen PC TuneUp - Multi-Threading, Parallel programming, MTA

by Mountain Computers Inc., Publication Date: Tuesday, January 29, 2019

View Count: 1983, Keywords: multi-threading, parallel programming, background worker, Hashtags: #multi-threading #parallelprogramming #backgroundworker



First of all, the GoGreen PC TuneUp is single threaded. GGPCTU. while I realize most of the tasks in the program are batched and scripted out and can run simultaneously, the competitors that I have really don't have the option either of threaded programming, parallel programming techniques or background worker features. So, I looked a little deeper and came up and fixed a few of the online examples that I sampled. Pretty cool stuff. take a look.
 
more to come...
 
of all the examples, I like this one the most.

source: https://www.youtube.com/watch?v=SdacuA-01_A 
 
Imports System.ComponentModel
Imports System.Threading

'https://www.youtube.com/watch?v=SdacuA-01_A

Public Class Form1

    Dim amount As Integer = 10
    Dim pics(amount) As Bitmap
    Dim sw As Stopwatch = New Stopwatch()

    Dim t As Threading.Thread

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        sw.Restart()
        sw.Start()
        For i = 0 To amount - 1
            picset(i)
        Next
        Label1.Text = sw.ElapsedMilliseconds
    End Sub

    Private Sub picset(ByVal i As Integer)
        pics(i) = New Bitmap(640, 480)
        For i2 = 0 To pics(i).Width - 1
            For i3 = 0 To pics(i).Height - 1
                pics(i).SetPixel(i2, i3, Color.Blue)
            Next
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        sw.Restart()
        sw.Start()

        Threading.Tasks.Parallel.For(0, amount - 1, AddressOf picset)

        Label2.Text = sw.ElapsedMilliseconds

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        For i = 0 To amount - 1
            picset(i)
        Next
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        label3.text = sw.ElapsedMilliseconds
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        sw.Restart()
        sw.Start()

        BackgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Threading.ThreadPool.QueueUserWorkItem(AddressOf CallThread1)

    End Sub
    Private Sub CallThread1(ByVal stateinfo As Object)
        sw.Restart()
        sw.Start()

        For i = 0 To amount - 1
            picset(i)
        Next

        'MsgBox(sw.ElapsedMilliseconds)
        Label4.Invoke(Sub() Label4.Text = sw.ElapsedMilliseconds)

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        sw.Restart()
        sw.Start()

        t = New Threading.Thread(AddressOf CallThread2)
        t.IsBackground = False
        t.SetApartmentState(Threading.ApartmentState.MTA)
        t.Start()

    End Sub
    Private Sub CallThread2()

        For i = 0 To amount - 1
            picset(i)
        Next
        'MsgBox(sw.ElapsedMilliseconds)
        Label5.Invoke(Sub() Label5.Text = sw.ElapsedMilliseconds)
    End Sub
End Class



if you found this article helpful, consider contributing $10, 20 an Andrew Jackson or so..to the author. more authors coming soon
FYI we use paypal or patreon, patreon has 3x the transaction fees, so we don't, not yet.

© 2024 myBlog™ v1.1 All rights reserved. We count views as reads, so let's not over think it.