Click here to Skip to main content
15,891,248 members
Home / Discussions / C#
   

C#

 
GeneralRe: make a video tutorial Pin
OriginalGriff21-Jul-17 8:32
mveOriginalGriff21-Jul-17 8:32 
GeneralRe: make a video tutorial Pin
Member 1330981721-Jul-17 22:06
Member 1330981721-Jul-17 22:06 
GeneralRe: make a video tutorial Pin
OriginalGriff21-Jul-17 22:16
mveOriginalGriff21-Jul-17 22:16 
GeneralRe: make a video tutorial Pin
Member 1330981721-Jul-17 22:04
Member 1330981721-Jul-17 22:04 
AnswerRe: make a video tutorial Pin
BenScharbach12-Aug-17 9:32
BenScharbach12-Aug-17 9:32 
QuestionMessage Removed Pin
20-Jul-17 2:33
professionalEhsan Sajjad20-Jul-17 2:33 
AnswerMessage Removed Pin
20-Jul-17 4:00
mveOriginalGriff20-Jul-17 4:00 
GeneralMessage Removed Pin
20-Jul-17 4:05
professionalEhsan Sajjad20-Jul-17 4:05 
QuestionHow to call the load runner (LR) controller from visual studio Pin
M.perumal.samy18-Jul-17 21:53
M.perumal.samy18-Jul-17 21:53 
AnswerRe: How to call the load runner (LR) controller from visual studio Pin
Richard MacCutchan18-Jul-17 23:23
mveRichard MacCutchan18-Jul-17 23:23 
AnswerRe: How to call the load runner (LR) controller from visual studio Pin
BillWoodruff19-Jul-17 1:29
professionalBillWoodruff19-Jul-17 1:29 
QuestionSum columns from joined tables to get a calculated value using linq to SQL Pin
Member 1246241116-Jul-17 8:54
Member 1246241116-Jul-17 8:54 
AnswerRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
BillWoodruff16-Jul-17 11:25
professionalBillWoodruff16-Jul-17 11:25 
GeneralRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Member 1246241117-Jul-17 3:01
Member 1246241117-Jul-17 3:01 
AnswerRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Nathan Minier17-Jul-17 1:13
professionalNathan Minier17-Jul-17 1:13 
AnswerRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Richard Deeming17-Jul-17 1:27
mveRichard Deeming17-Jul-17 1:27 
GeneralRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Member 1246241117-Jul-17 12:15
Member 1246241117-Jul-17 12:15 
GeneralRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Mycroft Holmes17-Jul-17 14:18
professionalMycroft Holmes17-Jul-17 14:18 
GeneralRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Richard Deeming18-Jul-17 0:38
mveRichard Deeming18-Jul-17 0:38 
AnswerRe: Sum columns from joined tables to get a calculated value using linq to SQL Pin
Member 1246241118-Jul-17 10:56
Member 1246241118-Jul-17 10:56 
QuestionWriting to UI Thread in a Real time Application Pin
andycruce16-Jul-17 2:41
andycruce16-Jul-17 2:41 
AnswerRe: Writing to UI Thread in a Real time Application Pin
Gerry Schmitz16-Jul-17 4:49
mveGerry Schmitz16-Jul-17 4:49 
AnswerRe: Writing to UI Thread in a Real time Application Pin
Dave Kreskowiak16-Jul-17 5:28
mveDave Kreskowiak16-Jul-17 5:28 
AnswerRe: Writing to UI Thread in a Real time Application Pin
Alan N16-Jul-17 8:31
Alan N16-Jul-17 8:31 
The BackgroundWorker (BGW) uses a WindowFormsSynchronizationContext (SC) to invoke the completed and progress events on the UI thread. The SC comes from the thread that calls the RunWorkerAsync method.

A non UI thread typically has no SC and then the BGW operates what is sometimes called the "free threaded model" using a ThreadPool thread to fire the completed and progress events.

A BGW started from a new thread will behave properly if the thread is given the UI thread's SC.

Some example code, I hope the missing bits are obvious.
C#
private void StartFromThreadWithContext_Click(Object sender, EventArgs e) {
  log.WriteLine("UI thread {0}", Thread.CurrentThread.ManagedThreadId);
  Thread starterThread = new Thread(StarterThreadProc);
  starterThread.Start(SynchronizationContext.Current);
}

private void StarterThreadProc(Object context) {
  log.WriteLine("Starting BGW from thread {0}", Thread.CurrentThread.ManagedThreadId);
  WindowsFormsSynchronizationContext wfsc = context as WindowsFormsSynchronizationContext;
  if (wfsc != null) {
    SynchronizationContext.SetSynchronizationContext(wfsc);  // THE IMPORTANT BIT!!
  }
  log.WriteLine("Thread has {0}", SynchronizationContext.Current);
  worker.RunWorkerAsync();
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
  log.WriteLine("RunWorkerCompleted thread {0}", Thread.CurrentThread.ManagedThreadId);
}

private void Worker_DoWork(object sender, DoWorkEventArgs e) {
  log.WriteLine("DoWork thread {0}", Thread.CurrentThread.ManagedThreadId);
  Thread.Sleep(300);
}

Output to the log shows correct invocation of the completed event
UI thread 10
Starting BGW from thread 12
Thread has System.Windows.Forms.WindowsFormsSynchronizationContext
DoWork thread 7
RunWorkerCompleted thread 10

Alan.
SuggestionRe: Writing to UI Thread in a Real time Application Pin
BenScharbach12-Aug-17 10:17
BenScharbach12-Aug-17 10:17 

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.