Solved

AsyncDelegate CallBack Thread-Safety problem...

Posted on 2006-07-18
8
628 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

623 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