1

I'm trying to develop an application using C# wiforms. I want to execute void sendEmail(){} function in a class only when internet connection is up. The method that I used is bool checkConnection(){} function infinitely loops a while loop and sending ping requests to google.com. if the ping is successful, sendEmail(){} function is called by the checkConnection() function.

But i know this is not the best practice of doing this. I'm very confusing about custom events in C#. how to use custom events to accomplish this task. expecting an answer with simple explanation.

Thanks in advance

JayNaz
  • 343
  • 1
  • 5
  • 24

1 Answers1

1

I understand that you are trying to build a scheduler task like functionality in c#. Based on my understanding, a windows service would do the task for you like listening for the availability of internet and then performing the mail sending operation when the application goes online.

W.R.To Events, you can build your own event engine that the one that raises the application events when the app runs and then there will be database entries that lists the pending tasks. There will be a background job like a windows service that reads the database and based on the availability of internet or on some condition executes the job.

If you can be more clear on the exact use-case and what you have tried so far the community can help you better.

Sample class Observable { public event ImageUploadeventHandler InternetcOnnected;

public void DoSomething()
{
    ImageUploadeventHandler handler = InternetcOnnected;
    if (handler != null)
    {
        handler(this, EventArgs.Empty);
    }
    }
}
class Observer
{
    public void HandleEvent(object sender, EventArgs args)
    {
        // upload the image to the online service
    }
}
Saravanan
  • 7,637
  • 5
  • 41
  • 72
  • Every five minutes my program captures the screen of the computer if the internet connection is available and send those snapshots via email. If internet connection is not available it is not capturing the screen. I have written the functions for capture screen & save them in a specific folder as jpg. want to run these capturing and sending tasks periodically only in the internet connection is on. – JayNaz Feb 28 '16 at 11:34
  • Fine, then you can make use of the sample given in: http://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net to check for internet connection and then do your tasks. In case the method returns `true`, you can raise an event that takes the images in the folder and then sends them by mail. – Saravanan Feb 28 '16 at 11:40
  • I have already use that function to get the status of internet connection. Could you please explain how to implement "C# Custom Events" to raised by the response of this function. I think.. if(CheckForInternetConnection()) { sendMail(); } is not the proper way to do that. – JayNaz Feb 28 '16 at 11:47
  • I have added a sample, however to give more indepth details about events, please refer this link http://csharpindepth.com/Articles/Chapter2/Events.aspx – Saravanan Feb 28 '16 at 11:53
  • Thanks saravanan. I'll go through it. – JayNaz Feb 28 '16 at 11:55