Solved

Threading with a function returning value to windows form control

Posted on 2013-12-17
4
934 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now