Click here to Skip to main content
16,007,932 members
Home / Discussions / C#
   

C#

 
GeneralRe: DNS query - help Pin
Richard Deeming19-Aug-02 1:14
mveRichard Deeming19-Aug-02 1:14 
Generalforcibly unloading a .NET dll Pin
Nish Nishant16-Aug-02 20:27
sitebuilderNish Nishant16-Aug-02 20:27 
GeneralRe: forcibly unloading a .NET dll Pin
James T. Johnson17-Aug-02 1:23
James T. Johnson17-Aug-02 1:23 
GeneralRe: forcibly unloading a .NET dll Pin
Nish Nishant17-Aug-02 9:52
sitebuilderNish Nishant17-Aug-02 9:52 
GeneralWindows Message Handling Pin
ke5in16-Aug-02 17:42
ke5in16-Aug-02 17:42 
GeneralRe: Windows Message Handling Pin
leppie16-Aug-02 18:43
leppie16-Aug-02 18:43 
GeneralRe: Windows Message Handling Pin
James T. Johnson17-Aug-02 1:43
James T. Johnson17-Aug-02 1:43 
GeneralRe: Windows Message Handling Pin
James T. Johnson20-Aug-02 6:14
James T. Johnson20-Aug-02 6:14 
I tried emailing this but just got a bounce-back in return:

> In C# I can create a class for a thread and get it running.
> But the thing that has me confused is the “event” which
> occurs in the application. How do I notify the thread that
> this “event” has occurred using “the event architecture that
> .NET already provides”?

Here is how I would set it up (ignore capitalization, I'm typing in outlook so it likes to 'correct' me). There are two different ways of doing this, the first is used if its just a notification that something happened ("I was clicked", "I finished processing the task you already know about", etc), the second is used if you need to supply some data to the listener.

First the notification method:

class MyThread : ....
{
  // other members
 
  public event EventHandler MyEvent;
  
  protected virtual void OnMyEvent(EventArgs e)
  {
    FireMyEvent(e);
  }
 
  private void FireMyEvent(EventArgs e)
  {
    if( MyEvent != null )
    {
      MyEvent(this, e);
    }
  }
 
  private void SomeCode()
  {
    SomeLengthyProcessing();
 
    SomeMoreProcessing();
 
    // MyEvent occurred let everyone know
    OnMyEvent(EventArgs.Empty);
 
    EvenMoreProcessing();
  }
}
Now in the code that needs to receive that event:

.....
MyThread mt = new MyThread();
 
// attach event listner
mt.MyEvent += new EventHandler(myMethodToExecuteWhenMyEventHappens);
.....
 
private void myMethodToExecuteWhenMyEventHappens(object sender, EventArgs e) {
  // Code to execute when MyEvent occurs
}


In the cases where you have data to be returned you use the same pattern, but the type names change a bit

class MyEventEventArgs : EventArgs
{
  // data members
  private int myValue;
 
  public MyEventEventArgs( /* arguments */ int myValue )
  {
    // set data members
    this.myValue = myValue;
  }
 
  public int MyValue
  {
    get { return myValue; }
  }
}


class MyThread : ....
{
  // other members
  public delegate void MyEventEventHandler(object sender, MyEventEventArgs e);
 
  public event MyEventEventHandler MyEvent;
  
  protected virtual void OnMyEvent(MyEventEventArgs e)
  {
    FireMyEvent(e);
  }
 
  private void FireMyEvent(MyEventEventArgs e)
  {
    if( MyEvent != null )
    {
      MyEvent(this, e);
    }
  }
 
  private void SomeCode()
  {
    SomeLengthyProcessing();
 
    SomeMoreProcessing();
 
    // MyEvent occurred let everyone know
    MyEventEventArgs e = new MyEventEventArgs(42);
 
    OnMyEvent(e);
 
    EvenMoreProcessing();
  }
}
Now in the code that needs to receive that event:

.....
MyThread mt = new MyThread();
 
// attach event listner
mt.MyEvent += new MyEventEventHandler(myMethodToExecuteWhenMyEventHappens);
.....
 
private void myMethodToExecuteWhenMyEventHappens(object sender, MyEventEventArgs e) {
  // Code to execute when MyEvent occurs
  int value = e.MyValue;
   
  // do something with value
}
A lot of this seems like its overkill, but once you have used the framework for a while you begin to appreciate this style of event handling (its possible to attach 100 listeners to an event, where as posting to a thread's message queue only notifys that thread).

HTH,

James
"And we are all men; apart from the females." - Colin Davies
GeneralCreating Wizard in C# Pin
cloudno916-Aug-02 17:06
cloudno916-Aug-02 17:06 
GeneralRe: Creating Wizard in C# Pin
Anonymous16-Aug-02 17:43
Anonymous16-Aug-02 17:43 
GeneralRe: Creating Wizard in C# Pin
James T. Johnson17-Aug-02 1:31
James T. Johnson17-Aug-02 1:31 
GeneralSimple questions I am sure... Pin
Ray Cassick16-Aug-02 15:44
Ray Cassick16-Aug-02 15:44 
GeneralRe: Simple questions I am sure... Pin
Stephane Rodriguez.16-Aug-02 23:27
Stephane Rodriguez.16-Aug-02 23:27 
GeneralRe: Simple questions I am sure... Pin
Ray Cassick17-Aug-02 17:01
Ray Cassick17-Aug-02 17:01 
GeneralRe: Simple questions I am sure... Pin
Stephane Rodriguez.18-Aug-02 8:18
Stephane Rodriguez.18-Aug-02 8:18 
GeneralRe: Simple questions I am sure... Pin
Ray Cassick18-Aug-02 15:58
Ray Cassick18-Aug-02 15:58 
GeneralRe: Simple questions I am sure... Pin
Stephane Rodriguez.16-Aug-02 23:31
Stephane Rodriguez.16-Aug-02 23:31 
GeneralEvent handling question Pin
Paul Riley16-Aug-02 14:57
Paul Riley16-Aug-02 14:57 
GeneralRe: Event handling question Pin
Stephane Rodriguez.16-Aug-02 23:15
Stephane Rodriguez.16-Aug-02 23:15 
GeneralRe: Event handling question Pin
Paul Riley17-Aug-02 0:16
Paul Riley17-Aug-02 0:16 
GeneralRe: Event handling question Pin
James T. Johnson17-Aug-02 2:05
James T. Johnson17-Aug-02 2:05 
GeneralRe: Event handling question Pin
Paul Riley18-Aug-02 10:06
Paul Riley18-Aug-02 10:06 
GeneralC++ SendMessage Pin
ke5in16-Aug-02 11:01
ke5in16-Aug-02 11:01 
GeneralRe: C++ SendMessage Pin
skn2k16-Aug-02 11:25
skn2k16-Aug-02 11:25 
GeneralRe: C++ SendMessage Pin
ke5in16-Aug-02 16:49
ke5in16-Aug-02 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.