Begininvoke needed?

K

kimiraikkonen

Hi,
I have a small app which sends using .NET's SmtpClient Class and sends
well. But the problem is that i placed a marquee-type progress bar to
have an animation and hide by default.

Then i placed "progressbar1.show" code inside send button, but the
problem is that progress bar is shown after the mail is sent / after
operation ends. I want my form displays progress bar at the same time
with mail is being sent.

And during sending process form seems hanged and locked but still
operation is ended successfully after a while. I have searched for
similiar threads with no help and i think i need to insert a kind of
invoking / delegation part into my code to allow my form tointeract
during operation. My code:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _btnSubmit.Click
ProgressBar1.Show()
Dim message As New MailMessage(txtfrom.Text, txtto.Text,
txtsubject.Text, txtbody.Text)
Dim emailClient As New SmtpClient("smtp.gmail.com")
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!")

End Sub


I have no experience about "invoking types and delagates" and i want
to learn briefly.

If you help and explain about the code i'd be so glad.

Thanks!
 
A

Armin Zingler

kimiraikkonen said:
Hi,
I have a small app which sends using .NET's SmtpClient Class and
sends well. But the problem is that i placed a marquee-type progress
bar to have an animation and hide by default.

Then i placed "progressbar1.show" code inside send button, but the
problem is that progress bar is shown after the mail is sent / after
operation ends. I want my form displays progress bar at the same
time with mail is being sent.

And during sending process form seems hanged and locked but still
operation is ended successfully after a while. I have searched for
similiar threads with no help and i think i need to insert a kind of
invoking / delegation part into my code to allow my form tointeract
during operation. My code:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles _btnSubmit.Click
ProgressBar1.Show()
Dim message As New MailMessage(txtfrom.Text, txtto.Text,
txtsubject.Text, txtbody.Text)
Dim emailClient As New SmtpClient("smtp.gmail.com")
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!")

End Sub


You have to put the task (sending) into another thread if you want to keep
the UI (user interface) busy.

Does the ProgressBar1 really reflect the progress of sending? Or is it just
an animation? If you had to reflect the progress, you would have to call
ProgressBar1's BeginInvoke method from the working thread.

I have no experience about "invoking types and delagates" and i want
to learn briefly.

A delegate is an object containing a reference to a certain
method/procedure. You can pass a delegate around, and the one who gets the
delegate is able to call the method that is referenced in the delegate.

BeginInvoke makes the passed method (wrapped in a delegate) execute in the
UI thread. The UI thread then can update the progress bar, which is not
allowed from the worker thread.

BeginInvoke is (like) sending a message to the UI thread. The message says:
"execute this method". The UI thread will receive the message and do like
instructed (as soon as it is idle).

If you help and explain about the code i'd be so glad.

Please use google to search for "BeginInvoke" in this group. I think it has
been explained many times.


Armin
 
S

Stephany Young

It's nothing to do with 'invoking' and/or 'delegates'.

It is simply that your UI thread is being blocked from the point where you
click the 'submit' button until you have dismissed the MessageBox dialog.
 
A

Armin Zingler

Stephany Young said:
It's nothing to do with 'invoking' and/or 'delegates'.

It is simply that your UI thread is being blocked from the point
where you click the 'submit' button until you have dismissed the
MessageBox dialog.

Thx Stephany, a good example how I can learn to make shorter answers. :)


Armin
 
T

Teemu

kimiraikkonen said:
Hi,
I have a small app which sends using .NET's SmtpClient Class and sends
well. But the problem is that i placed a marquee-type progress bar to
have an animation and hide by default.

Then i placed "progressbar1.show" code inside send button, but the
problem is that progress bar is shown after the mail is sent / after
operation ends. I want my form displays progress bar at the same time
with mail is being sent.

BackroundWorker might be the best solution for this. When you press the
button, make the ProgressBar visible and start BackroundWorker which sends
the e-mail message.

-Teemu
 
K

kimiraikkonen

You have to put the task (sending) into another thread if you want to keep
the UI (user interface) busy.
How?

