Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys

I'm working on the client code for my TCP server. I have the ServerConnection class running in its own thread and then I have a thread pool that sits and processes the raw info seperate to the receiving.

When a single packet is isolated from the raw stuff, I have to trigger an event so the parts of the program that are interested in knowing about this can register for it. (Each form that opens will get passed a reference of the ServerConnection object)

Now obviously if I try to work with the UI in these said events I'll get a cross thread operation error... So Invoke here we come

My question is this:
It might even be a stupid question but here goes nothing
Is there some way I Invoke the event before hand... As in, when the event is triggered in the form, its all ready in the UI thread so I can work with it directly from there.

I'd hate to have to have a million invoke calls and delegates inbetween my WinForms code.

Thanks :)
Posted

I would use System.Threading.SynchronizationContext instead of Invoke.

Pass SynchronizationContext.Current from your Form1_Load to your ServerConnection and save it as a static.

Then when you want to raise an event, write something like this:
C#
_sUIContext.Post( o => Event( this, EventArgs.Empty ), null );

and the event will be raised on the UI thread.

Notice I haven't checked if Event != null. I use this trick when declaring an event ( as there is debate over whether other patterns are thread-safe ):
C#
public EventHandler Event = delegate { };


This adds an empty delegate to the invocation list, which means the Event is never null.

Nick
 
Share this answer
 
If I understand you correctly I don't think it's possible.

You can however simplify the invoke a bit.

In the event handler you can simply check if a invoke is needed and if so call a delegate that has the event handler method as it's method.
K that sounds confusing ;P

Here is some code (in vb.net but I'm sure you can translate it into c#)

Private Sub event_method(ByVal Param1 As SAPbouiCOM.Application, ByVal param2 As SAPbobsCOM.Company) Handles event
        If Me.InvokeRequired = True Then
            Me.Invoke(New delegate(AddressOf event_method), param1, param2)
        Else
           'do the code here
        End If
    End Sub


Not sure if this is what your looking for tho.
 
Share this answer
 
If you're using a thread pool and you want certain stuff to happen on the GUI thread then there'll have to be Invokes somewhere.

You can use the ISynchronizeInvoke[^] class to handle this internally in the ServerConnection object if you don't want it in you GUI class.

An example of how this is used is the System.Timers.Timer class. It has a SynchronizingObject[^] property; if you set that to a value (like a reference to your GUI) then the Timer will execute the callback on that object's thread, otherwise if it's null then the Timer callback is executed in the Thread Pool. You can create the same behavior in your objects using the ISynchronizeInvoke class.
 
Share this answer
 

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