0

I am new to Windows programming and trying to understand a client written in C#. This client listens to messages from a server and handles them. In this case, it is using C# application, forms and controls that I don't understand. Following is a sample code snippet. It creates an application context that I believe gets called when form is closed. It then connects to a server which sends this client messages and the callbackInterface is used to handle those messages. It creates a form and runs the application with the context created earlier. I believe this part runs a message loop until the form is closed.

Now I am trying to understand what is the point of creating these application context and forms and how they are being used in this case. Can someone help me understand the flow of things here and what role is played by these GUI controls? The client runs entirely in the background with no GUI interface. What is the advantage of using these controls over having a typical client application without these controls that just polls for server messages in a loop?

void start() {
// get application context
context = new ApplicationContext();

// connect to a server
connect(name, callbackInterface,...);

// creates a form
cform = new Form();
cform.createControl();
int dummy = cform.Handle.ToInt32();

Application.run(context);

// message loop has returned
disconnect(...)

}
4

1 回答 1

0

在设计 WinForms 时,很容易将大量代码放在代码隐藏中。有时很多业务逻辑都被埋在那里。不是推荐的做法,但它可能很常见。如果您有这样的表单并希望重用业务逻辑但不需要 GUI,您可能会采用您所看到的方法。只是一个猜测,但您的问题中没有足够的细节来做更多的事情。

于 2012-09-15T22:16:54.743 回答