Does the ProgressBar1 really reflect the progress of sending? Or is it just
an animation? If you had to reflect the progress, you would have to call
ProgressBar1's BeginInvoke method from the working thread.

It's just an animation, doesn't calculates anything based on mail
sending (would be great if so)
A delegate is an object containing a reference to a certain
method/procedure. You can pass a delegate around, and the one who gets the
delegate is able to call the method that is referenced in the delegate.

BeginInvoke makes the passed method (wrapped in a delegate) execute in the
UI thread. The UI thread then can update the progress bar, which is not
allowed from the worker thread.

BeginInvoke is (like) sending a message to the UI thread. The message says:
"execute this method". The UI thread will receive the message and do like
instructed (as soon as it is idle).

Sorry, but i still didn't understand what should i insert into my code
described in my first post to make visible marquee animated progress
bar control while the mail is being sent.
Please use google to search for "BeginInvoke" in this group. I think it has
been explained many times.

I always search but not always gives point solutions, beleive :)

I hope to be helped,

Thanks...
 
K

kimiraikkonen

BackroundWorker might be the best solution for this. When you press the
button, make the ProgressBar visible and start BackroundWorker which sends
the e-mail message.

-Teemu

Teemu, how can i use Background Worker? Could you give a sample code
for my first usage?

Thanks.
 
K

kimiraikkonen

BackroundWorker might be the best solution for this. When you press the
button, make the ProgressBar visible and start BackroundWorker which sends
the e-mail message.

-Teemu

Hi, Bgworker is a good idea i tried and worked for showing progress
bar, but i want to hide it when sending process ends by clicking
Messagebox("sent successfully")

I got "crossthread operation not valid....etc". I think i had to
marshal it / use "addressof. " Am i right?

How can i do this (hide progressbar after mail sending finishes)?

Thanks.
 
K

kimiraikkonen

OK, don't mind. I found solution for hiding progress bar by putting
"progressbar1.hide" inside background worker's "RunWorkerCompleted"
event.
 
A

Armin Zingler

kimiraikkonen said:

Sorry, I am not able to teach multi threading here because it is a broad
topic. The best place is to look into the documentation and learn. That's
what I had to do, too. What I can do is look for the links...here they are:

http://msdn2.microsoft.com/en-us/library/eed6swsx.aspx
http://msdn2.microsoft.com/en-us/library/3e8s7xdd.aspx

It's just an animation, doesn't calculates anything based on mail
sending (would be great if so)


Sorry, but i still didn't understand what should i insert into my
code described in my first post to make visible marquee animated
progress bar control while the mail is being sent.

It's not only about inserting anything into your code. You first have to
learn and understand. Read the links above.

Sorry, I only tried to explain what BeginInvoke does (your question in the
subject BTW). I hope it was comprehensive even though you were looking for a
complete solution for your problem.
I always search but not always gives point solutions, beleive :)

You mean, nobody writes the code for you? Sure.

Concerning "begininvoke" - what I was referring to - I am pretty sure you
will find an appropriate answer in this group, because there is no other
reason for using BeginInvoke but multi threading and having the UI thread do
something.


Armin
 
K

kimiraikkonen

Sorry, I am not able to teach multi threading here because it is a broad
topic. The best place is to look into the documentation and learn. That's
what I had to do, too. What I can do is look for the links...here they are:

http://msdn2.microsoft.com/en-us/li...dn2.microsoft.com/en-us/library/3e8s7xdd.aspx






It's not only about inserting anything into your code. You first have to
learn and understand. Read the links above.

Sorry, I only tried to explain what BeginInvoke does (your question in the
subject BTW). I hope it was comprehensive even though you were looking for a
complete solution for your problem.



You mean, nobody writes the code for you? Sure.

No, i didn't mean this.
Concerning "begininvoke" - what I was referring to - I am pretty sure you
will find an appropriate answer in this group, because there is no other
reason for using BeginInvoke but multi threading and having the UI thread do
something.

Armin

Thanks for the links.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top