• Create Account

### #ActualGavin Williams

Posted 20 January 2013 - 10:54 PM

Hi,

I've started using async / await as I'm taking a look at WinRT. But Im having a terrible time getting to grips with async usage. Suddenly all of my objects have 3 'constructor' methods ...

public someModel(string modelAsText) // the constructor
: base(modelAsText, VertexElementTypes.Position, VertexElementTypes.Normal, VertexElementTypes.Texcoord)
{
// now often empty
}

public override void Initialize(DeviceManager devices) // called when graphics data needs to be loaded and bound to the device
{
// setup any programmatic/fast data
....
LoadData(devices) // calls the async method
}

{
...
IsReady = true; // flag indicating that all data is now loaded.
}

One thing that's starting to annoy me about async is that all my constructors are emptying. And all the construction code is having to be moved into the Initialize and async LoadData methods. I can't understand why a constructor can't be asynchronous. In my mind, it absolutely should be because like any object being constructed, it is immediately identfiable but isn't ready to be used until all its parts are in place. It seems to me that the language isn't designed correctly to handle this. I've read comments on the topic of async in constructors and I understand the reasoning .. early disposal causing leaks and object usage before its ready, but I would argue that the reasoning is only necessary because the model is flawed. That discussion isn't really helping me solve the problem, but I think it's a part of the topic.

