473,513 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Async Execution and Thread-Safety

hi @all,

I've have this working piece of code in an async callback function used:

private void SetCount()
{
if (listView.InvokeRequired)
{
listView.Invoke((MethodInvoker)(() =SetCount()));
}
else
{
listItem.SubItems[1].Text = list.Count.ToString();
}
}

Can this produce an infinite loop under some circumstances?

I've seen similar in the net, but no comments about termination...
mfG
--stefan <--
Sep 2 '08 #1
3 2165
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:OG**************@TK2MSFTNGP04.phx.gbl...
I've have this working piece of code in an async callback function used:

private void SetCount()
{
if (listView.InvokeRequired)
{
listView.Invoke((MethodInvoker)(() =SetCount()));
}
else
{
listItem.SubItems[1].Text = list.Count.ToString();
}
}

Can this produce an infinite loop under some circumstances?
I don't think so. InvokeRequired is True when the code is executing in a
different thread from that one that created the form. The Invoke method will
marshall execution into the main thread, and therefore InvokeRequired will
become False, so the second try will get into the "else" and not produce an
infinite loop.

Sep 2 '08 #2
On Tue, 02 Sep 2008 08:05:23 -0700, Stefan Hoffmann
<st*************@explido.dewrote:
hi @all,

I've have this working piece of code in an async callback function used:

private void SetCount()
{
if (listView.InvokeRequired)
{
listView.Invoke((MethodInvoker)(() =SetCount()));
}
else
{
listItem.SubItems[1].Text = list.Count.ToString();
}
}

Can this produce an infinite loop under some circumstances?
Sure. But hopefully not in your code.

The only way it could recurse is if something in the "else" clause of the
"if" statement were to cause the SetCount() method to be called again.
You've got five different opportunities for that to happen, but it's not
likely any of them would:

-- Getting the Count property from your "list" variable
-- Calling ToString() on the result
-- Getting the SubItems property from your listItem variable
-- Indexing the result
-- Setting the Text property of the retrieved object

Assuming we're dealing with the built-in .NET classes, the first four
possibilities just shouldn't be an issue at all. Those kinds of
side-effects shouldn't be in your own code, and it's pretty much
guaranteed that they won't exist in .NET code.

As for the last, it depends a bit on the type returned by the indexer. If
you've got a way of subscribing to a TextChanged event (for example) that
is raised when the Text property is set, then you have an opportunity to
write code that is called when that assignment happens. You _could_ have
something in that code that then in turn calls SetCount() again.

But, as long as you haven't handled such an event, or in that handler you
haven't done anything that would call SetCount() again, there should be no
issues.

By the way, I really, _really_, don't like the "InvokeRequired" pattern.
I wish MSDN had never suggested it. The Invoke() method already has to do
the same check, and it correctly deals with the condition where
InvokeRequired is false by just calling the invoked delegate directly.
So, your example could much more cleanly be written like this:

private void SetCount()
{
listView.Invoke((MethodInvoker) delegate
{
listItem.SubItems[1].Text = list.Count.ToString();
});
}

I prefer the "delegate" keyword to the lambda syntax for imperatives like
this; seems more expressive to me. But of course, you can stick with the
() =syntax if you prefer.

Pete
Sep 2 '08 #3
hi Peter,

Peter Duniho wrote:
By the way, I really, _really_, don't like the "InvokeRequired"
pattern. I wish MSDN had never suggested it. The Invoke() method
already has to do the same check, and it correctly deals with the
condition where InvokeRequired is false by just calling the invoked
delegate directly. So, your example could much more cleanly be written
like this:

private void SetCount()
{
listView.Invoke((MethodInvoker) delegate
{
listItem.SubItems[1].Text = list.Count.ToString();
});
}
This is really good idea. Thanks.
mfG
--stefan <--
Sep 3 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
789
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have...
4
3843
by: Vlad | last post by:
I have implemented an asyncrhonous execution of a method using the delegate's BeginInvoke() I am trying to spin multiple worker threads in a thread pool. Here is the code: TestProcess process =...
1
2742
by: eminemabcd | last post by:
Hi, I encountered a problem trying to write a code that does the following: 1. Main GUI thread invokes a delegate using BeginInvoke. 2. A secondery thread (from the application's managed thread...
6
1838
by: gabriel.landais | last post by:
Hi, I'm currently building a parser class in JS and I have a question about variables. I retrieve XML data and then process it. After that, I process a result array (mydataarray). It looks like...
4
2463
by: Bob Badger | last post by:
Hi, Simple question (although I guess with a complicated answer). Is HTTP an async protocol? For instance, if I send a message to a c# webservice via http what is the protocol actually doing? ...
1
2276
by: jonathan | last post by:
I need to create a webpage that asynchronously loads a series of user controls onto a page. If the user control take longer than X seconds to load it should display an error message in it place....
7
5054
by: =?Utf-8?B?Q2FybG8gRm9saW5p?= | last post by:
Hi, I implemented asynchronous calls to a web resource (using HttpWebRequest) from asp.net 2.0. The request it's made asyncronously (I see that beginGetResponse returns immediately). The number...
12
3520
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
12
3444
by: =?Utf-8?B?cGI=?= | last post by:
I am having trouble doing a redirect in an async asp.net implemention. Most of the time it works, but when it doesn't it just "hangs", the browser never gets any return page. If I run it under the...
11
4006
by: nadeem_far | last post by:
Hello, I am working on a c# project using framework 1.1. Since its a console application,no windows forms are being used. I am trying to implement event driven classes that just want to raise...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.