when I call Application.Run() on a form, this command halts the program until the form is closed, it seems. Is there another command I can use to create the开发者_Python百科 form without halting the program? Thanks
When you call Run
, it doesn't halt the program. It transfers control to the Windows Forms main message loop. Any further code you want to execute should be run either in a separate thread or in event handlers such as in the Form_Load.
myForm F = new myForm();
F.Show();
Having read through your post and comments, you say that you want to make an IRC application. I'm sure there are many ways to do this, but this is how I think I would do it.
I would make a library project that does all the IRC stuff in it's own thread. When a message is received, have it fire an event. Then provide methods for sending messages.
Then make the WinForm project that references the IRC library. Have the form make a reference to an instance of the class you made and register to all the events. Then whenever the form needs to send a message, it just calls the SendMessage
method. Whenever an event is fired, the IRC form will get it and display the information properly (such as putting the right message in the right chat room tab or in the server log or whatever).
Doing it this way will let you create a way to connect to several servers very easily as it makes the IRC stuff very reusable. And also you have the WinForm running the way it's supposed to be, and you don't have to hack around it.
Sure, you can -- just create a new thread and spin up the form on that thread -- but you're opening yourself to a lot of potential grief when you start multithreading UI elements.
Could you explain why it is that you want to keep processing? It may be that you'd be better off spinning up a worker thread to do your background tasks instead of using the main thread.
Edit: Based on your elaboration, I think you may be approaching this from the wrong direction.
From Windows' perspective, your main form is essentially your application. The Application.Run call is initiating a Windows message loop which handles the processing and dispatch of communication between the application and operating system. This is the "core loop" of the program and is where all of the UI processing is done.
As a general rule, you don't want to mess with the message loop unless you have tasks which need to take place constantly -- you want them to occupy 100% of the CPU if possible. (A good example is the simulation and render loop in a computer game; typically, you want it to render updates as quickly as it can, using the full resources of the computer.)
In your case, what you're interested in is background communications processing, a process which is independent of the UI and needs to run as long as the application is running. This is a good candidate for a worker thread to be spawned by the application during its initialization process. While a "purist" separation model might suggest putting this spinup in your app main() function, I would suggest that you treat your main Form_Load event as your "main" for all intents and purposes. You can create a communications manager class and initialize it on a background thread during the Load event, allowing it to process data in the background and signal events for the UI to respond to.
This will show the Form without halting the program
new Form().Show()
Code would be helpful here please. Normally the Application.Run() call is generated by visual studio when you start a windows.forms project in the program.cs file. You shouldn't need to call this yourself anywhere.
If that's not your problem you may have code in your form that has a tight loop bug.
精彩评论