[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Threading with a function returning value to windows form control

Posted on 2013-12-17
4
Medium Priority
?
1,167 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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