PDA

Click to See Complete Forum and Search --> : eventhandling


Techno
Sep 13th, 2005, 10:24 AM
*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?

MrPolite
Sep 13th, 2005, 11:43 AM
hello? I wrote you an extensive example before
http://www.vbforums.com/showthread.php?t=357956

Mike Hildner
Sep 13th, 2005, 12:00 PM
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

Techno
Sep 13th, 2005, 12:50 PM
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:



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

//this class is instantiated by the main form
//somemethod
//create new thread and execute ClassB



Worker Thread:


//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!

MrPolite
Sep 13th, 2005, 12:58 PM
hmm not sure if I get the idea but you could try this:D
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

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();

Techno
Sep 13th, 2005, 01:26 PM
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:

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! :)

Techno
Sep 13th, 2005, 02:52 PM
OK, correct me if i am wrong but I THINK i got it. here we go:

MainForm:


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:


//constructor of Worker Thread Class
this.theDataManager = new IncomingDataManager(someparams);
this.theDataManager.OnStatusChange += this.theMainForm.aDelegate;


IncomingDataMan:

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! :)

Techno
Sep 13th, 2005, 07:29 PM
another edit

Techno
Sep 14th, 2005, 08:38 PM
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:

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:



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 :(