Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:48:24+00:00 2026-06-10T15:48:24+00:00

I try to implement (in WPF , C# ) Ellipse control which changes its

  • 0

I try to implement (in WPF, C#) Ellipse control which changes its colour depending on connection to google.com. If there is connection with Google, ellipse is green; else it’s red.

I coded it this way:

XAML code

<Window.Resources>
    <l:InternetConnectionToBrushConverter x:Key="InternetConnectionConverter" />
</Window.Resources>
<Grid>
    <DockPanel LastChildFill="True">
        <StatusBar Height="23" Name="statusBar1" VerticalAlignment="Bottom" DockPanel.Dock="Bottom">
            <Label Content="Google connection:" Name="label1" FontSize="10" Padding="3" />
            <Ellipse Name="ellipse1" Stroke="Black" Width="10" Height="10" Fill="{Binding Converter={StaticResource InternetConnectionConverter}}" Margin="0,4,0,0" />
        </StatusBar>
    </DockPanel>
</Grid>

and C# behind-code (value converter and function checking connection):

public class InternetConnectionToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType != typeof(Brush))
            throw new InvalidOperationException("The target must be a Brush!");

        return CheckConnection("http://www.google.com/") == true ? Brushes.Green : Brushes.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    /// <summary>
    /// Checks connection to specified URL. 
    /// </summary>
    /// <param name="URL"></param>
    /// <returns><b>True</b> if there is connection. Else <b>false</b>.</returns>
    private bool CheckConnection(String URL)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
            request.Timeout = 15000;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            return response.StatusCode == HttpStatusCode.OK ? true : false;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
            return false;
        }
    }

}

It works well but there are 2 problems:

  1. it takes some time to check connection, so I think I should be doing it in new thread (if I’m wrong, please tell me why)
  2. what’s more important – it checks connection with Google only once, when program starts running (despite I use binding, so I expect that there would be constant connection monitoring)

How should I solve this?
I want to have constant connection monitoring, so that when I disconnect internet connection, Ellipse control will change its colour.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:48:26+00:00Added an answer on June 10, 2026 at 3:48 pm

    You must change your architecture a bit.
    You can not use threading in an IValueConverter to avoid locking the UI. You still need to wait for the thread to complete before returning from the IValueConverter.

    You need to create a HasConnection property to bind the ellipse color to. You can then run the connection check in another thread. Preferably by using the BackgroundWorker. The HasConnection property should be updated once the check is complete. You can then use a timer and check your connection periodically and update the HasConnection after each check.

    Edit
    You can also monitor the NetworkChange.NetworkAvailabilityChanged event to find out when local network connection goes up or down. However of you want to ensure that you can actually connect to your target you should keep your old CheckConnection, but call CheckConnection on startup, when the network availability changes and periodically on a timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to understand the ICommand from wpf. In my Event class I implement
I try to implement a query by using Between Clause,but there is some problem
I try to implement callback interface with COM techonlogy and have IDL like that:
Hi I try implement solution from this site im my WPF app for global
WPF, Shapes I have a custom Shape , and try to implement MouseClick and
How do I implement a DependencyProperty on a custom wpf control for an ImageSource
I'm working on a Surface WPF project where we try to implement the MVVM
I try to implement the animation: when you enter iPhone Gallery, press the image,
I try to implement footer datagrid with allowMultipleSelection properties. But in my case, it
I try to implement a browser-like app. I want to let it can open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.