Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
QuestionC# threads and invoking Pin
Blue3652-Jul-09 6:15
Blue3652-Jul-09 6:15 
AnswerRe: C# threads and invoking Pin
Henry Minute2-Jul-09 6:44
Henry Minute2-Jul-09 6:44 
GeneralRe: C# threads and invoking Pin
Luc Pattyn2-Jul-09 6:50
sitebuilderLuc Pattyn2-Jul-09 6:50 
GeneralRe: C# threads and invoking Pin
Henry Minute2-Jul-09 6:56
Henry Minute2-Jul-09 6:56 
AnswerRe: C# threads and invoking Pin
Luc Pattyn2-Jul-09 6:49
sitebuilderLuc Pattyn2-Jul-09 6:49 
GeneralRe: C# threads and invoking Pin
Blue3652-Jul-09 12:22
Blue3652-Jul-09 12:22 
GeneralRe: C# threads and invoking Pin
Luc Pattyn2-Jul-09 12:53
sitebuilderLuc Pattyn2-Jul-09 12:53 
AnswerRe: C# threads and invoking Pin
Alan N2-Jul-09 8:29
Alan N2-Jul-09 8:29 
Hi,
The System.ComponentModel.BackgroundWorker class raises it’s events on UI thread and would suit your requirements as you only require a task completed event.

In general terms it is possible to arrange for an event to be raised on the UI thread through use of either the Post or Send methods of the SynchronizationContext class or alternatively the Post method of the AsyncOperation class.

I’ve pasted in an example class that shows one way of doing this.

Alan.

using System;
using System.ComponentModel;
using System.Threading;
public class BackGroundTask {
  private SynchronizationContext context;
  public event EventHandler<AsyncCompletedEventArgs> Completed;

  /// <summary>
  /// Start the background task
  /// </summary>
  public void RunAsync(Object args) {
    this.context = AsyncOperationManager.SynchronizationContext;
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadProc), args);
  }

  private void ThreadProc(Object args) {
    Exception exc = null;
    Boolean cancelled = false;
    try {
      ////this.DoWork();
    } catch (Exception e) {
      exc = e;
    } finally {
      this.OnCompleted(exc, cancelled, null);
      this.context = null;
    }
  }

  private void OnCompleted(Exception e, Boolean cancelled, Object state) {
    // Using anonymous delegate to allow access to local variables
    this.context.Post(
      delegate {
        EventHandler<AsyncCompletedEventArgs> handler = this.Completed;
        if (handler != null) {
          AsyncCompletedEventArgs args = new AsyncCompletedEventArgs(e, cancelled, state);
          handler(this, args);
        }
      },
    null);
  }
}

QuestionGridView and making my site look like this page Pin
mjc2252-Jul-09 6:12
mjc2252-Jul-09 6:12 
AnswerRe: GridView and making my site look like this page Pin
Moim Hossain2-Jul-09 9:30
Moim Hossain2-Jul-09 9:30 
QuestionEnumerate Custom Class Pin
Dowse2-Jul-09 4:40
Dowse2-Jul-09 4:40 
AnswerRe: Enumerate Custom Class Pin
Thomas Weller2-Jul-09 5:36
Thomas Weller2-Jul-09 5:36 
GeneralRe: Enumerate Custom Class Pin
Dowse2-Jul-09 5:45
Dowse2-Jul-09 5:45 
GeneralRe: Enumerate Custom Class Pin
Thomas Weller2-Jul-09 6:07
Thomas Weller2-Jul-09 6:07 
GeneralRe: Enumerate Custom Class Pin
Dowse2-Jul-09 6:10
Dowse2-Jul-09 6:10 
AnswerRe: Enumerate Custom Class Pin
Luc Pattyn2-Jul-09 5:47
sitebuilderLuc Pattyn2-Jul-09 5:47 
AnswerRe: Enumerate Custom Class Pin
Dowse2-Jul-09 6:03
Dowse2-Jul-09 6:03 
AnswerRe: Enumerate Custom Class Pin
Dowse2-Jul-09 7:01
Dowse2-Jul-09 7:01 
AnswerRe: Enumerate Custom Class Pin
DaveyM692-Jul-09 5:58
professionalDaveyM692-Jul-09 5:58 
AnswerRe: Enumerate Custom Class Pin
harold aptroot2-Jul-09 6:44
harold aptroot2-Jul-09 6:44 
QuestionThreading Pin
Wogboiii2-Jul-09 4:23
Wogboiii2-Jul-09 4:23 
AnswerRe: Threading Pin
Luc Pattyn2-Jul-09 4:26
sitebuilderLuc Pattyn2-Jul-09 4:26 
AnswerRe: Threading Pin
led mike2-Jul-09 4:36
led mike2-Jul-09 4:36 
GeneralRe: Threading Pin
DaveyM692-Jul-09 6:01
professionalDaveyM692-Jul-09 6:01 
AnswerRe: Threading Pin
Thomas Weller2-Jul-09 5:48
Thomas Weller2-Jul-09 5:48 

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.