Results 1 to 9 of 9

Thread: eventhandling

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    eventhandling

    *sigh*

    so stressful.

    I cannot get the idea on how to raise events! how????

    I have:

    MainForm
    ClassA (Worker thread)
    ClassB (can be called from worker thread or from MainForm)

    MainForm has a method which updates the status of a label (status text)
    This will be changed from ClassA and ClassB (Mainly ClassA)

    How can I create an eventhandler and raise it so that the main form, when event was raised, can change the status text?

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: eventhandling

    hello? I wrote you an extensive example before
    http://www.vbforums.com/showthread.php?t=357956
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: eventhandling

    Here's a quick example project. Not using .Invoke, which I guess you should. Still, it works.

    Trickier than I thought, because one needs to (I guess) instantiate the class before you can register with the event, so the constructor can't do anything that would tie things up.

    Mike
    Attached Files Attached Files

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: eventhandling

    Thanks very much guys i will look into this

    I appreciate the extensive reply MR.Polite I just cant seem to get the idea of it. I am using .NETCF so invoking is a bit tricky (but not much)

    I just dont know how to properly invoke the event from the worker thread!

    Main form:


    Code:
    this.OnStatusChange += new TheStatusChange(frmMain_OnStatusChange);
    public delegate void TheStatusChanged();
    public event TheStatusChanged OnStatusChange;
    public string theStatusChange;
    
    ..
    ..
    private void ChangeStatus()
    {
       this.lblCurrentStatus.Text = this.theStatusChange;
    }
    
    private void frmMain_OnStatusChange()
    {
       this.ChangeStatus();
    }
    ClassA
    Code:
    //this class is instantiated by the main form
    //somemethod
    //create new thread and execute ClassB
    Worker Thread:

    Code:
    //ClassB
    public delegate void TheStatusChangedEventHandler();
    		
    ..
    //some method
    
    //dont know what to put here to invoke the eventhandler on the main form from this worker thread
    I dont know if I am doing this correctly or not.
    I will check out the attached example as well as re read Mr.Polite's reply/in depth explanation!
    please, any more tips do post!
    Last edited by Techno; Sep 13th, 2005 at 01:05 PM.

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: eventhandling

    hmm not sure if I get the idea but you could try this
    remove that delegate from the worker thread.

    in the worker thread, add this (the same as the main form's event)
    public event TheStatusChanged OnStatusChange;
    now in the main form, replace that line with this
    Code:
    private event TheStatusChanged m_OnStatusChange;
    public event TheStatusChanged OnStatusChange
    {
    add
    {
        // add the event handler both to this class and to the worker thread
         m_OnStatusChange += value;
          workerThreadHere.OnStatusChange += value;
    }
    remove
    {
         m_OnStatusChange -= value;
          workerThreadHere.OnStatusChange -= value;
    }
    }
    now inside the worker thread, to raise the event do this:

    if (OnStatusChange != null)
    OnStatusChange();
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: eventhandling

    hmmm

    I will try that but it kinda isnt possible.

    Sorry, I should have explained more in depth of what/how the app is coded.

    mainform (has label)
    ConnMan
    CommunicationMan
    IncomingDataMan

    When button is clicked on main form, it creates a new instance of ConnMan.
    In ConnMan constructor it creates an instance of CommunicationMan.
    In CommunicationMan it creates an instance of IncomingDataMan

    A thread is created in ConnMan and executes a method in CommunicationMan.

    in that method in CommunicationMan it calls a method everytime (while true) in IncomingDataMan.

    IncomingDataMan deals with incoming data and sets the status of the incoming data (like a description) on the main form in a label, or by a public string variable on mainform.

    instead of doing this FROM incomingDataMan:
    Code:
    this.theMainForm.lblCurrentStatus = "sometext";
    I prefer to use events/delegates simply because its thread safer and won't cause strange behaviour and frankly its the best way to go.

    This is what I like to know is on how to make events and invoke them/make them "raised" in the main form from the incomingDataMan, which has been instantiated from a worker thread.

    I will keep trying no doubt!

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: eventhandling

    OK, correct me if i am wrong but I THINK i got it. here we go:

    MainForm:

    Code:
    private string theStatusMessage;
    public delegate void TheStatusChanged();
    public event TheStatusChanged OnStatusChange;
    
    public IncomingDataManager.TheStatusChanged aDelegate;
    
    //form load event:
    
    this.aDelegate = new namespace.IncomingDataManager.TheStatusChanged(OnStatusChange);
    ..
    ..
    
    private void frmMain_OnStatusChange()
    {
       this.lblCurrentStatus.Text = this.theStatusMessage;
    }
    Worker thread:

    Code:
    //constructor of Worker Thread Class
    this.theDataManager = new IncomingDataManager(someparams);
    this.theDataManager.OnStatusChange += this.theMainForm.aDelegate;
    IncomingDataMan:
    Code:
    public delegate void TheStatusChanged();
    public event TheStatusChanged OnStatusChange;
    
    ..
    ..
    //in some method
    this.theMainForm.theStatusMessage = "Something";
    if (this.OnStatusChange != null)
    {
       this.OnStatusChange();
    }

    it seems to work but would like to know if this is ok from the pro's view?
    how can I be so sure it works? I mean, i have stepped through in the debugger and when the event appears to be raised, it goes into the form and executes the frmMain_OnStatusChange() method!

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: eventhandling

    another edit
    Last edited by Techno; Sep 13th, 2005 at 11:38 PM.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: eventhandling

    it does work

    wierd thing, tried implementing another event/delegate on another form and it worked but now all of a sudden it doesnt! I get undefined value on the delegate

    i've even re-written the code but doesnt make a difference. any ideas?

    the form:
    Code:
    public delegate void TheNotificationOfDirs();
    public event TheNotificationOfDirs OnNotificationOfDirs;
    public IncomingDataManager.TheNotificationOfDirs abcDelegate;
    
    ..
    
    this.OnNotificationOfDirs += new TheNotificationOfDirs(FrmDirSelector_OnNotificationOfDirs);
    
    //form load:
    this.abcDelegate = new [i]namespace[/].IncomingDataManager.TheNotificationOfDirs(OnNotificationOfDirs);
    			
    
    private void FrmDirSelector_OnNotificationOfDirs()
    {
    	MessageBox.Show("Cool!!!");
    }
    incoming data manager:

    Code:
    public delegate void TheNotificationOfDirs();
    public event TheNotificationOfDirs OnNotificationOfDirs;
    
    
    //constructor:
    this.OnNotificationOfDirs += this.theMainForm.theDirSelector.abcDelegate;
    .abcdelegate always shows undefined value. But i dont understand, it was working fine a few hours ago and made no change to anything but other code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width