Another problem I am seeing is that even though the file accesses are asynchronous, the awaits in LoadData cause the LoadData method to be synchronous (don't they ?) and therefore the thread has to wait anyway for LoadData to complete, so what's the point. The initialize method in this case is synchronous, so all the asyncronous code in the world can't make the drive load data any faster.

I'm really not sure about this, because when I run the app, the GUI does come up and the model pops into existence when it's loaded. So it actually seems to be doing what I want. And if i remove the IsReady flag I get a few frames where the states and shaders aren't set yet. So it seems to be running asynchronously !

Q : does the LoadData call create it's own thread and does execution immediately continue ( in this case, returning from the Initialize method immediately ) while the LoadData method contiues on it's way ?

I'd like to know what patterns people are using to initialize models and resources asynchronously.

Thanks for any help or advice.  Gavin

### #3Gavin Williams

Posted 20 January 2013 - 10:53 PM

Hi,

I've started using async / await as I'm taking a look at WinRT. But Im having a terrible time getting to grips with async usage. Suddenly all of my objects have 3 'constructor' methods ...

public someModel(string modelAsText) // the constructor
: base(modelAsText, VertexElementTypes.Position, VertexElementTypes.Normal, VertexElementTypes.Texcoord)
{
// now often empty
}

public override void Initialize(DeviceManager devices) // called when graphics data needs to be loaded and bound to the device
{
// setup any programmatic/fast data
....
LoadData(devices) // calls the async method
}

{
...
IsReady = true; // flag indicating that all data is now loaded.
}

One thing that's starting to annoy me about async is that all my constructors are emptying. And all the construction code is having to be moved into the Initialize and async LoadData methods. I can't understand why a constructor can't be asynchronous. In my mind, it absolutely should be because like any object being constructed, it is immediately identfiable but isn't ready to be used until all its parts are in place. It seems to me that the language isn't designed correctly to handle this. I've read comments on the topic of async in constructors and I understand the reasoning .. early disposal causing leaks and object usage before its ready, but I would argue that the reasoning is only necessary because the model is flawed. That discussion isn't really helping me solve the problem, but I think it's a part of the topic.

Another problem I am seeing is that even though the file accesses are asynchronous, the awaits in LoadData cause the LoadData method to be synchronous (don't they ?) and therefore the thread has to wait anyway for LoadData to complete, so what's the point. The initialize method in this case is synchronous, so all the asyncronous code in the world can't make the drive load data any faster.

I'm really not sure about this, because when I run the app, the GUI does come up and the model pops into existence when it's loaded. So it actually seems to be doing what I want. And if i remove the IsReady flag I get a few frames where the states and shaders aren't set yet. So it seems to be running asynchronously !

Q : does the LoadData call create it's own thread and does execution immediately continue ( in this case, returning from the Initialize method immediately ) while the LoadData method contiues on it's way ?

I'd like to know what patterns people are using to initialize models and resources asynchronously.

Thanks for any help or advice.  Gavin

### #2Gavin Williams

Posted 20 January 2013 - 10:51 PM

Hi,

I've started using async / await as I'm taking a look at WinRT. But Im having a terrible time getting to grips with async usage. Suddenly all of my objects have 3 'constructor' methods ...

public someModel(string modelAsText) // the constructor
: base(modelAsText, VertexElementTypes.Position, VertexElementTypes.Normal, VertexElementTypes.Texcoord)
{
// now often empty
}

public override void Initialize(DeviceManager devices) // called when graphics data needs to be loaded and bound to the device
{
// setup any programmatic/fast data
....
LoadData(devices) // calls the async method
}

{
...
IsReady = true; // flag indicating that all data is now loaded.
}

One thing that's starting to annoy me about async is that all my constructors are emptying. And all the construction code is having to be moved into the Initialize and async LoadData methods. I can't understand why a constructor can't be asynchronous. In my mind, it absolutely should be because like any object being constructed, it is immediately identfiable but isn't ready to be used until all its parts are in place. It seems to me that the language isn't designed correctly to handle this. I've read comments on the topic of async in constructors and I understand the reasoning .. early disposal causing leaks and object usage before its ready, but I would argue that the reasoning is only necessary because the model is flawed. That discussion isn't really helping me solve the problem, but I think it's a part of the topic.

Another problem I am seeing is that even though the file accesses are asynchronous, the awaits in LoadData cause the LoadData method to be synchronous (don't they ?) and therefore the thread has to wait anyway for LoadData to complete, so what's the point. The initialize method in this case is synchronous, so all the asyncronous code in the world can't make the drive load data any faster.

I'm really not sure about this, because when I run the app, the GUI does come up and the model pops into existence when it's loaded. So it actually seems to be doing what I want. And if i remove the IsReady flag I get a few frames where the states and shaders aren't set yet. So it seems to be running asynchronously !

Q : does the LoadData call create it's own thread and does execution immediately continue ( in this case, returning from the Initialize method immediately ) while the LoadData method contiues on it's way ?

I'd like to know what patterns people are using to initialize models and resources asynchronously.

### #1Gavin Williams

Posted 20 January 2013 - 10:49 PM

Hi,

I've started using async / await as I'm taking a look at WinRT. But Im having a terrible time getting to grips with async usage. Suddenly all of my objects have 3 'constructor' methods ...

public someModel(string modelAsText) // the constructor
{
// now often empty
}

public override void Initialize(DeviceManager devices) // called when graphics data needs to be loaded and bound to the device
{
// setup any programmatic/fast data
....
LoadData(devices) // calls the async method
}

{
...
IsReady = true; // flag indicating that all data is now loaded.
}

One thing that's starting to annoy me about async is that all my constructors are emptying. And all the construction code is having to be moved into the Initialize and async LoadData methods. I can't understand why a constructor can't be asynchronous. In my mind, it absolutely should be because like any object being constructed, it is immediately identfiable but isn't ready to be used until all its parts are in place. It seems to me that the language isn't designed correctly to handle this. I've read comments on the topic of async in constructors and I understand the reasoning .. early disposal causing leaks and object usage before its ready, but I would argue that the reasoning is only necessary because the model is flawed. That discussion isn't really helping me solve the problem, but I think it's a part of the topic.

Another problem I am seeing is that even though the file accesses are asynchronous, the awaits in LoadData cause the LoadData method to be synchronous (don't they ?) and therefore the thread has to wait anyway for LoadData to complete, so what's the point. The initialize method in this case is synchronous, so all the asyncronous code in the world can't make the drive load data any faster.

I'm really not sure about this, because when I run the app, the GUI does come up and the model pops into existence when it's loaded. So it actually seems to be doing what I want. And if i remove the IsReady flag I get a few frames where the states and shaders aren't set yet. So it seems to be running asynchronously !

Q : does the LoadData call create it's own thread and does execution immediately continue ( in this case, returning from the Initialize method immediately ) while the LoadData method contiues on it's way ?

I'd like to know what patterns people are using to initialize models and resources asynchronously.

PARTNERS