.NET Forum / Languages / VB.NET / October 2007
Progress bar for my project, multi-threading needed?
|
|
Thread rating:  |
kimiraikkonen - 23 Oct 2007 09:32 GMT Hello experts, I've been already working on a project and also asked and i've managed to create a basic Gmail mail sender, but i want to add a progressbar that shows "sending is in progress" but when i add the progressbar1.show() when sending then progressbar.hide() after sending finishes, as known well progressbar is shown after sending is finished because the main form is in use.
So, must i add multi-threading function to show my progress bar at the same while my mail is sending simultaneously? Where can i add the code of that code?
Thanks:
Imports System.Net.mail Public Class Form1 Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Try
Dim message As New MailMessage(txtfrom.Text, txtto.Text, txtsubject.Text, txtbody.Text) Dim emailClient As New SmtpClient("smtp.gmail.com") message.Priority = MailPriority.High
Dim SMTPUserInfo As New System.Net.NetworkCredential(txtSMTPUser.Text + "@gmail.com", txtSMTPPass.Text) emailClient.UseDefaultCredentials = False emailClient.Credentials = SMTPUserInfo emailClient.Port = 587 emailClient.EnableSsl = True emailClient.Send(message)
MsgBox("Mail was sent successfully", MsgBoxStyle.Information, "Success!") Catch ex As Exception MsgBox("Mail couldn't be send" + vbNewLine + "Make sure you entered correct username/password then try again later", MsgBoxStyle.Exclamation, "Fail") End Try
End Sub
End Class
iwdu15 - 23 Oct 2007 14:25 GMT it seems like using a progress bar for sending one email is a bit overkill. usually progress bars are used to show the progress of a long running task. by the time the progressbar could be re-drawn to show how far it is, the email would be sent. as for the use of multi-threading, it would be bad practice (plus it would throw an error in VS2005) to access a control from another thread. you should only access controls from the main thread. now if you still want to show a progress bar, youl need to use the forms invoke method, but as i said, IMHO its a little overkill for sending one email.
hope this helps
 Signature -iwdu15
eBob.com - 23 Oct 2007 15:10 GMT Because I am trying to multi-thread an application, I was interested in this and some other recent posts in this general area and would like to seek some additional information and clarification.
> it seems like using a progress bar for sending one email is a bit > overkill. [quoted text clipped - 9 lines] > > hope this helps Does "it would be bad practice (...) to access a control from another thread." mean it would be bad practice (etc) to access a control from a thread other than the one which created it?
Is it also the case that controls on a form should be, or must be, created by the same thread which created the form?
Thanks, Bob
Armin Zingler - 23 Oct 2007 15:22 GMT > Does "it would be bad practice (...) to access a control from > another thread." mean it would be bad practice (etc) to access a > control from a thread other than the one which created it? > > Is it also the case that controls on a form should be, or must be, > created by the same thread which created the form? Yes and yes (must be).
Armin
kimiraikkonen - 23 Oct 2007 16:32 GMT > > Does "it would be bad practice (...) to access a control from > > another thread." mean it would be bad practice (etc) to access a [quoted text clipped - 6 lines] > > Armin OK, but i haven't undestood well something solid, still trying to learn like multi-threading or working with progress bar simultaneously with the main form, could you show some example code that shows a continuous progress bar while sending/connecting is in progress? Because after i click to send button, the software tries to reach SMTP server and authenticates, then sends. That procedure usually takes much time due to slow connections, in that time the form is locked (is busy, cannot be inteacted) as well.(so that progressbar cannot be shown without special multi-threading code)
The sample code would be welcomed for running long-length processes in interaction with progress bar.
Armin Zingler - 23 Oct 2007 16:45 GMT > The sample code would be welcomed for running long-length processes > in interaction with progress bar. There are many samples in the documentation and in this and other groups, also in the web. In FW 2.0, you can use the BackgroundWorker class.
Armin
iwdu15 - 23 Oct 2007 15:29 GMT > Does "it would be bad practice (...) to access a control from another > thread." mean it would be bad practice (etc) to access a control from a > thread other than the one which created it? > > Is it also the case that controls on a form should be, or must be, created > by the same thread which created the form? yes, controls should only be accessed, created, etc from the main thread, which is the thread that the main form is on. you can have your form update controls based on info from other threads, which is how progressbars are usually used. in this case, you need to look at the forms "invoke" method.
hope this helps
 Signature -iwdu15
rowe_newsgroups - 23 Oct 2007 15:29 GMT > Does "it would be bad practice (...) to access a control from another > thread." mean it would be bad practice (etc) to access a control from a > thread other than the one which created it? > > Is it also the case that controls on a form should be, or must be, created > by the same thread which created the form? Yes, accessing a control from a thread different than the one it was created on will throw a cross-thread exception error.
Also, what multithreading topics where/are you seeking information on? For a broad overview and theory, you're best bet is to go find a book on it, but for simpler questions I (and many others I'm sure) will do what we can to help you out.
Thanks,
Seth Rowe
eBob.com - 23 Oct 2007 19:23 GMT ><snip> > Also, what multithreading topics where/are you seeking information on? [quoted text clipped - 10 lines] > on it, but for simpler questions I (and many others I'm sure) will do > what we can to help you out. Hi Seth,
You and others here (Tom Shelton, iwdu15 and Armin come immediately to mind) are expremely helpful and that is much appreciated.
I've been reading the Threading chapter in Balena which in my opinion is an excellent book. But I have not noticed in his Threading chapter any discussion of how a thread might go about updating a control on another thread's form. (On the other hand I have not read every word of the chapter yet.) I know from iwdu15's reply to this thread and others (that's thread in the ng sense!) that I need to look at Invoke and I'll be doing that ASAP. I also use the Petzold book but have to admit that I have not yet looked at what he has to say about threading.
Thanks again to all who have responded to this query.
Bob
rowe_newsgroups - 23 Oct 2007 15:26 GMT > Hello experts, > I've been already working on a project and also asked and i've managed [quoted text clipped - 41 lines] > > End Class Nothing personal, but haven't we been over this topic multiple times? It seems you are constantly asking the same type of question over and over, and Armin, I, and others keep giving you the same answers? I really hate being rude, and I apologize for the tone of the message, but it gets rather frustrating answering the same questions from the same person over and over...
Thanks,
Seth Rowe
kimiraikkonen - 23 Oct 2007 16:42 GMT > Nothing personal, but haven't we been over this topic multiple times? > It seems you are constantly asking the same type of question over and [quoted text clipped - 6 lines] > > Seth Rowe OK, but i haven't undestood well something solid, still trying to learn like multi-threading or working with progress bar simultaneously with the main form, could you show some example code that shows a continuous progress bar while sending/connecting is in progress? Because after i click to send button, the software tries to reach SMTP server and authenticates, then sends. That procedure usually takes much time due to slow connections, in that time the form is locked (is busy, cannot be inteacted) as well.(so that progressbar cannot be shown without special multi-threading code)
The sample code would be welcomed for running long-length processes in interaction with progress bar.
That's why i have asked similiar questions more than one time and if it will be hard to help/reply for you, it's OK not to get help. But thanks for the help so far in that group.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|