A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
This example demonstrates how to generate log file with Delegate and Event technique.
A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
Event represents the state of an event, such as the element in which the event occurred, the state of the keyboard keys, the location of the mouse, and the state of the mouse buttons.
First, you will need to import the System.IO namespace.
The System.IO namespace contains the StreamWriter and File Classes.
| Imports System.IO |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.We use the button1_Click event to do the work.
Click the button Generate Log File ,System will generate a log file in folder log which locates in root directory. The code as follows.
| Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGeneratelog.Click Client.Run(txtKey.Text.Trim(), Server.MapPath("log")) End Sub |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Add two custom class Client and UserInputMoniter in the project. The code as follows.| Imports System Imports System.IO Public Class Client Private Shared path As String = "" End ClassPublic Shared Sub Run(ByVal key As String, ByVal strpath As String) path = strpath End SubDim monitor As New UserInputMoniter() Dim a As New Client(monitor) monitor.Run(key) Private Sub GenerateLog(ByVal sender As Object, ByVal e As EventArgs) Dim writer As StreamWriter = Nothing End SubDim strPath As String = path + "\log.ini" Try If File.Exists(strPath) Then Catch ex As Exception File.Delete(strPath) End IfTry writer = File.AppendText(strPath) End Trywriter.WriteLine("Test log.") Finally writer.Dispose() Throw New Exception(ex.Message) End TryFinally writer.Close() writer.Dispose() Sub New(ByVal m As UserInputMoniter) AddHandler m.OnUserRequest, AddressOf Me.GenerateLog End SubImports System '/ <summary> '/ UserInputMoniter '/ </summary> Public Class UserInputMoniter Delegate Sub UserRequest(ByVal sender As Object, ByVal e As EventArgs) End Class Public Event OnUserRequest As UserRequest Public Sub Run(ByVal key As String) If key.Trim() = "h" Then End SubRaiseEvent OnUserRequest(Me, New EventArgs()) End If |
The front end DelegateEventVB.aspx page looks something like this:
| <table> <tr> </table> <td colspan="2" style="text-align: left"> </tr>Information: <span style="color: #ff3333">If the KeyValue is 'h' ,click the follow button will generate log file in the folder log which locate in root directory.</span></td> <tr> <td style="width: 100px; text-align: right;"> </tr><asp:Label ID="lblKeyValue" runat="server" Text="KeyValue:"></asp:Label></td> <td style="width: 100px"> <asp:TextBox ID="txtKey" runat="server"></asp:TextBox></td> <tr> <td colspan="2"> </tr><asp:Button ID="btnGeneratelog" runat="server" OnClick="Button1_Click" Text="Generate Log File" /></td> |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The flow for the code behind page is as follows.| Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGeneratelog.Click End Class Client.Run(txtKey.Text.Trim(), Server.MapPath("log")) End SubProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub |
No comments:
Post a Comment