By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

App "Freezes" when getting atom feed

P: n/a
I am currently working an an app that will post and edit blogs on
blogger. Right now I have it so I can recive a list of the blogs and
the content of those blogs using Atomizer. This is my first program
where I am accessing data over the net.When I go to retrieve the list
of blogs my program acts like it is locked up/frozen for a few seconds
and then when it is done it goes back to normal. Does anyone know how I
would get rid of the freezing? Here is the code where the freeze
occurs.

public void getBlogs(string[] args)
{
string atomEndPoint = "https://www.blogger.com/atom/";

generatorType generator = new generatorType();
generator.url = "http://www.mywebsite.com";
generator.Value = "myappname";
generator.version = "0.1";

Atom atom = Atom.Create(new Uri(atomEndPoint), generator,
"username", "password");

service[] services = atom.GetServices();
foreach (service service in services)
{
if (service.srvType.Equals(serviceType.feed))
{

feedType feed = atom.GetFeed(service.postURL);

foreach (entryType entry in feed.entries)
{
blogSet.Blogs.AddBlogsRow(entry.id.ToString(),
entry.title.ToString(), entry.issued.ToString(),
entry.author.ToString(), entry.contentValue.Text);
}
}
}

}

Feb 6 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Brady,

Are you doing this in response to a UI event, like a button click? If
you are, chances are you are running the code on the UI thread.

To make a long story short, when your program runs, it processes
messages in a loop for the length of the program. These messages are sent
to and from windows to tell them when to do things, and send notifications.

Now, when you write code in response to a button click, your code has to
process before any more messages can be processed. This is what is causing
the "freezing".

To get around this, you would want to run the code in another thread.
This is quite simple to do. However, what you have to be careful of is when
you want to send notifications back to the thread (i.e. update the UI). You
can't make calls to do this from the thread you did the downloading from.

In order to do this, you have to create a method which will perform your
UI work. You then have to create a delegate which shares the signature of
your method. Then, from your UI thread, you can call the Invoke method on a
control, passing the delegate (pointing to your UI update method) and your
parameters. The method will then invoke the delegate passed in and then
invoke it on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brady Love" <op******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I am currently working an an app that will post and edit blogs on
blogger. Right now I have it so I can recive a list of the blogs and
the content of those blogs using Atomizer. This is my first program
where I am accessing data over the net.When I go to retrieve the list
of blogs my program acts like it is locked up/frozen for a few seconds
and then when it is done it goes back to normal. Does anyone know how I
would get rid of the freezing? Here is the code where the freeze
occurs.

public void getBlogs(string[] args)
{
string atomEndPoint = "https://www.blogger.com/atom/";

generatorType generator = new generatorType();
generator.url = "http://www.mywebsite.com";
generator.Value = "myappname";
generator.version = "0.1";

Atom atom = Atom.Create(new Uri(atomEndPoint), generator,
"username", "password");

service[] services = atom.GetServices();
foreach (service service in services)
{
if (service.srvType.Equals(serviceType.feed))
{

feedType feed = atom.GetFeed(service.postURL);

foreach (entryType entry in feed.entries)
{
blogSet.Blogs.AddBlogsRow(entry.id.ToString(),
entry.title.ToString(), entry.issued.ToString(),
entry.author.ToString(), entry.contentValue.Text);
}
}
}

}

Feb 6 '06 #2

P: n/a
Thank you for your reply, Do you know of any good online tutorials that
would show how I would do this?

Thanks again.

Feb 6 '06 #3

P: n/a
I figured out how to create the thread and use it. But the problem is
now as you mentioned, updating the UI.

" In order to do this, you have to create a method which will
perform your
UI work. You then have to create a delegate which shares the signature
of
your method. Then, from your UI thread, you can call the Invoke method
on a
control, passing the delegate (pointing to your UI update method) and
your
parameters. The method will then invoke the delegate passed in and
then
invoke it on the UI thread. "

Do you have any tutorials on that?

Feb 6 '06 #4

P: n/a
In your simple example, a call to Application.DoEvents would be
sufficient.

Feb 6 '06 #5

P: n/a
Brady,

I don't have one offhand, but here is an example.

Say you needed to update the Text property of a control with a string
generated/retrieved in the thread. Create a method to do it, like this:

private void UpdateText(string val)
{
// Set the text.
textControl.Text = val;
}

Now, create a delegate with the same signature:

delegate void UpdateTextDelegate(string val);

Then, in your thread, you would do this:

// Call UpdateText on the UI thread.
textControl.Invoke(new UpdateTextDelegate(UpdateText), new
object[]{someString});
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brady Love" <op******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I figured out how to create the thread and use it. But the problem is
now as you mentioned, updating the UI.

