|
-
May 1st, 2013, 05:43 PM
#24
Thread Starter
Hyperactive Member
Re: Shared functions
Let me try and explain, When I wrote the code, it was my understanding (which I still believe to be correct) that if you run all the code on the UI thread, when long running code is processing, the UI effectively freezes until the code has finished running (or you allow the UI to refresh).
Hence, whenever an option is selected that processes some code, I start a new thread. This then does not lock the UI.
The thread that is running can not however interact directly with the UI as they are running in different threads. I have always been told that cross thread processing is not a good idea. What you should do is check to see if its the same thread and invoke it.
As such I have written a lot of different routines to interact with the UI from different threads. A few examples are below.
Code:
Private Delegate Sub DThread_InvalidateInvoker(ByVal ctl As Form)
Private Sub DThread_Invalidate(ByVal CTRL As Form)
If CTRL.InvokeRequired Then
CTRL.Invoke(New DThread_InvalidateInvoker(AddressOf DThread_Invalidate), CTRL)
Else
CTRL.Invalidate()
End If
End Sub
----------------
Private Delegate Sub DThread_UpdateInvoker(ByVal ctl As Form)
Private Sub DThread_Update(ByVal CTRL As Form)
If CTRL.InvokeRequired Then
CTRL.Invoke(New DThread_UpdateInvoker(AddressOf DThread_Update), CTRL)
Else
CTRL.Update()
End If
End Sub
-----------
Private Delegate Sub DThread_ToolstripMenuItemRemoveInvoker(ByVal menuItemName As ToolStripMenuItem, ByVal item As ToolStripMenuItem)
Private Sub DThread_ToolstripMenuItemRemove(ByVal menuItemName As ToolStripMenuItem, ByVal item As ToolStripMenuItem)
If Me.InvokeRequired Then
Me.Invoke(New DThread_ToolstripMenuItemRemoveInvoker(AddressOf DThread_ToolstripMenuItemRemove), menuItemName, item)
Else
menuItemName.DropDownItems.Remove(item)
End If
End Sub
-------------
Private Delegate Sub DThread_ToolstripMenuItemvisibleInvoker(ByVal menuItemName As ToolStripMenuItem, ByVal value As Boolean)
Private Sub DThread_ToolstripMenuItemVisible(ByVal menuItemName As ToolStripMenuItem, ByVal value As Boolean)
If Me.InvokeRequired Then
Me.Invoke(New DThread_ToolstripMenuItemvisibleInvoker(AddressOf DThread_ToolstripMenuItemVisible), menuItemName, value)
Else
menuItemName.Visible = value
End if
End sub
It is these that I am trying to put into a module so that I can use them for all applications that I write. They all however reference me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|