Click here to Skip to main content
15,894,405 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Code obfuscation Pin
N a v a n e e t h25-Aug-09 20:04
N a v a n e e t h25-Aug-09 20:04 
QuestionWinXP Home Can't Use Subfolder as Source for Picture Pin
Hypermommy25-Aug-09 8:58
Hypermommy25-Aug-09 8:58 
AnswerRe: WinXP Home Can't Use Subfolder as Source for Picture Pin
Henry Minute26-Aug-09 5:08
Henry Minute26-Aug-09 5:08 
GeneralRe: WinXP Home Can't Use Subfolder as Source for Picture Pin
Hypermommy26-Aug-09 5:11
Hypermommy26-Aug-09 5:11 
GeneralRe: WinXP Home Can't Use Subfolder as Source for Picture Pin
Henry Minute26-Aug-09 5:19
Henry Minute26-Aug-09 5:19 
GeneralRe: WinXP Home Can't Use Subfolder as Source for Picture Pin
Hypermommy26-Aug-09 6:09
Hypermommy26-Aug-09 6:09 
QuestionInstall software microphone Pin
TripShock24-Aug-09 2:11
TripShock24-Aug-09 2:11 
QuestionWhy does backgroundworker complete event is fired in other thread, instead of the one started it? [modified] Pin
Carl00723-Aug-09 22:53
Carl00723-Aug-09 22:53 
I have a backgroundworker on my form, I start it from the UI thread.
Then it would start a worker thread to do network-consuming task.

private void btnOK_Click(object sender, EventArgs e)
{
DisableForm();
Object obj = GetObjFromUI();

BackgroundWorker saveAsWorker = new BackgroundWorker();
saveAsWorker.DoWork += new DoWorkEventHandler(saveAsWorker_DoWork);
saveAsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(saveAsWorker_RunWorkerCompleted);
saveAsWorker.RunWorkerAsync(obj);
}


After the task completed, it would return to the UI thread and modify related UI, in the Complete event, it worked very well before.

But now I found some times, the method saveAsWorker_RunWorkerCompleted will return to other thread, which is nor the UI thread, neither the worker thread which run the network-consuming task. So the cross-thread exception will be thrown, when the system try to modify related UI in the method saveAsWorker_RunWorkerCompleted.

I have to write code below, to avoid the cross-thread exception.
void saveAsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Check if we should invoke
if (InvokeRequired)
{
// Invoke
Invoke(new MyEventHandler(MyObject_MyEvent), new object[] { sender, e });
}
else{
// Update UI here.
}
}


But I do not want to call the "if (InvokeRequired)", as before, this method "saveAsWorker_RunWorkerCompleted" was indeed excuted in the UI thread, and I am sure the thread which constructed the Backgroundworker and called RunWorkerAsync() was indeed UI thread. Why does the complete method return to "the 3rd thread" now?

Who knows why? Is any system configuration, for windows and .NET framework?

modified on Monday, August 24, 2009 9:48 PM

AnswerRe: Why does backgroundworker complete event is fired in other thread, instead of the one started it? Pin
Alan N24-Aug-09 1:17
Alan N24-Aug-09 1:17 
AnswerRe: Why does backgroundworker complete event is fired in other thread, instead of the one started it? Pin
Luc Pattyn24-Aug-09 1:46
sitebuilderLuc Pattyn24-Aug-09 1:46 
AnswerRe: Why does backgroundworker complete event is fired in other thread, instead of the one started it? Pin
Luc Pattyn26-Aug-09 0:05
sitebuilderLuc Pattyn26-Aug-09 0:05 
QuestionGetting mscorlib.Authentication Failure error Pin
V K 223-Aug-09 21:13
V K 223-Aug-09 21:13 
QuestionVB.NET PropertyGrid Categories Pin
NFranks23-Aug-09 15:06
NFranks23-Aug-09 15:06 
AnswerRe: VB.NET PropertyGrid Categories Pin
0x3c023-Aug-09 21:00
0x3c023-Aug-09 21:00 
GeneralRe: VB.NET PropertyGrid Categories Pin
NFranks24-Aug-09 4:04
NFranks24-Aug-09 4:04 
General[SOLVED] VB.NET PropertyGrid Categories Pin
NFranks24-Aug-09 5:25
NFranks24-Aug-09 5:25 
QuestionTimeout Error While Taking Database Back-up using Code Pin
VikashGohil21-Aug-09 19:44
VikashGohil21-Aug-09 19:44 
QuestionHow to detect when an application is about to exit Pin
CodeBerserker21-Aug-09 15:30
CodeBerserker21-Aug-09 15:30 
AnswerRe: How to detect when an application is about to exit [modified] Pin
Luc Pattyn21-Aug-09 16:17
sitebuilderLuc Pattyn21-Aug-09 16:17 
AnswerRe: How to detect when an application is about to exit Pin
VikashGohil21-Aug-09 22:05
VikashGohil21-Aug-09 22:05 
AnswerRe: How to detect when an application is about to exit Pin
Jack Vanderhorst22-Aug-09 12:02
Jack Vanderhorst22-Aug-09 12:02 
AnswerRe: How to detect when an application is about to exit Pin
qmartens22-Aug-09 19:01
qmartens22-Aug-09 19:01 
GeneralRe: How to detect when an application is about to exit Pin
Luc Pattyn22-Aug-09 22:28
sitebuilderLuc Pattyn22-Aug-09 22:28 
QuestionAllowPartiallyTrustedCallers not working on Shared Assembly Pin
Tristan Rhodes21-Aug-09 1:43
Tristan Rhodes21-Aug-09 1:43 
AnswerRe: AllowPartiallyTrustedCallers not working on Shared Assembly Pin
Dave Kreskowiak21-Aug-09 5:15
mveDave Kreskowiak21-Aug-09 5:15 

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.