Resolved A usercontrol on a form with long running method on BackgroundWorker

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
216
Programming Experience
10+
I have a usercontrol on a form that contains a long running method.

I need the usercontrol to show on the form for the user to click something on it.

I also need to run a method that is on the usecontrol from a BackgroundWorker that is created by the form.

I think I'm up a creek without a paddle but 've seen magic o this site before so I thought I'd ask.

Is there any way??

Edited after the two replies.

Is this different than what I said before appeared to say?
 
Last edited:
So go ahead and do that. What's the actual problem? You design a user control in exactly the same way as you design a form, so you can add a Button and a BackgroundWorker the same way as always. You then add your user control to a form in exactly the same way as any other control. It seems to me that you have just assumed that you can't do this without even trying.
 
If you're saying, without actually saying, that you want the BackgroundWorker to be part of the form then go ahead and do that. You simply call the method on the user control from the DoWork event handler. Of course, just as always, you can't access the UI directly in the DoWork event handler or any method called from it. If you're saying, without actually saying, that that is the problem then there is no solution but to restructure the code. We shouldn't have to guess at this stuff though. You need to provide a FULL and CLEAR explanation of the problem.
 
If it wasn't clear it wasn't because I didn't try. Does the edit make it any better?

It's like the usercontrol does a GetFiles that that takes some time and the form wants to use the files which also takes a while.

So the form's Backgroundworker would like to call the usercontrol method that gets the files

Before then backgroundworker is created the user has to do something to the usercontrol to select which files to get.
 
Last edited:
That's clearer than it was but I still don't see what the issue is. If you want to call a method of the user control then call it. What is stopping you calling that method like you would call any other method? Surely you know how to call a method so please describe why calling this one is a problem.
 
What is your actual problem?

Calling the method of the background worker, or understanding how a background worker works?


Or call a function on MSDN which is not much different than calling a method


As for the background worker. There is a whole section of documentation on it here

 
Perhaps if you show us the relevant code you are struggling with, we might be of better assistance. What have you tried and where are you stuck?
 
I misinterpreted the problem. It's actually the SelectedNode statement that gives a "Cross-thread" error.
The usercontrol contains a TreeView
Just as you said the method did get called.

Public Function GetFileFullPathsFromSelectedFoldersTrees() As String()

If TreeView_Folders.SelectedNode Is Nothing Then



I believe there is a insert code icon but my big cursor icon covers the text that would have shown me which one.
 
Like I said, you cannot access the UI from the DoWork event handler or any method called from it. The fact that you want to do that or you think you need to doesn't mean that you can. If you have code that may be executed on a secondary thread and that code accesses the UI then you need to restructure the code so the UI access is done on the UI thread regardless of which thread the rest of the code is executed on. For instance, if you have code like this:
VB.NET:
Dim str = TextBox1.Text
then, at the very least, you would need to change it to this:
VB.NET:
Dim str = CStr(TextBox1.Invoke(Function() TextBox1.Text))
 
Please show us the relevant code you are having a problem with?

In the user control this is the method that gets called

Public Function qGetFileFullPathsFromSelectedFoldersTrees() As String()

If TreeView_Folders.SelectedNode Is Nothing Then
...


In the form this is the calling code

Private Sub BackgroundWorker_11_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles mBackgroundWorker_11.DoWork

Dim editor As ControlTextEditor = DirectCast(e.Argument, ControlTextEditor)
Dim sourceFiePaths As String() = ControlFolderAndFileExplorer_1_11.qGetFileFullPathsFromSelectedFoldersTrees
...

If I give you, which I'd like to do, the complete worker and form code it would be pages of code.

I get a cross-thread error at the line:


If TreeView_Folders.SelectedNode Is Nothing Then
 
I've told you why that happens and I've told you how to avoid it. What's the problem?

The biggest problem is that I don't have much insight in this subject so I just barely understand the suggestions. But I believe you just told me, again, that I have to restructure the code. Something I don't expect you to do for me. And I don't have any idea how to do it myself, so I'll just have to avoid using the backgroundworker in this code.

Thanks for taking so much time with me.
 
Or you could do some research on the subject. For instance, my example uses the Invoke method of a TextBox. Did you try to find out what that method does? There is a lot of information out on the internet already. A lot of people seem to think that the only way to find out anything is to ask and have someone tell you. You can actually find information that's already out there on a subject.
 
I've used BackgroundWorked a few time with much more complex code than this without unresolved problems but this one stopped me. I looked at your code and if it solved this problem I missed it. I'll look again to see where it is applicable.

I just re-looked at your replies as realize I completely missed the invoke statement, sorry. I'll have to study that before I can say anything about it.
 
Back
Top