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

Threading causing results to be different, Please Help

P: n/a
I'm trying to trow in another thread into my program. What it's for is
to ping a list of IPs. When it runs the Ping portion of the code, it
assigns a value of 1 or 2 (pingable / not pingable) to a global int. It
works great, however it's slow... If I run the ping portion of my code
in a thread, it returns random values for the pingable / non pingable
values...

My code and a screenshot of is located here.
http://csharp.thinkglobaltech.com/viewtopic.php?p=6#6

the 2 on the left are when i run it w/ the thread, the 1 on the right
is without it in a thread. Only .1 and .2 are pingable in this
situation.

Sep 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If you are assigning to a global int, and i presume you are spawning a
thread per ping, then each thread will change the global int as and when it
gets its ping result.

So if you then do soemthing like store that global int as a ping completes
you are likely to get innacurate results, or moreover, accurate results int
he wrong order giving you a wrong looking readng.

I only glanced over your code but when i saw 'global int' variable and
threads i thought that sounds like the issue, especially if this problem
isnt hapeening when it is non threaded. Because in a non threaded it will do
them one at a time, so ping first, change global int, store value at that
point, next ping etc, so it would work.

Try this, make a struct to contain data like:

string ipaddress,
bool pingSuccess,

etc...

Then per ping create a new instance or have an arraylist containing the
structs and add a new one to the list as you need, and on successfull ping
store in that struct the result. I'd also have a event fire everytime a ping
completes to let your gui know that a ping just completed and pass into that
event the struct.

Then on that event being raised update gui with the ping data.

The result should be a list of pinged ip's all updating as and when the
results complete, all in threads alongside each other.

A global value being changed by multiple threads is a bad idea.
<CE*********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
I'm trying to trow in another thread into my program. What it's for is
to ping a list of IPs. When it runs the Ping portion of the code, it
assigns a value of 1 or 2 (pingable / not pingable) to a global int. It
works great, however it's slow... If I run the ping portion of my code
in a thread, it returns random values for the pingable / non pingable
values...

My code and a screenshot of is located here.
http://csharp.thinkglobaltech.com/viewtopic.php?p=6#6

the 2 on the left are when i run it w/ the thread, the 1 on the right
is without it in a thread. Only .1 and .2 are pingable in this
situation.

Sep 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.