Click here to Skip to main content
15,914,419 members
Home / Discussions / C#
   

C#

 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81713-Jun-06 14:19
acezrwild81713-Jun-06 14:19 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81713-Jun-06 14:25
acezrwild81713-Jun-06 14:25 
GeneralRe: Delegates throwing Null Exception Pin
gantww14-Jun-06 3:17
gantww14-Jun-06 3:17 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81714-Jun-06 8:14
acezrwild81714-Jun-06 8:14 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81713-Jun-06 8:53
acezrwild81713-Jun-06 8:53 
GeneralRe: Delegates throwing Null Exception Pin
Judah Gabriel Himango13-Jun-06 9:39
sponsorJudah Gabriel Himango13-Jun-06 9:39 
GeneralRe: Delegates throwing Null Exception Pin
Dustin Metzgar13-Jun-06 10:15
Dustin Metzgar13-Jun-06 10:15 
GeneralRe: Delegates throwing Null Exception Pin
Judah Gabriel Himango13-Jun-06 12:50
sponsorJudah Gabriel Himango13-Jun-06 12:50 
It really addresses a particular problem that encompasses events: SynchronizationContext are meant for doing things (such as calling methods, raising events, etc.) on a particular thread by either Posting (which corresponds to BeginInvoke for Windows Forms) or Sending (which corresponds to Invoke for Windows Forms).

Thus, you *could* make a library that can dispatch events on a UI thread without tying your library to that control, or even to Windows Forms, for that matter. For example:

class MyCrazyLib
{
   private SynchronizationContext syncContext;
   public event EventHandler WorkProgressed;

   public MyCrazyLib(SynchronizationContext eventRaisingSync)
   {
      this.syncContext = eventRaisingSync;
   }

   void DoWorkInBackground()
   {
      ThreadPool.QueueUserWorkItem(MyWorkMethod);
   }

   void MyWorkMethod(object state)
   {
      // Pretend we do some work here...
      ...

      // Now raise the event using our synchronization context.
      this.syncContext.Post(delegate(object state)
      {
         this.WorkProgressed(someSender, EventArgs.Empty);
      });
   }
}


There you've got yourself a library that *can* raise events on any thread, UI thread or otherwise. To use this with Windows Forms, you can simply do:

// While on the UI thread.
MyCrazyLib lib = new MyCrazyLib(WindowsFormsSynchronizationContext.Current); // We want to raise events on the current UI thread
lib.WorkProgressed += MyHandler;
lib.DoWorkInBackground();

...

void MyHandler(object sender, EventArgs e)
{
   // This will be called on the UI thread! No Invoke required. :)
}


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


GeneralRe: Delegates throwing Null Exception Pin
Dustin Metzgar13-Jun-06 15:31
Dustin Metzgar13-Jun-06 15:31 
AnswerRe: Delegates throwing Null Exception Pin
Dustin Metzgar13-Jun-06 7:43
Dustin Metzgar13-Jun-06 7:43 
GeneralRe: Delegates throwing Null Exception Pin
gantww13-Jun-06 7:49
gantww13-Jun-06 7:49 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81713-Jun-06 7:54
acezrwild81713-Jun-06 7:54 
GeneralRe: Delegates throwing Null Exception Pin
Dustin Metzgar13-Jun-06 8:04
Dustin Metzgar13-Jun-06 8:04 
GeneralRe: Delegates throwing Null Exception Pin
acezrwild81713-Jun-06 8:13
acezrwild81713-Jun-06 8:13 
GeneralRe: Delegates throwing Null Exception Pin
gantww13-Jun-06 8:21
gantww13-Jun-06 8:21 
GeneralRe: Delegates throwing Null Exception Pin
nicknotyet13-Jun-06 9:28
nicknotyet13-Jun-06 9:28 
QuestionCustom Tools Pin
gantww13-Jun-06 7:27
gantww13-Jun-06 7:27 
AnswerRe: Custom Tools Pin
gantww13-Jun-06 7:51
gantww13-Jun-06 7:51 
QuestionDelay of MDI [modified] Pin
mohamed fahmy13-Jun-06 6:57
mohamed fahmy13-Jun-06 6:57 
QuestionToolTip Bubble Incorrect location! Pin
cnich2313-Jun-06 6:30
cnich2313-Jun-06 6:30 
QuestionConsole.WriteLine ? Pin
AlexKiat13-Jun-06 6:13
AlexKiat13-Jun-06 6:13 
AnswerRe: Console.WriteLine ? Pin
Josh Smith13-Jun-06 8:29
Josh Smith13-Jun-06 8:29 
AnswerRe: Console.WriteLine ? Pin
Elina Blank13-Jun-06 9:59
sitebuilderElina Blank13-Jun-06 9:59 
Questionhow to close an application from another form? Pin
donkaiser13-Jun-06 5:49
donkaiser13-Jun-06 5:49 
AnswerRe: how to close an application from another form? Pin
led mike13-Jun-06 5:57
led mike13-Jun-06 5:57 

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.