Click here to Skip to main content
15,897,184 members
Home / Discussions / C#
   

C#

 
QuestionBeginReceive: How do I get out of the thread when reachig callback function? Pin
Kurt23-Oct-08 23:41
Kurt23-Oct-08 23:41 
AnswerRe: BeginReceive: How do I get out of the thread when reachig callback function? Pin
Simon P Stevens24-Oct-08 0:20
Simon P Stevens24-Oct-08 0:20 
GeneralRe: BeginReceive: How do I get out of the thread when reachig callback function? Pin
Kurt24-Oct-08 0:35
Kurt24-Oct-08 0:35 
GeneralRe: BeginReceive: How do I get out of the thread when reachig callback function? Pin
Simon P Stevens24-Oct-08 0:42
Simon P Stevens24-Oct-08 0:42 
GeneralWhy don't we need invoke events of controls? Pin
Kurt24-Oct-08 1:41
Kurt24-Oct-08 1:41 
GeneralRe: Why don't we need invoke events of controls? Pin
Simon P Stevens24-Oct-08 1:53
Simon P Stevens24-Oct-08 1:53 
AnswerRe: BeginReceive: How do I get out of the thread when reachig callback function? Pin
Gideon Engelberth24-Oct-08 3:04
Gideon Engelberth24-Oct-08 3:04 
AnswerRe: BeginReceive: How do I get out of the thread when reachig callback function? [modified] Pin
Alan N24-Oct-08 8:13
Alan N24-Oct-08 8:13 
Hi Kurt,
The System.ComponentModel namespace contains the AsyncOperation class which enables a thread to raise events on the GUI thread. I won't try and modify your code as my knowledge of network programming is minimal but instead I present a simple example showing the basics.

using System;
using System.ComponentModel;
using System.Threading;

namespace WorkerThreadEvent {
  class EventThread {
    private AsyncOperation operation;

    // Raised by the thread to report progress
    public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

    private void OnProgress(Object progress) {
      EventHandler<ProgressChangedEventArgs> handler = ProgressChanged;
      if (handler != null) {
        ProgressChangedEventArgs args = new ProgressChangedEventArgs((int)progress, null);
        handler(this, args);
      }
    }

    // Start the worker thread by calling Run from the GUI thread
    public void Run() {
      operation = AsyncOperationManager.CreateOperation(null);
      ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), null);
    }

    private void ThreadProc(Object state) {
      try {
        for (int i = 1; i <= 100; i++) {
          // simulate work
          Thread.Sleep(100);
          // raise an event on the GUI thread
          operation.Post(new SendOrPostCallback(OnProgress), i);
        }
      } finally {
        // end the asynchronous operation
        operation.OperationCompleted();
      }
    }

  }
}

The only real changes to standard event coding are
1) Creation of an AsyncOperation
2) Use of the operation's Post method to marshal the event to the thread that called AsyncOperationManager.CreateOperation(null)
3) Ending the operation correctly when it no longer needed

You can incorporate this class in a simple winforms app and you'll find that the ProgressChanged event is raised on the GUI thread and there will be no need to use invoke to update the display.

I use the AsyncOperation quite often now as it really simplifies UI coding in a multithreaded programme and I'm sure that many more people would too if only the documentation on MSDN was better. An example like the one above might help!

Alan.

By the way I think that the technique I have shown is similar to the way that the BackgroundWorker class marshals events to the GUI thread.

And it would be remiss of me not to reference this excellent article by Leslie Sanford.
http://www.codeproject.com/KB/cpp/SyncContextTutorial.aspx[^]

modified on Friday, October 24, 2008 2:29 PM

AnswerSolution incl. example :-) Pin
Kurt27-Oct-08 0:50
Kurt27-Oct-08 0:50 
QuestionOpen-source C# shopping cart Pin
Andrey Mazoulnitsyn23-Oct-08 22:49
Andrey Mazoulnitsyn23-Oct-08 22:49 
QuestionAccess different variable names on the fly Pin
J-Cod3r23-Oct-08 20:55
J-Cod3r23-Oct-08 20:55 
AnswerRe: Access different variable names on the fly Pin
Eduard Keilholz23-Oct-08 21:44
Eduard Keilholz23-Oct-08 21:44 
AnswerRe: Access different variable names on the fly Pin
N a v a n e e t h23-Oct-08 22:42
N a v a n e e t h23-Oct-08 22:42 
AnswerRe: Access different variable names on the fly Pin
PIEBALDconsult24-Oct-08 4:32
mvePIEBALDconsult24-Oct-08 4:32 
QuestionAuto Generate Customer ID Pin
nt_virus23-Oct-08 18:12
nt_virus23-Oct-08 18:12 
AnswerRe: Auto Generate Customer ID Pin
AhsanS23-Oct-08 20:16
AhsanS23-Oct-08 20:16 
AnswerRe: Auto Generate Customer ID Pin
N a v a n e e t h23-Oct-08 20:42
N a v a n e e t h23-Oct-08 20:42 
AnswerRe: Auto Generate Customer ID Pin
V.23-Oct-08 22:24
professionalV.23-Oct-08 22:24 
GeneralRe: Auto Generate Customer ID Pin
sumit703423-Oct-08 22:43
sumit703423-Oct-08 22:43 
GeneralRe: Auto Generate Customer ID Pin
N a v a n e e t h23-Oct-08 22:45
N a v a n e e t h23-Oct-08 22:45 
GeneralRe: Auto Generate Customer ID Pin
sumit703423-Oct-08 22:54
sumit703423-Oct-08 22:54 
GeneralRe: Auto Generate Customer ID PinPopular
V.23-Oct-08 23:10
professionalV.23-Oct-08 23:10 
QuestionBrowser Wrangler Idea - Direction Needed Pin
iceman347923-Oct-08 17:53
iceman347923-Oct-08 17:53 
QuestionDataGrid .. DataSet .. Datatable Pin
chicago7623-Oct-08 11:15
chicago7623-Oct-08 11:15 
AnswerRe: DataGrid .. DataSet .. Datatable Pin
PIEBALDconsult23-Oct-08 12:42
mvePIEBALDconsult23-Oct-08 12:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.