Solved

AsyncDelegate CallBack Thread-Safety problem...

Posted on 2006-07-18
8
622 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

25 Experts available now in Live!

Get 1:1 Help Now