Separation of GUI and logic in different threads in a Windows Form application

In a Windows Forms application, I want to separate the GUI from the logic. The user’s requests are complex and messaging, so I don’t want it to depend on the flow of the GUI. The structure should be something like this:

         GUI -----> splash screen ---------> User Form --------------> ...
               |  #
               |  create manager thread |  Show this Form
               # |
     Manager -----> Check user type -------> Wait for user command -> ...

What are your tips, tricks, or patterns for designs like this? Is this the right choice? Thank!

EDIT The manager thread must manage the graphical interface, and not vice versa. In addition, the Manager thread must run throughout the entire application.

+1
multithreading user-interface c # winforms
Aug 26 '09 at 9:53
source share
3 answers

Traditionally working the way it is done with BackgroundWorker

Basically, this is a simple class that gives you the ability to execute a function in a work thread, and then is automatically called back into the user interface thread after this function completes. During the execution of the function, the user interface is unlocked and can display a progress message or process other user input (for example, cancellation).

The result is similar to your template, but a separate thread is created and destroyed (well, actually there is a union ...) for each task.

 UI thread ---> Show splashscreen -------------------> Show window -------
                       |  | return to UI |
                       |  create background worker |  |
                       -> Process user ------------ -> Perform query etc.



Good, based on your comment:

You can use such a template, this is a simple eventing event. Give the user interface access to your manager so that he can make a method call on it and register events when the task is completed ( this link shows two basic patterns for asynchronous operations in .NET). Inside the manager, you will need to keep a list of tasks that can be executed sequentially in one thread and ensure that events that are called to return the results to the user interface are correctly called so that they are executed in the main user interface thread (basically recreate the background work pattern )

I'm not sure what you hope to get by doing this, is there a reason why an application should be limited to two threads? Are you worried about the cost of creating background workers? Do you need some kind of query system? The diagram examples in your question do not seem to require the complexity of this type of template.

+4
Aug 26 '09 at 10:04
source share

To complete your task, you must use a background worker .

It has events that will be very convenient. DoWork and RunWorkerCompleted, which is very convenient.

You can also try using Themes , they will look something like this: (Keep in mind that when you use Threads, if you want to do some work with the user interface thread, you must Invoke (), otherwise you will get a CrossThreadException)

private delegate void MyFunctionCaller(); //This will set up your worker thread and set it off private void SetThreadToDoWork() { ThreadStart threadStart = new ThreadStart(DoWork); Thread MyThread = new Thread(threadStart); MyThread.Start(); ShowSplashScreen(); } private void DoWork() { DoMyWork(); WorkCompleted(); } private void WorkCompleted() { if (InvokeRequired == true) { MyFunctionCaller InvokeCall = delegate { WorkCompleted(); }; Invoke(InvokeCall); } else { //Back to the UI thread now. } } 

OPs Edit

You can create a stream wherever you want, after starting it you can perform several functions. But keep in mind that you need Invoke () if you want to complete any work that only the user interface can do, otherwise you will get a CrossThreadException.

+2
Aug 26 '09 at 10:06
source share

I developed a similar project. The GUI launches the manager in a different topic. The manager has some way to get commands from the GUI. The GUI listens for dispatcher events, so it is updated only when the manager raises an event. To avoid cross-references, I used the Call methods

+1
Aug 26 '09 at 10:08
source share



All Articles