Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i'm currently stuck on Events and Delegates. I have an dll containing Events - for example the PlayerJoiningEventArgs Event. It's a part of the BF3Rcon.NET Project. My goal is to capture the Event if it's fired and return the values of the events!

I create an Object called:

C#
RconClient client = new RconClient();


and then assign:

C#
client.PlayerJoining += new EventHandler<PlayerJoinInEventArgs>((playersender, playerevent) => OnPlayerJoining(playerevent));


where OnPlayerJoining is a method and is fired when the Event is called.

C#
protected virtual void OnPlayerJoining(PlayerJoiningEventArgs e)
{            
   MessageBox.Show(e.Name);
}


My Problem is now, that i can't return the variable e.Name to my Form1 since i get the Message, that the Form1 is not the same Thread as the client Object!?. - MessageBox works well here! - Shows me correct Playername. So, what should i do to get the variable e.Name to my Form1 Thread?

Thank you for any suggestions or snippets!

With best Regards

Ralf
Posted
Updated 7-Nov-11 15:30pm
v5
Comments
[no name] 7-Nov-11 20:05pm    
Format code snippets when posting
FreewareFire 7-Nov-11 20:07pm    
Thank you - i've forget it, since it's 2 a.m. and i'm tired and stuck, since trying to solve this problem 2 days...
Sergey Alexandrovich Kryukov 7-Nov-11 21:28pm    
Formatting fixed: don't forget to replace <> with HTML entities.
--SA
Sergey Alexandrovich Kryukov 7-Nov-11 21:35pm    
All is wrong, so we would need to know some stuff. What happens in what thread? What does it mean "get the e-Name to my Form1 Thread"? What do you want to do with it except MessageBox? (Better get rid of MessageBox as it is modal.)
--SA
LanFanNinja 7-Nov-11 22:53pm    
Check my solution below. Never mind the rating I think someone thought it would be funny to down vote it less than a minute after it was posted. Maybe they don't like me? Anyway I have ran into this type of problem many times before and I think this code will help you.

1 solution

Well I don't have Battlefield 3 so I cannot test to be sure but if the error you are getting is a cross thread operation error then something like this might help.

C#
string name;

private delegate void OnPlayerJoiningCallback(PlayerJoiningEventArgs e);

protected virtual void OnPlayerJoining(PlayerJoiningEventArgs e)
{
    if (this.InvokeRequired)
    {
        OnPlayerJoiningCallback opjc = 
            new OnPlayerJoiningCallback(OnPlayerJoining);
        this.Invoke(opjc, new object[] { e });
    }
    else
    {
        this.name = e.Name;
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900