Since I've started to use a computer, I hated everytime an application's UI stops responding for a while. However, there are some applications I see, that are doing their "heavy" jobs, yet the UI remains responsive, when the proccess is completed the result is shown allowing the user do something else meanwhile.
So, as my project is growing bigger and bigger, I started to suffer from the same issue. I think I have a general idea about the underlying logic of crossthreads. However, the sample code I have below doesn't seem to me to be the proper way of doing things. Implementing each function takes so much code. For all the functions accepting diffrent kind and number of parameters, creating a delegate, and making them invokable are very long jobs. For a already written project, this is even worse.
Here is my little template that changes the text of the form from another thread contained in another class. Sorry for the messy code, I hope it is clear enough.
Public Class Form1
Dim x As ClassA
Private Sub Form1_HandleCreated(sender As Object, e As System.EventArgs) Handles Me.HandleCreated
x = New ClassA(Me.Handle)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
x.StartAThread()
End Sub
End Class
Public Class ClassA
Dim MainForm As Form1
Sub New(handle As IntPtr)
MainForm = CType(Form1.FromHandle(handle), Form1)
End Sub
Public Sub StartAThread()
Dim newthread As New Threading.Thread(AddressOf threadsub)
newthread.IsBackground = True
newthread.Start()
End Sub
Private Delegate Sub dnoparam()
Dim dabc As New dnoparam(AddressOf abc)
Private Sub threadsub()
If MainForm.InvokeRequired Then
MainForm.Invoke(dabc)
Else
abc()
End If
End Sub
Private Sub abc()
MainForm.Text = "blabla"
End Sub
End Class
What I am asking is: Is the code above contains the true pattern of doing things? Is this a coding problem for everyone? Is there a better way? What path the professional products are following for keeping the UI as responsive as possible?