Example #1
0
        // You could get a similar behaviour when using the following public delegate
        //public delegate void pageLoaded(object sender, MyWebSurferEventArgs args);


        //this asynchronous function does the web request and fires the event pageLoaded when a result is obtained
        public async void StartWebrequest(string url)
        {
            //Code from the lecture
            var request  = WebRequest.Create(url);
            var response = await request.GetResponseAsync();                    //await the result

            using (var stream = new StreamReader(response.GetResponseStream())) //use a streamreader with using to take care of the closing/disposing of the stream
            {
                var content = stream.ReadToEnd();                               //read all data

                //create a new instance of MyWebSurferEventargs where the content is saved
                var parameter = new MyWebSurferEventArgs(content);

                //check if the event has listeners - important for proper code
                if (pageLoaded != null)
                {
                    pageLoaded(this, parameter); //fire the event with sender = this and the MyWebSurferEventArgs from above
                }
            }
            response.Close(); //dispose the response
        }
Example #2
0
 //This is the function that will be called when the pageLoaded-event is fired
 //it takes MyWebSurferEventArgs to transmit the message
 private static void Surfi_pageLoaded(object sender, MyWebSurferEventArgs e)
 {
     //give out result which is saved in the message property
     Console.WriteLine(e.message);
 }