473,785 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event can only appear n the left hand side of += or -=

Airslash
221 New Member
Hello,

this is the code of my StreamServer class:

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9.  
  10. namespace Tools.IO
  11. {
  12.     #region HelperClass ClientEventArgs
  13.  
  14.     /// <summary>
  15.     /// Helper class that holds information about the client
  16.     /// that connects to the server.
  17.     /// </summary>
  18.     public class ClientEventArgs : EventArgs
  19.     {
  20.         #region Constructor
  21.  
  22.         /// <summary>
  23.         /// Creates a new instance of the ClientEventArgs class.
  24.         /// </summary>
  25.         /// <param name="client">The TCPClient that has connected/disconnected from the server.</param>
  26.         public ClientEventArgs(TcpClient client)
  27.         {
  28.             // save the client.
  29.             Client = client;
  30.         }
  31.  
  32.         #endregion
  33.  
  34.         #region Public Properties
  35.  
  36.         /// <summary>
  37.         /// Gets or sets the TcpClient that has connected/disconnected from the server.
  38.         /// </summary>
  39.         public TcpClient Client
  40.         {
  41.             get { return m_client; }
  42.             set { m_client = value; }
  43.         }
  44.  
  45.         #endregion
  46.  
  47.         #region Private Fields
  48.  
  49.         private TcpClient m_client; // The TcpClient that has connected/disconnected from the server.
  50.  
  51.         #endregion
  52.     }
  53.  
  54.     #endregion
  55.  
  56.     public sealed class StreamServer
  57.     {
  58.         #region Events
  59.  
  60.         /// <summary>
  61.         /// This event gets fired when a client connects to the StreamServer.
  62.         /// </summary>
  63.         public event EventHandler<ClientEventArgs> OnClientConnected
  64.         {
  65.             add { lock (m_eventlock) { OnClientConnected += value; } }
  66.             remove { lock (m_eventlock) { OnClientConnected -= value; } }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// This event gets fired when a client disconnects from the StreamServer.
  71.         /// </summary>
  72.         public event EventHandler<ClientEventArgs> OnClientDisconnected
  73.         {
  74.             add { lock (m_eventlock) { OnClientDisconnected += value; } }
  75.             remove { lock (m_eventlock) { OnClientDisconnected -= value; } }
  76.         }
  77.  
  78.         #endregion
  79.  
  80.         #region Private Methods
  81.  
  82.         private void ListenRoutine()
  83.         {
  84.             // Try to enter the Monitor lock guarding the TcpListener.
  85.             // If acquiring the lock fails, then fall through the function
  86.             // and try again on the next call.
  87.             if (Monitor.TryEnter(m_listenlock, 10))
  88.             {
  89.                 // We succeeded in acquiring the lock on the TcpListener.
  90.                 // Check if a connection is pending, and if the TcpListener
  91.                 // is still initialized.
  92.                 // If there is no connection pending, fall through the function
  93.                 // and recheck on the next call.
  94.                 if ((m_listener != null) && (m_listener.Pending()))
  95.                 {
  96.                     // There is a connetion pending, accept this connection first.
  97.                     TcpClient client = m_listener.AcceptTcpClient();
  98.  
  99.                     // Raise the OnClientConnectedEvent if possible.
  100.                     RaiseOnClientConnectedEvent(ref client);
  101.                 }
  102.             }
  103.         }
  104.  
  105.         private void RaiseOnClientConnectedEvent(ref TcpClient client)
  106.         {
  107.             // Take a copy of the event handler for the onConnected event.
  108.             // That way we can raise the event safely without having to 
  109.             // worry about racing conditions between the call & unsbscribing
  110.             // of the event.
  111.             EventHandler<ClientEventArgs> handler = OnClientConnected;
  112.  
  113.             // Check if the handler is not null, fire the event if possible.
  114.             if (handler != null)
  115.                 handler(this, new ClientEventArgs(client));
  116.         }
  117.  
  118.         #endregion
  119.  
  120.         #region Private Fields
  121.  
  122.         private TcpListener m_listener;
  123.         private readonly object m_listenlock;
  124.         private Timer m_listentimer;
  125.         private readonly object m_eventlock;
  126.         private List<Fiber> m_fibers;
  127.         private readonly object m_fiberlock;
  128.  
  129.         #endregion
  130.     }
  131. }
  132.  
I'm getting this error message (see title) in the RaiseOnClientCo nnectedEvent function. I've followed several sites that said to raise the event from a seperate function, but i'm stuck with this error, despite having the same code from MSDN and CodeProject examples.

What am I missing...?
Aug 18 '10 #1
1 14596
Airslash
221 New Member
never mind, solved it :)

