2

My program starts with windows startup,
But a background worker is supposed to work instantly after the program is opened.
But it starts with a delay and then even returns false signs(it returns if a site is up),
Only after about 15 seconds the background-worker continues to work normally and the program too. I think this is because of .net framework trying to load, or internet connection that is not up yet, or something that didn't load yet(windows startup).
What can solve this, and what is the probable cause? (WinForm C#)

Edit:
Here is something I thought of,
I don't think though that this is a good practice. Is there a better way?
(Load method):

    while (!netConnection())
    {
    }
    if(netConnection())
    bwCheck.RunWorkerAsync();
Charles
  • 50,943
  • 13
  • 104
  • 142
funerr
  • 7,212
  • 14
  • 81
  • 129

2 Answers2

5

I think this is because of .net framework trying to load

Nope. If that were the case your program wouldn't run.

or internet connection that is not up yet, or

Yup. The network card/interface/connection/whatever is not initialized and connected to the internet yet. You can't expect a PC to be connected to the internet immediately at startup. Even more, what if your customer is on a domain using network authentication? What if they delay network communications until some task is complete (this was actually the problem in my case below. Seriously.)

It may take even longer to get it up and running in that case (read: don't add a Thread.Sleep() in a vain attempt to 'fix' the issue.

I had to fix a problem like this once in a systems design where we communicated to a motion control board via the ethernet bus in a PC. I ended up adding some code to monitor the status of the network connection and, only when it was established, started talking to the device via the network card.

EDIT: As SLaks pointed out in the comments, this is pretty simple in C#: The NetworkAvailabilityChanged event for your programming pleasure.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • How can I then solve this? Check if internet connection is up-> then only start the bg-worker? – funerr Oct 24 '11 at 21:07
  • @agam360: I added more info, had to do the exact same thing once. Hope it helps. – Ed S. Oct 24 '11 at 21:09
  • SLaks: My project was in C, didn't even realize it would be so much more trivial in C#. Thanks. – Ed S. Oct 24 '11 at 21:10
  • @agam360: Posted a link to the docs that should get you going. – Ed S. Oct 24 '11 at 21:13
  • @agam: It's not bad to sit in a loop waiting, but there are two things you should do. 1. add a `Sleep` in the loop so that your program relinquishes CPU time and doesn't peg the machine, and 2. Add some break condition. You don't want to loop forever. If the network never comes up you should log it and do... something, but don't just wait ad infinitum. Did you try using the NetworkAvailabilityChanged event? You'll still have too loop and wait, but I'm not sure where `netConnection()` is coming from. – Ed S. Oct 25 '11 at 16:56
  • @EdS. My problem is mainly to check if the internet is on, because the adapter can start from working to -> not working. anyways I'll try what your saying. – funerr Oct 27 '11 at 14:01
0

It is absolutely because of everything still starting up. Services can still be coming online long after you log in, the quick login dialog you see was an optimization in windows to let you log in while everything else still starts up.

Take note of How to detect working internet connection in C#? specifically a technique that avoids the loopback adapter:

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • NetworkInterface.GetIsNetworkAvailable(), Does not seem to work, It returns true all the time... (even if I shutdown my adsl adapter+modem). – funerr Oct 25 '11 at 18:52
  • by shut down - is your network interface up still? if so the network is 'available' regardless of if something is on the other end. Note you may have to ping for example, google to determine this. See : http://channel9.msdn.com/coding4fun/articles/You-Can-Take-It-With-You-Part-2 – Adam Tuliper Oct 25 '11 at 20:34
  • I think that pinging Google seems quite a bad practice although there is a 0 or close to it chance that Google will fall. Any other way? – funerr Oct 26 '11 at 18:45
  • Ping your own other provider. Otherwise how do you know you are actually on the net? You don't. If it's dhcp you can get notified when an address change event occurs this would tell you that you've communicated potentially outside the machine. – Adam Tuliper Oct 26 '11 at 21:57