Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From an architecture point of view:</p> <p>Message Queues decouple bits of your application. You can in Windows Forms applications rely on the Message Queue that Windows creates and manages for you. Google for PostMessage/GetMessage etc. This is generally called "message passing".</p> <p><strong>Typical Arcitecture</strong></p> <ul> <li>One part of your app "pushes" a request into a queue</li> <li>Some other part of your app "pulls" a request from a queue and writes a result to a second queue.</li> <li>The first part can then "pull" requests from the second "results" queue and display to a user.</li> </ul> <p>So it looks like this:</p> <p>App --> REQUESTS QUEUE --> processing engine --> RESULTS QUEUE --> App</p> <p>The processing engine could be in the same app, on the same thread or in a different thread/process (or even different machine).</p> <p>You can use simple queues : say a <code>Queue&lt;string&gt;()</code> (as long as you use locks to access it) or increase complexity or more and more complex/functional queues.</p> <p><strong>Issues with the naive strategy and other solutions ... things to think about:</strong></p> <ol> <li>What happens if you make a new request while the old one has not yet completed? </li> <li>What happens if an error occurs? Do you want errors inline? You can use another queue for errors?</li> <li>Do you want retries?</li> <li>What happens if a message is lost? (i.e. a request was pushed, but no response comes in...)? There are transactional queues etc.</li> </ol> <p><strong>Sample Code</strong></p> <pre><code> object oLock = new object(); Queue&lt;string&gt; requests = new Queue&lt;string&gt;(); Queue&lt;string&gt; responses = new Queue&lt;string&gt;(); Thread mThread; AutoResetEvent mEvent = new AutoResetEvent(false); public Form1() { InitializeComponent(); mThread = new Thread(ProcessingEngine); mThread.IsBackground = true; mThread.Start(); } private void ProcessingEngine() { string result; string request = null; while (true) { try { mEvent.WaitOne(); lock (oLock) { request = requests.Dequeue(); } var wc = new WebClient(); result = wc.DownloadString(request); lock (oLock) { responses.Enqueue(result); } } catch (Exception ex) { lock (oLock) { responses.Enqueue(ex.ToString()); } } } } private void timer1_Tick(object sender, EventArgs e) { lock (oLock) { //Stick in a new request requests.Enqueue("http://yahoo.com"); //Allow thread to start work mEvent.Set(); //Check if a response has arrived if (responses.Any()) { var result = responses.Dequeue(); listBox1.Items.Add(result.Substring(1,200)); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload