Re: Async delegates, illegal cross thread UI control access

Ron Young <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <DOTNET-WINFORMS%[email protected]>
Thanks everyone for their reply, and doubt. I suppose I should have
mentioned I have a habit of running without debugging (Start->Start Without
Debugging). It seems that makes all the difference.

I created this little app:

          delegate string GetStringDelegate();

        private void button1_Click(object sender, EventArgs e)
        {
            Trace.WriteLine("button1_Click " +
                Thread.CurrentThread.ManagedThreadId.ToString());
            GetStringDelegate del = new GetStringDelegate(GetString);
            del.BeginInvoke(OnGetStringComplete, del);
        }

        private string GetString()
        {
            Trace.WriteLine("GetString " +
                Thread.CurrentThread.ManagedThreadId.ToString());
            return "Hello world";
        }

        private void OnGetStringComplete(IAsyncResult ar)
        {
            Trace.WriteLine("OnGetStringComplete " +
                Thread.CurrentThread.ManagedThreadId.ToString());
            GetStringDelegate del = ar.AsyncState as GetStringDelegate;
            button1.Text = del.EndInvoke(ar);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Trace.WriteLine("Main thread ID: " +
                Thread.CurrentThread.ManagedThreadId.ToString());
        }

Ran it without debugging, and didn't get the illegal cross thread call.
I ran a second time with debugging and got the the InvalidOperationException
At the EndInvoke, which is what I was looking for.

Any reason why the exception is getting swallowed when run with debugging
would be nice to know.

But thanks,

Ron Young

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps and
controls [mailto:[email protected]] On Behalf Of Fabian
Schmied
Sent: Friday, March 30, 2007 2:13 AM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Async delegates, illegal cross thread UI
control access

> Yeah, to be sure I set CheckForIllegal.... to true in Form_Load. I
remember
> when I starting VS2005 and did some async calls which ultimately updated
the
> UI, that I got that warning and thought that was pretty conveniant, and it
> taught me the correct way to do it. But now, I can't get that warning
back.

When you use "pictureBox.Refresh()" instead of "pictureBox.Image =
image", does this cause the error to appear?

If yes, there are two possibilities:
1 - It could be a small bug or oversight (i.e. they just forgot to add
that check), or
2 - It could be actually legal to set pictureBox.Image from different
threads, at least in the current implementation.

A quick glance via Reflector shows that the PictureBox actually does a
lot of locking, as itself seems to use a background thread for
animation. However, it also shows that not all of the code is
protected against multi-threading; therefore, I'd vote for version 1.
And even if 2 were correct, it's not documented and thus could not be
relied upon.

Fabian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.