Seems I need to declare my handers as private fields and use those.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. namespace Tools.IO
  10. {
  11.     #region HelperClass ClientEventArgs
  12.  
  13.     /// <summary>
  14.     /// Helper class that holds information about the client
  15.     /// that connects to the server.
  16.     /// </summary>
  17.     public class ClientEventArgs : EventArgs
  18.     {
  19.         #region Constructor
  20.  
  21.         /// <summary>
  22.         /// Creates a new instance of the ClientEventArgs class.
  23.         /// </summary>
  24.         /// <param name="client">The TCPClient that has connected/disconnected from the server.</param>
  25.         public ClientEventArgs(TcpClient client)
  26.         {
  27.             // save the client.
  28.             Client = client;
  29.         }
  30.  
  31.         #endregion
  32.  
  33.         #region Public Properties
  34.  
  35.         /// <summary>
  36.         /// Gets or sets the TcpClient that has connected/disconnected from the server.
  37.         /// </summary>
  38.         public TcpClient Client
  39.         {
  40.             get { return m_client; }
  41.             set { m_client = value; }
  42.         }
  43.  
  44.         #endregion
  45.  
  46.         #region Private Fields
  47.  
  48.         private TcpClient m_client; // The TcpClient that has connected/disconnected from the server.
  49.  
  50.         #endregion
  51.     }
  52.  
  53.     #endregion
  54.  
  55.     public sealed class StreamServer
  56.     {
  57.         #region Events
  58.  
  59.         /// <summary>
  60.         /// This event gets fired when a client connects to the StreamServer.
  61.         /// </summary>
  62.         public event EventHandler<ClientEventArgs> OnClientConnected
  63.         {
  64.             add { lock (m_eventlock) { m_clientconnect += value; } }
  65.             remove { lock (m_eventlock) { m_clientconnect -= value; } }
  66.         }
  67.  
  68.         /// <summary>
  69.         /// This event gets fired when a client disconnects from the StreamServer.
  70.         /// </summary>
  71.         public event EventHandler<ClientEventArgs> OnClientDisconnected
  72.         {
  73.             add { lock (m_eventlock) { m_clientdisconnect += value; } }
  74.             remove { lock (m_eventlock) { m_clientdisconnect -= value; } }
  75.         }
  76.  
  77.         #endregion
  78.  
  79.         #region Private Methods
  80.  
  81.         private void ListenRoutine()
  82.         {
  83.             // Try to enter the Monitor lock guarding the TcpListener.
  84.             // If acquiring the lock fails, then fall through the function
  85.             // and try again on the next call.
  86.             if (Monitor.TryEnter(m_listenlock, 10))
  87.             {
  88.                 // We succeeded in acquiring the lock on the TcpListener.
  89.                 // Check if a connection is pending, and if the TcpListener
  90.                 // is still initialized.
  91.                 // If there is no connection pending, fall through the function
  92.                 // and recheck on the next call.
  93.                 if ((m_listener != null) && (m_listener.Pending()))
  94.                 {
  95.                     // There is a connetion pending, accept this connection first.
  96.                     TcpClient client = m_listener.AcceptTcpClient();
  97.  
  98.                     // Raise the OnClientConnectedEvent if possible.
  99.                     RaiseOnClientConnectedEvent(ref client);
  100.                 }
  101.             }
  102.         }
  103.  
  104.         private void RaiseOnClientConnectedEvent(ref TcpClient client)
  105.         {
  106.             // Take a copy of the event handler for the onConnected event.
  107.             // That way we can raise the event safely without having to 
  108.             // worry about racing conditions between the call & unsbscribing
  109.             // of the event.
  110.             EventHandler<ClientEventArgs> handler = m_clientconnect;
  111.  
  112.             // Check if the handler is not null, fire the event if possible.
  113.             if (handler != null)
  114.                 handler(this, new ClientEventArgs(client));
  115.         }
  116.  
  117.         #endregion
  118.  
  119.         #region Private Fields
  120.  
  121.         private TcpListener m_listener;
  122.         private readonly object m_listenlock;
  123.         private Timer m_listentimer;
  124.         private readonly object m_eventlock;
  125.         private List<Fiber> m_fibers;
  126.         private readonly object m_fiberlock;
  127.         private EventHandler<ClientEventArgs> m_clientconnect;
  128.         private EventHandler<ClientEventArgs> m_clientdisconnect;
  129.  
  130.         #endregion
  131.     }
  132. }
  133.  
Aug 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

45
2684
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
10
6182
by: Cybertof | last post by:
Hello, Do you know why i get the error "Left hand side must be variable, property or indexer" with the following code : struct FundDataStruct { internal int quantity;
4
12471
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, Is there a setting/property for displaying a checkbox label (or radiobutton label) on the left hand side instead of the right hand side? Or am I limited to no text in the label - take on parent backcolor and add my own label to the left side? Thanks, Rich
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.