Sicheres und einfaches multithreading in windows forms




















LoadAsync Documents how to load an image asynchronously. How to: Run an Operation in the Background Shows how to perform a time-consuming operation with the BackgroundWorker component. BackgroundWorker Component Overview Provides topics that describe how to use the BackgroundWorker component for asynchronous operations.

Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. Any additional feedback? Introduction I've come across a requirement, on a number of occasions, to produce a Windows application that interacts with a remote web server.

Background The main concept being introduced in this article is Windows Multi-threading. The technologies being demonstrated here are. XML messages. The threading concept is as follows.

Create a thread and set its various attributes When the thread completes its work, have it call back into a work-completed method in the main form thread and update the user as required. Setting things up The simple server - An MVC app In order to test our work, and not trigger a denial of service warning large number of multiple threads can do that!

And returns output XML like this. Create a model method that takes care of the logic of constructing the XML response to send back. These could be credit cards, websites to scrape, account numbers, etc. In reality, you can send any data you need to work with, and return, then, to the main calling application.

The threaded client - A Windows form app The client is visually quite simple. Everything is kicked off by the RunProcess button click event. We keep track of the start time, and update this when all processes are complete to test how long the process took.

We also, at this stage, call an Init method that sets things up for us, assigning some variables and filling the ListView with values. Let's now look at the sequential RunProcess method. This controls the main body of work for each web request.

The number of times to run the process is set by the value of edtTimesToRun. Text which is assigned to the variable RunTimes. The RunProcess method has a keyword of async - this is important as we are using the await keyword within the RunProcess method.

The important part of this code is SendWebRequest - this takes the input, queries the web server, and returns a value that we use to update the UI for the user. SendWebRequest is located in a separate shared. The shared. This is used to carry data between methods. The await keyword tells the code to sit there until with the HTTPClient comes back with a response, or an exception is raised.

We examine the response to ensure it is valid response. So, that is the basic sequential work-flow. Take a list of work items, iterate through them in sequence, call the web server, and parse back the xml response. The critical change is that instead of carrying out the WebRequest task on each loop sequentially, we are passing that task off to the method CreateWorkThread , passing in the required parameters. You will recall that in our main form, when we are creating each thread worker, we set up the thread object, then tell it to DoWork so this is the main kick-off method for the thread object.

The reason I am using a ThreadPool object is that creating threads is a very expensive operation therefore using the pool means that on completion, threads can be put into a pool to be reused.

After being added to the pool, we tell the thread to kick off the method PreformWork. And that is it, as you can see running threaded adds a bit more code, but dramatically improves performance. As I stated at the start of this article, this is but one way of handling a threaded application.

Thread pools have their advantages and disadvantages, you need to weigh up your goals and granular needs against the ease of use. If you are interested in this area you should also look at Background worker and if you want to harness the power that is in multi-core CPUs while threading the Task Parallel Library is a great way to go.

PS: If you found this article useful or downloaded the code please let me know by giving a rating below! Copy Code.



0コメント

  • 1000 / 1000