I have a thread on form1 that is performing a check and sending an email if the check proves to be true..The email that I want to use is located in a textbox on Form2.
I've tried setting a string inside my thread to the textbox on form2 but it still doesn't seam to be seeing the email address for sending. After thinking some more I don't think you can just call a variable on another form inside a thread. Am I correct on this?
How can I go about getting the email address string on form2 into the thread on form1?
Threads aren't "on a form". A thread is basically an area where you can execute a series of instructions. Each thread can only execute one instruction at a time. If you have multiple threads then you can execute multiple series of instructions simultaneously. Each thread can execute instructions from anywhere. You might have two threads and they can both execute code form two different forms.
You're running into two well known issues. The first is the fact that you cannot safely perform an operation on a control from any thread other than the one it was created on. When you run an application the system provides a thread to execute it on. This is usually known as the "main thread" or, in the case of a GUI application, the "UI thread". That second name comes from the fact that that is the thread on which your UI elements, i.e. forms and controls, are created on so that is the only thread on which it is safe to access them.
The second issue is the fact that, to avoid running into the first issue, default instances of forms in VB are thread specific. Just like any other objects, in order to use forms in VB you must create them first. Just like any other objects, you can do that using the New keyword to invoke a constructor of the class. Forms also have a feature where you can simply use the class name and the system will either create the object for you or else provide one that it created earlier. E.g.
'Explicitly create an instance of the Form1 class.
Dim f1 As New Form1
'Display the instance just created.
f1.Show()
'Display the default instance of the Form2 class.
Form2.Show()
You are most likely using the default instance but, as you are using it on a secondary thread, the system creates a new object specific to that thread rather than returning the one it created previously on the main thread. Your code is actually getting the email address from the TextBox, but it's doing so from a different form to the one that you displayed to the user. You need to keep a reference to the original form and then use it in the secondary thread.
To be honest, I'm not really sure why you would be using a secondary thread at all anyway. Is this just a learning exercise? I would think everything should be done on the one thread until it comes to actually sending the email. At that point, you can call SendAsync instead of Send on your SmtpClient and it will handle the multi-threading internally.
If you really want to stick with implementing the multi-threading yourself then I'm fairly certain that your code is going to need a reasonable amount of modification. In that case, please post the relevant code and we can help you rearrange it appropriately.
hey jmcil..thanks for your response. You are correct - the thread is running in the background. Also, I think I am going to try making a sub of its own that pulls that information from form2..and then reference that sub from inside that thread. I think this will fix this issue..thanks!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.