Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AsyncDelegate CallBack Thread-Safety problem...

Posted on 2006-07-18
8
Medium Priority
?
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

730 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