Using the Remoting Callbacks in .Net Applications
来源:岁月联盟
时间:2003-07-12
This code snippet shows invoking a method on the Remoting objects using the asynchronous design pattern. Clicking on the button Run/Abort the following code snippet is going to be perform:
private void buttonRM_Click(object sender, System.EventArgs e)
{
try
{
if(buttonRM.Text == "Run")
{
// client state
buttonRM.Text = "Busy";
progressBarStatus.Value = 0;
textBoxStatus.Text = string.Empty;
cb.State = true;
//
// arguments
int timeinsec = 10;
string ticketId = "ProgressBar";
RemoteCallback wire = new RemoteCallback(cb.Progress);
//
// invoking method on the objectA
AsyncCallback acbA = new AsyncCallback(asyncCallBackA);
DelegateGiveMeCallback dA = new DelegateGiveMeCallback(roA.GiveMeCallback);
IAsyncResult arA = dA.BeginInvoke(timeinsec, ticketId, wire, acbA, null);
//
// invoking method on the objectX
AsyncCallback acbX = new AsyncCallback(asyncCallBackX);
DelegateGiveMeCallback dX = new DelegateGiveMeCallback(roX.GiveMeCallback);
IAsyncResult arX = dX.BeginInvoke(timeinsec, "TextBox", wire, acbX, null);
//
// invoking method on the objectWS
AsyncCallback acbWS = new AsyncCallback(asyncCallBackWS);
DelegateGiveMeCallback dWS = new DelegateGiveMeCallback(roWS.GiveMeCallback);
IAsyncResult arWS = dWS.BeginInvoke(timeinsec, "Button", wire, acbWS, null);
//
buttonRM.Text = "Abort";
}
else
if(buttonRM.Text == "Abort")
{
cb.State = false;
}
}
catch(Exception ex)
{
textBoxStatus.Text = ex.Message;
buttonRM.Text = "Run";
}
}
// async result from the remoting objects
private void asyncCallBackA(IAsyncResult ar)
{
DelegateGiveMeCallback d = (DelegateGiveMeCallback)((AsyncResult)ar).AsyncDelegate;
object o = d.EndInvoke(ar);
buttonRM.Text = "Run";
}
private void asyncCallBackX(IAsyncResult ar)
{
DelegateGiveMeCallback d = (DelegateGiveMeCallback)((AsyncResult)ar).AsyncDelegate;
object o = d.EndInvoke(ar);
buttonRM.Text = "Run";
}
private void asyncCallBackWS(IAsyncResult ar)
{
DelegateGiveMeCallback d = (DelegateGiveMeCallback)((AsyncResult)ar).AsyncDelegate;
object o = d.EndInvoke(ar);
buttonRM.Text = "Run";
}
The design implementation is the same for any remoting object. It's starting to create a callback delegator - RemoteCallback for the specified callback method (Progress). Secondly, the AsyncCallBack is initiated for the particular handler function (for instance; asyncCallBackA). This function is going to be called at the end of the remoting call. Next step is to create a delegator of the Remoting method for the asynchronous call. As a last step is invoking a delegator method using the BeginInvoke function.
Note that all calls are processing without any blocking, so the Windows Control thread can be yielded and be ready to process any event such as callbacks, user interface, asyncCallBacks, etc. When the remoting is done, the post-call function (handler) is called to finish and resulting the remoting method.
RemoteWindowsForm.exe.config
This is a client configuration file. As you can see it's a different from the server side and also from the standard one. There is no section for the system.runtime.remoting. I created a custom section Client/urlAddress where located an objecturi address of the remote object which client wants to talk.
<configuration>
<configSections>
<sectionGroup name="Client">
<section name="urlAddress" type="System.Configuration.NameValueSectionHandler,System" />
</sectionGroup>
</configSections>
<Client>
<urlAddress>
<add key="objectA" value="tcp://localhost:12345/RemoteTest/ObjectA" />
<add key="objectX" value="tcp://localhost:12345/RemoteTest/ObjectX" />
<add key="objectWS" value="http://localhost/RemoteObjectWS/RemoteObjectWS.soap" />
</urlAddress>
</Client>
</configuration>
Test
Testing of the Remoting Callbacks solution needs to install properly Web Service and .Net Service projects. The package has included a batch files to make their installation easy. Please, look at them an change their pathname related to your setup environment if it's necessary. After that, launch the client program (RemoteWindowsForm) and you should see the following form:
The Echo Message has been nested trough all of three remote objects. This is an indication that your connectivity with the remote objects are valid. Now click the button Run to process the remoting callbacks.
As you can see, each remote object will notify its control on the Window Form such as the progress bar, text and button colors. Pressing the button Abort the remoting process will be aborted on the all remoting objects. After these simple test, lunch more clients (for instance five) and repeat the above test simultaneously for all of them. I hope everything is working well like on my machine.
Conclusion
Using the Remoting Callback in the .Net Application enhancing its event driven model, making more pre-emptive and realistic. As typically example is downloading a large file or query database. In this article I described the capability of the .Net Remoting and Reflexion which they play very significant role in the .Net Enterprise Solutions.
上一篇:UTF-8说分明