" In order to do this, you have to create a method which will
perform your
UI work. You then have to create a delegate which shares the signature
of
your method. Then, from your UI thread, you can call the Invoke method
on a
control, passing the delegate (pointing to your UI update method) and
your
parameters. The method will then invoke the delegate passed in and
then
invoke it on the UI thread. "

Do you have any tutorials on that?

Feb 6 '06 #6

P: n/a
And it would also be horribly wrong...

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
In your simple example, a call to Application.DoEvents would be
sufficient.

Feb 6 '06 #7

P: n/a
Hi,

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
In your simple example, a call to Application.DoEvents would be
sufficient.


And wrong too, the correct way is to create a thread and do the processing
there. Calling DoEvents can provoke some very funky problems, very hard to
solve !

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 6 '06 #8

P: n/a
It seems to still freeze up for a second, here is the code I have now:

delegate void getBlogsDelegate();

protected void getBlogs()
{
string atomEndPoint = "https://www.blogger.com/atom/";

generatorType generator = new generatorType();
generator.url = "http://www.mywebsite.com";
generator.Value = "myappname";
generator.version = "0.1";

Atom atom = Atom.Create(new Uri(atomEndPoint), generator,
"username", "pass");

service[] services = atom.GetServices();
foreach (service service in services)
{
if (service.srvType.Equals(serviceType.feed))
{

feedType feed = atom.GetFeed(service.postURL);

foreach (entryType entry in feed.entries)
{

this.blogSet.Blogs.AddBlogsRow(entry.id.ToString() ,
entry.title.ToString(), entry.issued.ToString(),
entry.author.ToString(), entry.contentValue.Text);
}
}
}
}

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{

td = new Thread(new ThreadStart(doGetBlogs));
td.Start();
}

private void doGetBlogs()
{
lstBlogs.Invoke(new getBlogsDelegate(getBlogs));
}

Is this the correct usage of the delegate and thread? Im not so sure (I
am very beginner at progoramming and threads are all new to me today)
Thanks again for your help.

Feb 6 '06 #9

P: n/a
Brady,

No, this is not. You don't want to call Invoke in your thread to
perform the whole operation, because this will cause the operation to just
be executed in the UI thread, which is the problem in the first place.

Rather, you want to call Invoke in your thread when you make this call:
this.blogSet.Blogs.AddBlogsRow(entry.id.ToString() ,
entry.title.ToString(), entry.issued.ToString(),
entry.author.ToString(), entry.contentValue.Text);
Because that seems to be the only thing updating the UI.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brady Love" <op******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... It seems to still freeze up for a second, here is the code I have now:

delegate void getBlogsDelegate();

protected void getBlogs()
{
string atomEndPoint = "https://www.blogger.com/atom/";

generatorType generator = new generatorType();
generator.url = "http://www.mywebsite.com";
generator.Value = "myappname";
generator.version = "0.1";

Atom atom = Atom.Create(new Uri(atomEndPoint), generator,
"username", "pass");

service[] services = atom.GetServices();
foreach (service service in services)
{
if (service.srvType.Equals(serviceType.feed))
{

feedType feed = atom.GetFeed(service.postURL);

foreach (entryType entry in feed.entries)
{

this.blogSet.Blogs.AddBlogsRow(entry.id.ToString() ,
entry.title.ToString(), entry.issued.ToString(),
entry.author.ToString(), entry.contentValue.Text);
}
}
}
}

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{

td = new Thread(new ThreadStart(doGetBlogs));
td.Start();
}

private void doGetBlogs()
{
lstBlogs.Invoke(new getBlogsDelegate(getBlogs));
}

Is this the correct usage of the delegate and thread? Im not so sure (I
am very beginner at progoramming and threads are all new to me today)
Thanks again for your help.

Feb 6 '06 #10

P: n/a
How would I do that? I am having trouble understanding where it goes
exactly and what it the line of code should be. Sorry to be a pain.

Feb 6 '06 #11

P: n/a
I found this example, is this what you are talking about?

http://msdn2.microsoft.com/a1hetckb(en-US,VS.80).aspx

Feb 6 '06 #12

P: n/a
Brady,

Yes, it is.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brady Love" <op******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I found this example, is this what you are talking about?

http://msdn2.microsoft.com/a1hetckb(en-US,VS.80).aspx

Feb 7 '06 #13

P: n/a
Okay I will see if i can get this working, Thanks.

Feb 7 '06 #14

P: n/a
SP

"Brady Love" <op******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I found this example, is this what you are talking about?

http://msdn2.microsoft.com/a1hetckb(en-US,VS.80).aspx


If you are using .Net 2.0 there is a BackgroundWorker object that you should
take a look at.

SP
Feb 7 '06 #15

P: n/a
Yay I did it! I was totally using the delegate completely wrong. This
is the document that helped me figure it out if anyone else is in the
same boat I was.

http://www.agiledeveloper.com/articl...oundWorker.pdf

Thanks again for all your guys help and pointing me in the right
direction.

Feb 7 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.