[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Dave Keenan

8/19/2004 2:47:00 PM

I'm a working on a Windows Forms application using
remoting and have hit a problem. I have three basic
components:

1) A Remote object, call it myServer which is a
MarshallByRef singleton. This raises an Event called
myRemoteEvent.

2) A Client business logic class, call it myClient. This
subscribes to the myRemoteEvent event on the remote
server. It also can raise an event of it's own, called
myClientEvent.

3) A client side Windows form, call it myForm. This
contains an instance of myClient and subscribes to it's
myClientEvent event.

What I'm looking for to happen is myServer to raise
myRemoteEvent, myClient should handle this event by
raising myClientEvent and myForm should handle the
myClientEvent.

I can get myServer to raise it's event and myClient to
handle it in it's event handler. However, when I then try
to raise myClientEvent I get a null reference exception
even though myForm is definitely subscribed to it.

However, if I skip the remoting stage and have myClient
raise myClientEvent directly then the event is raised and
myForm responds as it should.

I think it's probably a fairly basic scope issue, but I'm
at a loss to solve it. If anyone wants I can put together
a sample Visual Studio solution illustrating my problem.

Any and all help appreciated!

Thanks,

Dave
3 Answers

Dave Keenan

8/19/2004 4:01:00 PM

0

I have posted a zipped up Visual Studio solution
illustrating the problem at:

http://www.slaandjivaa.com/code/remoti...

On the Client form there are two buttons. One raises the
MyClientEvent directly and works fine, the other raises
the MyServerEvent which is in turn handled by the MyClient
object which then tries to raise the MyClientEvent. It is
at this point that the MyClientEvent shows as a null
reference which can be seen if you debug and step through
the code.

Again, all help appreciated.

Thanks,

Dave

Ken Kolda

8/19/2004 5:17:00 PM

0

You've got 3 issues you need to fix to make this code work as you expect:

1) Your MyClient class needs to derive from MarshalByRefObject. Otherwise,
when you pass the delegate to the server, the server will get a reference to
a new instance of the class in the server's process space instead of your
remoted object on the client.

2) You need to declare a channel in the client for server callbacks. Just
copy the exact same code you have for creating/registering your channel in
the server app to the client app but change the "port" setting to 0 (so the
client will pick a port on its own).

3) When the server raises the event it will come to the client on a
different thread. That thread should not attempt to update the TextBox since
it is not the UI thread for the form. Use the BeginInvoke() method to call a
delegate on the UI thread that will update the textbox, e.g.

//Event Handler for the MyClient's MyClientEvent
private void MyClient_ClientEventRaised(object sender, EventArgs e)
{
if (this.InvokeRequired)
BeginInvoke(new ThreadStart(showEventText));
else
showEventText();
}

private void showEventText()
{
this.msgBox.Text += "MyClientEvent has been handled\r\n";
}

Good luck -
Ken


"Dave Keenan" <davidkeenan76DELETE@NOSPAMhotmail.com> wrote in message
news:209301c48605$c2542770$a301280a@phx.gbl...
> I have posted a zipped up Visual Studio solution
> illustrating the problem at:
>
> http://www.slaandjivaa.com/code/remoti...
>
> On the Client form there are two buttons. One raises the
> MyClientEvent directly and works fine, the other raises
> the MyServerEvent which is in turn handled by the MyClient
> object which then tries to raise the MyClientEvent. It is
> at this point that the MyClientEvent shows as a null
> reference which can be seen if you debug and step through
> the code.
>
> Again, all help appreciated.
>
> Thanks,
>
> Dave
>


Dave Keenan

8/20/2004 8:40:00 AM

0


>-----Original Message-----
>You've got 3 issues you need to fix to make this code
work as you expect:
>

<snip>
Some very helpful suggestions and code.
</snip>

>Good luck -
>Ken

Thanks Ken, I thought I was going to have to waste all
weekend on this. I've been struggling along with remoting
just from the MSDN examples and snips of code found on the
net, think I might have to invest in a good book. In the
meantime I have my application doing exactly what I need.

Thanks again,

Dave