Solved

Threading with a function returning value to windows form control

Posted on 2013-12-17
4
997 Views
Last Modified: 2014-02-21
Hi all,
I'm trying to create a thread that passes parameters into a function. the return value needs to be set to a label control.  I'm having a problem figuring out how to do this.

the function looks like this:
lblFILESIZE.Text = CONVERT_FILESIZE(GET_FILESIZE(txtSOURCE.Text, COPY_TYPE$), False)

any help would be greatly appreciated.
thanks,
John.
0
Comment
Question by:Thunder724
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39726245
Well, you can't assign directly the results from a Thread to a label control. First of all, the thread has no results. You can create a Sub or a Function and execute it into a separate thread; once done this, you can monitorize the thread to check when it's finished. From the thread itself, you'll need to store any result you want to check once finished outside of the thread context. You can store it in a variable, for example.

Hope that helps.
0
 
LVL 4

Author Comment

by:Thunder724
ID: 39726711
could you provide an example?
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39726739
Imports System.Threading

'At form level:
Private _myThread As Thread = Nothing
Private _myVariable As String = String.Empty

'When you want to start your thread
_myThread = New Thread(AddressOf MiThreadSub)
_myThread.Start()
'Monitorize thread
While _myThread.IsAlive()
    Application.DoEvents() 'Or, better, check for your thread alive status into a timer
End While
'Once outside of the while...end while, your thread has finished
'Now you can check your _myVariable status and put it into a label or wherever you want
Me.Label1.Text = _myVariable

'What your thread does:
Private Sub MiThreadSub()
    '... do something
    'If the thread is executing inside a Windows Form, you cannot access Form objects
    'such as labels, etc.
    'But you can store values into form variables
    'For example:
    _myVariable = "Hello"
End Sub

Open in new window


Note that this code doesn't work if you simply paste it into a Form. For example, the "When you want to start your thread" block needs to be written somewhere it can execute, for example, a button's click event.

Hope that helps.
0
 
LVL 4

Author Comment

by:Thunder724
ID: 39727129
I figured it out.
I created a backgroundworker object
I created a variable to hold the result from the function within the backgroundworker .DoWork procedure.
I created a backgroundworker .RunWorkerCompleted procedure where I set that variable to the object it is meant for.

thanks,
John.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question