winforms - Not sure if I'm using "using" correctly c# in tftp app -
I am trying to use the pre-manufactured C # tftp server app with my Windows C # form. In the author server example, which works great, it uses the console app. When I try my console instance with my form in the app then it does not work (no error, just not connected) and I believe my problem is in the "use" statement:
By using (Var Server = New TftpServer ()) {server.OnReadRequest + = New TftpServerEventHandler (server_OnReadRequest); Server.OnWriteRequest + = New TftpServerEventHandler (server_OnWriteRequest); Server.Start (); Console.Read (); }
Not sure I understand correctly but I believe that console is Read ()
If this is the case, how do I apply equivalent to any form app? I just can not find my head around "use" Sorry, I'm new to C #.
The Windows Forms will always be open until they are explicitly closed. They always keep a thread to read the message queue for user input, so they will not exit the same way from an unnatural console application. In the form of Windows, we need to worry about multithreading and concurrency over console apps. It usually comes naturally, but not always.
Because of this, you can not actually use equal to the console. The user does not request to read ()
the use of the
settlement. If you have done so, then your form will only look insensitive.
However, you are in luck! By using a block in C #, syntax is nothing more than sugar, to remember to call
IDisposable.Dispose ()
to work with an object Later, in a form project, the object can be stored in a class-wide field equal to the server
object, then by calling the server. Disk ()
on, say There is a button. Click the
event. This, of course, is just an example. You can also do this on form.closing
if it feels more suitable.
High level, you want to do something like this:
- Your form class
TftpServer server;
. Declare a field in - Register a
load
event and whatever you want for yourserver
to work in your constructor. - Open the
server
field in theForm_Load
event. As events of -
Use the server
you can see very fit during the life of yourform
you can worry about concurrency Whether or not this is a case for another question. - Call
server.Dispose ()
in the form ofdissection
event.
In short,
class main: form {private TftpServer server; Public Main () {InitializeComponent (); this. Load + = main_load; Server = new TftpServer (); Server.OnReadRequest + = New TftpServerEventHandler (server_OnReadRequest); Server.OnWriteRequest + = New TftpServerEventHandler (server_OnWriteRequest); } Private Zero main_load (object sender, event aRGS) {server.start (); } Private zero server_onerequire (/ * I did not know about the argument here / /) Use the // read request: to give or receive data (depending on what "read" defines) Private Zero Server_OnrightText (/ * I make sure about debate) / // Use the write request: Give or receive your data (depending on what "write" defines) Safe Override Zero Remove (hair Dispojeng) {if (server! = Null) / / Since settlement can be called many times {server.Dispose (); Server = faucet; }}}
Comments
Post a Comment