question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoid using LoadAsync() method of PictureBox control

See original GitHub issue

Steps to reproduce:

  1. Create a new WinForms Core App.
  2. Add the following Code to the Form’s constructor.
public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.PictureBox pb = new PictureBox();
            pb.ImageLocation = @"\\winformssrvvm01\BAQAFiles\v-mikou\OGF Doc\picture\myLargeImage1.bmp";
            pb.LoadAsync();
        }
  1. Run the app.

Expected Result: LoadAsync should be executed. (NOTE: Although LoadAsync implies this, the method does not return Task but void, so the usage here is correct with regards to async/await.)

Actual Result: An exception pops up, please see following screenshot and exception log.

image

Exception log:

System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Operation is not supported on this platform.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.WaitCallback.BeginInvoke(Object state, AsyncCallback callback, Object object)
   at System.Windows.Forms.PictureBox.LoadAsync()
   at XpictureIssue.Form1..ctor() in C:\Users\v-zheshi\source\winforms\XpictureIssue\Form1.cs:line 20
   at XpictureIssue.Program.Main() in C:\Users\v-zheshi\source\winforms\XpictureIssue\Program.cs:line 19

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
ericstjcommented, Dec 17, 2018

Just to confirm, I believe this is intentional per https://github.com/dotnet/corefx/issues/5940#issuecomment-182107557 /cc @jkotas. You’ll need to make a fix to WinForms here to avoid this API. The comment here may not even apply any more on Core so perhaps you can just call BeginGetResponse https://github.com/dotnet/winforms/blob/408a31db5c58be99eff71ed94dfae6fda49364e0/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs#L671-L674 Alternatively just use the Task infrastructure to run the code on a background thread.

In addition: What do folks think about making this method Obsolete, and come up with a new one which does keep what its promising (namely returning Task and not void)?

We had similar goals back in .NETCore1.0 but dialed it back. In most cases it’s not worth breaking someone over just having a newer API. There should be some really good reason to force a dev to think about the change. “New” is not a good enough reason. Thus we still have all the old event-based and APM based API next to task based API.

It’s a reasonable goal to plumb task-based async API through WinForms, but that should be done in a wave impacting all async winforms APIs. That way you aren’t forcing people to learn two different async programming models. I can imagine that being a pretty huge task given that WinForms relies heavily on the event-based model.

2reactions
KlausLoeffelmanncommented, Dec 14, 2018

In addition: What do folks think about making this method Obsolete, and come up with a new one which does keep what its promising (namely returning Task and not void)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronously Load an Image from a Url to a PictureBox
PictireBox control supports loading images asynchronously itself and you don't need to use background worker or async/await. It also loads image ...
Read more >
PictureBox.LoadAsync Method (System.Windows.Forms)
A call to the LoadAsync method sets the ImageLocation property to the value of url . Besides calling the LoadAsync method, you must...
Read more >
Picturebox LoadAsync problem - vbCity - The .NET Developer ...
Basically, just create a blank project (which will contain form1), and add another form to the project named form2. In form1, add a...
Read more >
PictureBox doesn't work as expected
Hello! I am trying to create an application that displays random images from Reddit every X seconds in new Form.
Read more >
PictureBox load time notification
Within the LoadProgressChanged event you could call PictureBox.CancelAsync() to terminate the loading process, if necessary. Note: I have not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found