Solved

AsyncDelegate CallBack Thread-Safety problem...

Posted on 2006-07-18
8
621 Views
Last Modified: 2008-02-26

Hi!

I have an async delegate.
On the CallBack I need to set come properties on the form controls.

Doesn't callback already run o the main thread?
I'm having problems with that, raising an exception saying that the object is being changed from a different thread than the one it was created.

Thanks!
Alex
0
Comment
Question by:Alexandre Simões
  • 5
  • 3
8 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 17130130
If you need to set things on the form then use Invoke() method of the form class.
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17130161
Intead of typing a lot here is a good article explaining everything in detail.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17130299

Invoke makes the process Syncronous...
I need it to be Async.

I used the new BackgroundWorker Component and it worked without any problems.

My main question is "In which thread does an AsyncDelegate CallBack run? on the main thread or on the Delegate thread?"
Since... ever... I thought it wuns on the main thread...

Alex :p
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17133086
Right I was trying to point in in direction of the Invoke methods including 'BeginInvoke' which operates asynchrnously.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:devsolns
ID: 17133093
An asynchronous method runs on another thread different than the one currently executing, its just hidden from you.
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17133210
I know that an Asyncronous Delegate runs on another thread... that's why it's called Async...
What was suposed to be is the CallBack running on the main thread already...

And as it seems... it does not...
That's what I'm wondering why!

Alex
0
 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
ID: 17133524
My main question is "In which thread does an AsyncDelegate CallBack run? on the main thread or on the Delegate thread?"

Ok I think i see what your saying and yes the method callback would be running on the new thread that is processing the async method.  The callback is just a function pointer and whatever thread calls that (function pointer/delegate/callback) is the thread that will execute that function.


Run this to see,






using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;


    class Program
    {
        public delegate void FooDelegate();

        public void Foo()
        {
            Console.WriteLine("Thread id in Foo: {0}", AppDomain.GetCurrentThreadId());
        }

        public void FooCallBack(IAsyncResult ar)
        {
            Console.WriteLine("Thread id in FooCallBack: {0}", AppDomain.GetCurrentThreadId());
        }

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Thread id at main start: {0}", AppDomain.GetCurrentThreadId());
                Program p = new Program();
                FooDelegate fd = new FooDelegate(p.Foo);

                IAsyncResult ar = fd.BeginInvoke(new AsyncCallback(p.FooCallBack), null);

                fd.EndInvoke(ar);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Done");
                Console.ReadKey();
            }
        }    
    }
0
 
LVL 30

Author Comment

by:Alexandre Simões
ID: 17134317
Yep... I've done some testings too and conclude exactly the same thing...
I can tell you that I've been around with .net since its version 1.0 beta and always thought te callback would run on the main thread...

Thanks mate...
Alex
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now