Answered by:
Asynchronous C# component

Question
-
Hi all, I am doing a simple C# component. In component, I will create a folder, after create successfully, I will return a string to notify. But the problem is I can't notify when it complete. I can create a folder but always notify before it complete. I post my code below. private async Task createNewCaching(string productId) { mFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync(productId,CreationCollisionOption.OpenIfExists); } public String getFolderPath(string productId) { //createNewCaching(productId); Task t = createNewCaching(productId); while (mFolder == null) Thread.Sleep(1000); if (mFolder != null) return mFolder.Path.ToString(); else return "not created"; }Friday, November 25, 2011 7:46 AM
Answers
-
Vivian,
I outlined how to write a C# component and call async methods on it here:
Is this similar to what you are trying to do?
-Jeff
Jeff Sanders (MSFT)- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, November 30, 2011 8:34 PM
- Marked as answer by Vivian Ng Thursday, December 1, 2011 9:59 AM
Wednesday, November 30, 2011 8:34 PMModerator -
As jpsanders said, please refer to the link he provided. You could modify your C# as below, to create a WinRT class that can be invoked in JS:
public sealed class Helper { static StorageFolder mFolder; private static async Task<StorageFolder> createNewCaching(string productId) { return await KnownFolders.PicturesLibrary.CreateFolderAsync(productId, CreationCollisionOption.OpenIfExists); } private static async Task<String> getFolderPath(string productId) { mFolder = await createNewCaching(productId); if (mFolder != null) return mFolder.Path.ToString(); else return "not created"; } public IAsyncOperation<string> GetFolderPath(string productID) { return AsyncInfoFactory.Create(() => getFolderPath(productID)); } }
and invoke in JS:(new NameSpace.Helper).getFolderPath("abc").then(
function (p) {
var result = p;
}
);
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Vivian Ng Thursday, December 1, 2011 9:59 AM
Thursday, December 1, 2011 5:27 AM
All replies
-
Sorry, I re-post my code.
private async Task createNewCaching(string productId) { mFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync(productId, CreationCollisionOption.OpenIfExists); } public String getFolderPath(string productId) { //createNewCaching(productId); Task t = createNewCaching(productId); if (mFolder != null) return mFolder.Path.ToString(); else return "not created"; }
Friday, November 25, 2011 7:48 AM -
Design your method as below:
StorageFolder mFolder; private async Task<StorageFolder> createNewCaching(string productId) { return await KnownFolders.PicturesLibrary.CreateFolderAsync(productId, CreationCollisionOption.OpenIfExists); } public async Task<String> getFolderPath(string productId) { mFolder = await createNewCaching(productId); if (mFolder != null) return mFolder.Path.ToString(); else return "not created"; }
And call it via the await key word.
var result = await getFolderPath(...);
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- Edited by Jie Bao Tuesday, November 29, 2011 9:35 AM
Tuesday, November 29, 2011 9:29 AM -
Hi Bob,
I think it's not correct solution. I want to call function getFolderPath from javascript. But if you design like this, we cannot pass result to javascript. My idea is C# component do asynchronous function, and notify to javascript when complete. Is it possible? And how to do?
Thanks,
Vivian
Wednesday, November 30, 2011 9:18 AM -
Vivian, why do you want to write a C# WinMD object to do this? Why don't you keep all of your code javascript?
-Jeff
Jeff Sanders (MSFT)Wednesday, November 30, 2011 1:17 PMModerator -
Vivian,
I outlined how to write a C# component and call async methods on it here:
Is this similar to what you are trying to do?
-Jeff
Jeff Sanders (MSFT)- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, November 30, 2011 8:34 PM
- Marked as answer by Vivian Ng Thursday, December 1, 2011 9:59 AM
Wednesday, November 30, 2011 8:34 PMModerator -
Hi Jeft,
I will try it in my case.
Thanks,
Vivian
Thursday, December 1, 2011 3:56 AM -
Hi Jeft,
I am confusing with 2 solutions. The first solution I will do all in javascript, but it seems to be loose control when the program have a lot of downloads and cache functions. So I try the second solutions, using C# to do everything about cache and download, after that provide data to javascript.
VivianThursday, December 1, 2011 5:23 AM -
As jpsanders said, please refer to the link he provided. You could modify your C# as below, to create a WinRT class that can be invoked in JS:
public sealed class Helper { static StorageFolder mFolder; private static async Task<StorageFolder> createNewCaching(string productId) { return await KnownFolders.PicturesLibrary.CreateFolderAsync(productId, CreationCollisionOption.OpenIfExists); } private static async Task<String> getFolderPath(string productId) { mFolder = await createNewCaching(productId); if (mFolder != null) return mFolder.Path.ToString(); else return "not created"; } public IAsyncOperation<string> GetFolderPath(string productID) { return AsyncInfoFactory.Create(() => getFolderPath(productID)); } }
and invoke in JS:(new NameSpace.Helper).getFolderPath("abc").then(
function (p) {
var result = p;
}
);
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Vivian Ng Thursday, December 1, 2011 9:59 AM
Thursday, December 1, 2011 5:27 AM -
Hi Jeft and Bob,
It work now. Thank you very much.
Vivian
Thursday, December 1, 2011 9:58 AM