Click here to Skip to main content
15,884,637 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Fayu8-Sep-10 4:22
Fayu8-Sep-10 4:22 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Keith Vitali8-Sep-10 4:34
Keith Vitali8-Sep-10 4:34 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Fayu8-Sep-10 5:19
Fayu8-Sep-10 5:19 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Keith Vitali8-Sep-10 5:23
Keith Vitali8-Sep-10 5:23 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Chris Trelawny-Ross8-Sep-10 5:39
Chris Trelawny-Ross8-Sep-10 5:39 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
HuntrCkr9-Sep-10 1:40
HuntrCkr9-Sep-10 1:40 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Luc Pattyn9-Sep-10 3:14
sitebuilderLuc Pattyn9-Sep-10 3:14 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
HuntrCkr9-Sep-10 3:35
HuntrCkr9-Sep-10 3:35 
In fact, it doesn't make a difference. Common misunderstanding. Let me first put in the code snippet
public void SetProgressValue(int value)
{
    if( formClosed == true )
    {
        return;
    }

    if (this.InvokeRequired)
    {
       ProgressValueDelegate pvd = new ProgressValueDelegate(SetProgressValue);                
       this.Invoke(pvd, new object[] { value });
    }
    else
    {
        m_progressBar.Value = value;
    }
}


When SetProgressValue is called from another thread, the first thing that happens is the check if the form is closed already. Let's take you hypothetical situation where a FormClosing event is queued, but has not been executed yet. formClosed is false, so now this.InvokeRequired is checked. It's true so a call to SetProgressValue is queued as well.
The FormClosing event fires, and formClosed is set to true. Next, the queued call to SetProgressValue occurs. First thing, we check formClosed which is now true, so the function is terminated immediately.

The common misconception is that Invoke simply transfers the call to the correct thread, but in fact the function is called all over again.
Another thing many people are not aware of is that the call to Invoke actually halts the thread until the Invoked delegate has been executed in the GUI thread, which can cause major slowdowns. Rather use BeginInvoke. It does the same thing in this scenario, but does not block the calling thread.
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Luc Pattyn9-Sep-10 3:50
sitebuilderLuc Pattyn9-Sep-10 3:50 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
HuntrCkr9-Sep-10 4:04
HuntrCkr9-Sep-10 4:04 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Peter Trevor9-Sep-10 4:57
Peter Trevor9-Sep-10 4:57 
GeneralRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Luc Pattyn9-Sep-10 9:39
sitebuilderLuc Pattyn9-Sep-10 9:39 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Fabio Franco9-Sep-10 3:44
professionalFabio Franco9-Sep-10 3:44 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
CodeHawkz9-Sep-10 6:10
CodeHawkz9-Sep-10 6:10 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
JOAT-MON9-Sep-10 11:17
JOAT-MON9-Sep-10 11:17 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Patrick Fox9-Sep-10 11:58
Patrick Fox9-Sep-10 11:58 
AnswerRe: C# threading question: Invoke() fails with ObjectDisposedException Pin
Jason J. Chase12-Sep-10 20:14
Jason J. Chase12-Sep-10 20:14 
QuestionBest solution for a form that looks like a paper page formular Pin
lvq6848-Sep-10 2:10
lvq6848-Sep-10 2:10 
AnswerRe: Best solution for a form that looks like a paper page formular Pin
Pete O'Hanlon8-Sep-10 2:44
mvePete O'Hanlon8-Sep-10 2:44 
GeneralRe: Best solution for a form that looks like a paper page formular Pin
lvq6848-Sep-10 10:21
lvq6848-Sep-10 10:21 
GeneralRe: Best solution for a form that looks like a paper page formular Pin
Pete O'Hanlon8-Sep-10 10:42
mvePete O'Hanlon8-Sep-10 10:42 
AnswerRe: Best solution for a form that looks like a paper page formular Pin
The Man from U.N.C.L.E.8-Sep-10 2:47
The Man from U.N.C.L.E.8-Sep-10 2:47 
QuestionMemory leaks while calling functions from another assembly. Pin
Aseem Sharma8-Sep-10 1:56
Aseem Sharma8-Sep-10 1:56 
AnswerRe: Memory leaks while calling functions from another assembly. Pin
Luc Pattyn8-Sep-10 2:27
sitebuilderLuc Pattyn8-Sep-10 2:27 
GeneralRe: Memory leaks while calling functions from another assembly. Pin
Aseem Sharma8-Sep-10 2:48
Aseem Sharma8-Sep-10 2: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.