473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

A strategy for creating a thread-safe sub or function

!NoItAll
297 100+
Creating an application with multiple threads can be challenging. If you need to create new threads to get things done in the background (to keep your UI responsive for example) then you will find you need to move some, or all of your cpu intensive routines into their own, or at least separate threads. This article will not go extensively into HOW to create these new threads, suffice to say you can employ the backgroundworker to make this happen.

In many cases your routines may need to update the UI and because of this your application will wag it's bony finger at you with: "Cross-thread operation not valid: ..."

While tempting, you really should avoid the CheckforillegalCrossThreadCalls = True on your form(s) to force your program to ignore illegal cross thread calls. Doing so may well cause you lots of pain down the road. Instead I've become a fan of a relatively simple strategy.

To safely update the UI from another thread a delegate is required. When I run into the need for this I create a delegate directly above the sub or function that will be called from outside the UI thread (with the background worker for example).

FYI - all code you put into the form, or call directly from the form will run in the same thread as the UI - thus potentially slowing down your UI.

The delegate will look something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Delegate Sub del_MySub(MyArgument as MyType)
This delegate will be used to marshal the call across the threads safely.

Now, inside the sub (or function) I test the call to see if it is necessary to employ the delegate. It looks something like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub MySub(MyArgument as MyType)
  2.    If MyUIControl.InvokeRequired Then
  3.       MyUIControl.Invoke(New del_MySub(AddressOf MySub), MyArgument)
  4.    Else
  5.       MyUIControl.Property = MyArgument
  6.    End if
  7. End Sub
  8.  
So inline the code looks like this...

Expand|Select|Wrap|Line Numbers
  1. Private Delegate Sub del_MySub(MyArgument as MyType)
  2.  
  3. Private Sub MySub(MyArgument as MyType)
  4.    If MyUIControl.InvokeRequired Then
  5.       MyUIControl.Invoke(New del_MySub(AddressOf MySub), MyArgument)
  6.    Else
  7.       MyUIControl.Property = MyArgument
  8.    End if
  9. End Sub
  10.  
So what essentially happens is that the initial call to the sub from another thread is made, but the .invokerequired method marshals the call back to the same sub through the delegate (where the call to .invokerequired will return false of course). It then unwinds when the job is complete.
Jan 12 '16 #1
0 3354

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jozef | last post by:
Hello, I'm trying to create a central function that runs a connection to an SQL Server database. The connection etc works, but when I try to call it, I get an error saying "Runtime-Error 91:...
1
by: Rob | last post by:
Hi, I am creating a DataList with many CheckBoxes. When I click the Update button the UpdateCommand fires and my code looks at each CheckBox and determines if it is checked or not. The way I...
19
by: Gary Kahrau | last post by:
I want to create a function and can only get half way to my goal. dim sStr as string sStr = Entry(2,"a,b,c,d") ' Sets sStr = "b" Entry(2,sStr) = "B" ' Sets sStr = "a,B,c,d"...
2
by: Matthew T. O'Connor | last post by:
I was trying to create a sql function today (see below) using postgresql 7.3.3. I don't see how to get around this error, anyone have any suggestions? Thanks much, Matthew tocr=# CREATE...
3
by: Bundy | last post by:
Hi I am trying to create a function that adds to specific array, then checks the array to determine whether to alert 'Yes' or 'No'. Note this is a simplified version of the function I have...
3
by: Lynn McGuire | last post by:
I am creating a function prototype for a DLL entry point. I dynamically load the DLL at runtime and then map the entry points. typedef SP_STATUS SP_API (* myRNBOsproReadProc) ( SP_IN...
1
by: manu1001 | last post by:
I've an old C function that calls a function pointed by a global function pointer. How do I get it to call a member function of a class object determined at run-time. It'll be complied in a C++...
9
by: Steve B | last post by:
Hello, I am trying to create a math function in a module and then call the function from a VB form. For some reason I get errors each time I try. on the VB Form A = 1 B = 2 how do I...
3
by: fuchsia555 | last post by:
hi is there a code to crop bottom border from an image when creating thumbnail for this image without crop the original image , but just crop 15px bottom border for creating the thumbnails for the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.