-
Feb 3rd, 2015, 04:52 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to get information from another thread?
I'm working on a program where I have a listview in details view, where the user adds entries to be processed. Any time an entry is added it will become a new item at the largest index. I'd like to have a background worker be able to work through the list by pulling the information from index 0, work with that info for a while and then remove the listviewitem in index 0 (moving up all higher indexes) before starting again. I know I can invoke the control (or use reportprogress) in order to remove the item at index 0, but I do not know how to get the info in the first place.
All the instructions I have been able to find by searching had to do with getting information to the UI thread, not the other way around. I also saw an example where it suggested using a variable to keep that info, but that can't be the only way to achieve this outcome. It seems that explicitly defining each piece of information I need to access on another thread can be tedious, and hard to maintain in larger applications, just like how it would be tedious to copy info to variables in another form, rather than access them via that forms namespace. I would think that there would be a method, or function written for this purpose.
Any Ideas?
-
Feb 3rd, 2015, 04:57 PM
#2
Re: How to get information from another thread?
If you are using the BGW then pass the argument. JMC has a tutorial in the codebank section. Reportprogress reports progress as the name states. Should not be used to add and remove entries. It sounds like the BGW class is not for you.
-
Feb 3rd, 2015, 05:25 PM
#3
Re: How to get information from another thread?
You can do that using a delegate function.
Try this code with a textbox, label and backgroundworker on a form. It reads text from a textbox and send it to a label from inside the background worker's DoWork event handler.
Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do
Dim s As String = getTextBoxText()
Call setLabelText(s)
Threading.Thread.Sleep(250)
Loop
End Sub
Private Delegate Function getTextBoxTextDelegate() As String
Private Function getTextBoxText() As String
Dim ret As String = Nothing
If Me.InvokeRequired Then
ret = Me.Invoke(New getTextBoxTextDelegate(AddressOf getTextBoxText)).ToString
Else
ret = Me.TextBox1.Text
End If
Return ret
End Function
Private Delegate Sub setLabeltextDelegate(s As String)
Private Sub setLabelText(s As String)
If Me.InvokeRequired Then
Me.Invoke(New setLabeltextDelegate(AddressOf setLabelText), s)
Else
Me.Label1.Text = s
End If
End Sub
End Class
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Feb 3rd, 2015, 05:44 PM
#4
Re: How to get information from another thread?
Dont use the BGW with custom delegates. To simply invoke, get a control it would be
vb Code:
Imports System.Threading.Tasks Public Class Form1 Private Sub Foo() Dim t As New task(Sub() SetText()) t.Start() End Sub Private Sub SetText() Me.Invoke(Sub() Me.Text = "Hello World") End Sub Private Function GetTextBoxText() As String Return Me.Invoke(Function() Me.Text) End Function End Class
Last edited by ident; Feb 3rd, 2015 at 06:06 PM.
-
Feb 3rd, 2015, 07:00 PM
#5
Thread Starter
Hyperactive Member
Re: How to get information from another thread?
Originally Posted by kebo
You can do that using a delegate function.
Code:
Public Class Form1
Private Delegate Function getTextBoxTextDelegate() As String
Private Function getTextBoxText() As String
Dim ret As String = Nothing
If Me.InvokeRequired Then
ret = Me.Invoke(New getTextBoxTextDelegate(AddressOf getTextBoxText)).ToString
Else
ret = Me.TextBox1.Text
End If
Return ret
End Function
End Class
This right here was what I needed to see. I didn't know (and couldn't find an example that showed) that you could get a return from invoke. Although this example used .tostring I was able to get my exact situation working by casting the invoke to the proper type.
Thank you for the help!
Thanks again guys.
Last edited by FunkyDexter; Feb 4th, 2015 at 03:52 AM.
-
Feb 4th, 2015, 03:52 AM
#6
Re: [RESOLVED] How to get information from another thread?
I've just removed a bunch of posts from this thread because they were overly heated and/or off topic. StepDragon has started thread looking for a solution to a specific problem he's facing. We owe him the courtesy of trying to deliver that solution with a minimum of extraneous noise. Please bear that in mind.
StepDragon, I'm also editing your last post to keep things consistent but just wanted to say that I thoroughly agree with the sentiments you expressed. I shall preserve one particular quote for posterity because I feel it sums up correct behaviour more eloquently than I ever could:-
"If you feel someone is not being beneficial to the conversation, please don't go off topic yourself. Report the post, and let the mods sort it out."
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|