|
Heh,
thanks for that. Will give it a go,
Regards,
John
|
|
|
|
|
Hi,
I am trying the suggestions above. But another thing that I need to implement is having the client 'listening' to a port for any messages from the server program. This 'listening' needs to be constantly running in the background while also allowing the client to respond to user actions. Then if the server requests something of the client it will run. Any pointers on how I could implement this. Thanks again,
Regards,
John
|
|
|
|
|
hey!
i've a nice remoting/serialization problem:
i call a method of my server object, which needs a font parameter. if i access this font parameter in my server method i get a remoting exception. the cause of this problem is clear (?): if i read a property value of this font object, the server tries to call the property accessor on client side...
meanwhile i call my method without font parameter and with parameters like fontname, fontsize(...) instead...
but that's not the smoothest way, i think!
is there a way to use font parameters without exception?
font implements iserialization - but why becomes it not serialized?
thanks in advance
|
|
|
|
|
Yes, the Font is serializable, but what channel are you using for remoting? If you are hosting your remoting object in IIS, it can only use the HttpChannel. HTTP - by nature - is one way. The server can't initiate communications with the client. With you pass the name, size, etc., these are all value types (except for strings, which behave similarily) and are pass-by-value without using them as properties or the ref and out keywords.
Also, what exactly is the exception? The Type and message would help.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm using a TCP Channel.
The Exception is something like that:
"The Remotechannel has no Channelreceiver, it means that the Server has no registered Serverchannel or that the Application has no matching Clientchannel to communicate with the Server."
The type of the exception is System.Runtime.Remoting.RemotingException
|
|
|
|
|
What type of client- or server-activated type are you using (and which one of those is it)?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
it's a client-activated object (registered client type).
|
|
|
|
|
It's obvious the server is having trouble communicating with the client. What's not obvious is how you are registering the WKO on the server and connecting to it on the client. If you're using a config file to configure the remoting (both on the server and on the client, which in most cases is a better way so you don't have to recompile), could you post the relevent sections? Don't forget to escape your < and >, or check the "Do not treat <'s as HTML tags" below before posting.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
okay!
i register my object in source code.
here's an extract of the code to do this:
<br />
<br />
public void ConnectToServer(Type ObjectType)<br />
{<br />
TCPChannel Channel=new TcpClientChannel("ClientChannel",null);<br />
ChannelServices.RegisterChannel(Channel);<br />
<br />
RemotingConfiguration.RegisterWellKnownClientType(ObjectType,"tcp://server:10000/server.rem");<br />
}<br />
<br />
<br />
public void PublishObject()<br />
{<br />
TCPChannel Channel=new TcpServerChannel("ServerChannel",10000);<br />
ChannelServices.RegisterChannel(Channel);<br />
<br />
ServerObject Obj=new ServerObject();<br />
RemotingServices.Marshal(Obj,"server.rem",typeof(ServerObject));<br />
}<br />
<br />
|
|
|
|
|
Actually, this looks like a server-activated type. Using RemotingConfiguration.RegisterActivatedClientType would create the client-activated type. I'm having a friend look at this thread, though. He's a little better with this type of Remoting stuff than I.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
One thing that worries me from inspecting this code snip is that you might be mixing your client and server types.
You register ObjectType as your remoted object but then use another object ServerObject on the server. The object being remoted must be the same or implement the same interface. This is important because even if ObjectType is derived from ServerType the Remoting Framework only has registered/recognizes ObjectType as remotable. ServerType is just another type it will handle outside of the Remoting Framework. Both the client and server parts need to be dealing with the same object or deal with an interface or you get the wrong behavior.
Next once you seem to be using RemotingServices.Marshal in the wrong context. Marshal is used to transfer a MarshalByRefObject across application domains which has little to do with actually "publishing" the service.
What I recommend doing is using configuration files on both the client and the server and using RemotingConfiguration.Configure .
The client config file from your code snip should look something like this:
<configuration>
<system.runtime.remoting>
<application>
<client url="tcp://server:10000">
<wellknown
type = "ObjectType,ObjectAssembly"
url = "tcp://server:10000/server.rem" />
<!-- or atlernatively, the client activated version
<activated type="ObjectType,ObjectAssembly" />
-->
</client>
<channels>
<channel ref="tcp" port="10000" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Your server config file should look something like this:
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown mode="SingleCall" type="ObjectType, ObjectAssembly" objectUri="server.rem" />
<!-- alternatively, the client activated version
<activated type="ObjectType, ObjectAssembly" />
-->
</service>
<channels>
<channel ref="tcp" port="10000" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
If you use config files you don't have to worry about setup or teardown of channels or the registering or unregistering of types. It also makes for easy debugging since you only need to change the config file and run again instead of changing code, compile and run again.
|
|
|
|
|
Many thanks for this hint.
I'll change my remoting stuff in this way.
But solves it the problem of serialization of my Font object too?
|
|
|
|
|
My MethodCall looks like that:
<br />
Font MyFont=new Font("Arial",10);<br />
ServerObject.DoSomething(MyFont);<br />
The method throws an exception e.g. by accessing the Height Property.
|
|
|
|
|
HI
Quite a few times ago while browsing i came to know about the scripts who automate the procedure of registration on websites but now when i need few i am not able to find one. Actually i want to write the same script in C# so if anybody help me to find some good resources on internet.
THANX IN ADVANCE
|
|
|
|
|
I have the following event declaration in a base class window form with a button btnNew
public void btnNew_Click(object sender, System.EventArgs e)
{
btnDelete.Text = "Deleteddy";
// btndelete is another button on the form
And the following event declaration derived class that inherits from the above base class window
new public void btnNew_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Derived");
}
I am using the new modifier in the derived class to "hide" the behaviour in the base class. But when I click the btnNew, both events get fired.
Can anyone explain why ?
Thanks
|
|
|
|
|
First, that's not an event. That's a handler, and these generally should not be public. Second, when you handle the event you use a delegate which is a managed function pointer to the method you passed. Using new would not solve this problem - it is a different method and has a different address. Instead, consider declaring the first method with virtual and using override in the second case. This uses the IL instruction callvirt , which is a polymorphic instruction.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you very much.
I was under the impression that a virtual function can have no code in it. I modified the first method with virtual and used override in the second case. Now it does not execute the code in the base class but does the code in the derived handler twice.
Regards
Ganesh
|
|
|
|
|
You might be thinking of abstract .
If your event handler is firing twice (presumably before you had one to the base and derived methods), it means you have both methods hooked up. If you attempt to add a handler to a method that is already attached to the event, it shouldn't add another one. However, when you add a handler to a virtual method the compiler actually follows the virtual chain to the last method, which would be your derived method. This would make it possible to add the same handler (in essence) twice. Make sure in your code you only add a handler for one (like your base class, if you're going for a good polymorphic design where derived class's event handlers are fired in lieu of the base class's.
Also, when you override a method or property, you can use the base keyword to refer to the base class's implementation, much like you use this to refer to your own implementation (even though using this with a virtual function will still use the IL callvirtu instruction to call overriding methods in derived classes.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, thank you very much.
The windows designer had automatically added the
click event handler in the derived class. Once I commented that out, it is working like you described, that is the derived class handlers are fired in lieu of the base class's instead of cascading the two.
|
|
|
|
|
I have 2 simple controls. How can I do so that one becomes a child control of another.
Before:
Control1
Control2
After:
Control1
--Control2
I tried just to change the parent property, but after doing that, the control (who's parent property I just changed) disappeared. I tried calling Show() method but resulted nothing. So what's the trick ?
Regards, Desmond
|
|
|
|
|
On the would-be parent:
parent.Controls.Add(child); This sets the Parent property of the child, but there's more involved than simply that. Since Windows controls are backing these, the Windows handle must be recreated with the new parent Window handle.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I just have a simple question about auto checking of input value on NumericUpDown control.
Normally, if we press enter key after entered value on UpDown Control, it will automatically check whether it is between its max & min Value, and it will also limit the value. But if we just enter a value, didn't press enter key, it won't auto check the value.
I know we can use Validating to solve this problem, but inside Validating method, we also will need to compare the value by ourselves, I just feel quite inconvenient if we have 10 or more updown control in a Form...
Is there any build-in method of updown control that we can use to check the value ??
Thanks
|
|
|
|
|
Actually, both the Enter key behavior and validation work the same way - the control has to lose the focus (for validation) or the change has to be committed (by hitting the Enter key, which calls the set accessor of the Text property). See the documentation for the UpDownBase.UserEdit property in the .NET Framework SDK for a discussion of this behavior.
A good way to solve this would be to extend the NumericUpDown control and override either IsInputChar or IsInputKey , which are called with each key-down. Validate the entire number, though - not just the char or Keys that was passed to the method. Then change your code to instantiate this new control instead of the NumericUpDown . It's still a NumericUpDown control, though - you only extended it.
One other way is handle each NumericUpDown.KeyDown event (inherited from Control ) with the same handler, in which case you could do something like this:
numericUpDown1.KeyDown += new KeyEventHandler(ValidateNumber);
numericUpDown2.KeyDown += new KeyEventHandler(ValidateNumber);
private void ValidateNumber(object sender, KeyEventArgs e)
{
NumericUpDown nud = sender as NumericUpDown;
if (nud != null)
{
int value = int.Parse(nud.Text);
if (value < nud.Minimum || value > nud.Maximum)
e.Handled = true;
}
} As the comment said, you should also take things into account like the NumericUpDown.ThousandsSeparator and the like. You can use Int32.Parse (or Int64.Parse ) using the overload that uses the NumberFormatStyles as the second parameter. Do a bit-wise OR to construct the enum that you'll pass using the properties of the NumericUpDown control.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hm.... Thank you so much for your reply, Heath.
I just got an idea from your reply; instead of extenting the control, I would prefer using same handler for my UpdownControls. As I have 10 updowncontrols, so I assigned the same Validating handler for each of them.
private void UpDownValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
NumericUpDown nud = sender as NumericUpDown;
if(nud != null)
{
if(nud.Value < nud.Minimum)
nud.Value = nud.Minimum;
if(nud.Value > nud.Maximum)
nud.Value = nud.Maximum;
}
}
but I don't know will it cause any hidden effect? I think this method is better and simpler for me..
One more question,
Do you know any article that teaching us to make own control library. The idea is I want to make my own controls in my home, so that sometimes they can be used in my project by just import the control library.
Thanks a lot~~
|
|
|
|
|
Why would there be any "hidden side effect"? This is a perfectly reasonable solution, although I'd prefer encapsulation because it makes for a better OO design and doesn't force you to handle your events each time (which one could forget).
Developing a control library is trivial. In VS.NET, start a new Windows Control Library. Really, all this is is a DLL project, it just starts you off with a single control (with a bad name - so don't use it). Develop your controls and components deriving from the appropriate control and component classes. Don't forget to add decent designer support. There's a good, extensive section in MSDN that describes various base classes, attributes, and other design-time features that you should take into account for a good component library. See Developing Components[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|