Solved

Threading with a function returning value to windows form control

Posted on 2013-12-17
4
1,093 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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