Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AsyncDelegate CallBack Thread-Safety problem...

Posted on 2006-07-18
8
Medium Priority
?
634 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

971 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