Skip to content

Maybe the sample code is not working #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elda27 opened this issue May 4, 2020 — with docs.microsoft.com · 5 comments
Closed

Maybe the sample code is not working #2423

elda27 opened this issue May 4, 2020 — with docs.microsoft.com · 5 comments
Assignees
Labels

Comments

Copy link

elda27 commented May 4, 2020

I found following code and try it like this.

winrt::fire_and_forget RunAsync(DispatcherQueue queue)
{
    ... // Begin on the calling thread...
 
    co_await winrt::resume_foreground(queue);
 
    ... // ...resume on the dispatcher thread.
}

This sample code pass the DispatcherQueue to winrt::resume_foreground.
But the winrt::resume_foreground is not accept DispatcherQueue (I'm understanding to accept only CoreDispatcher and DispatcherQueue is not derrived CoreDispatcher.)

And also the following code block occured compile error.
I seem DispatcherQueue is not awaitable object. (don't have await_ready and etc).

winrt::fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...
 
    co_await queue;
 
    ...
}

What is the correct information in this page?
I'm newbee of WinRT so maybe this issue is misunderstanding.

My envrionment informations

  • Windows 10 x64 Build 1809
  • Visual Studio 2019 version 16.5.4

ドキュメントの詳細

このセクションを編集しないでください。 docs.microsoft.com で必須です ➟ GitHub の問題のリンク。

@stevewhims
Copy link
Member

Hi, @elda27, and thanks for filing this issue. The pattern shown in the topic is correct. You're right that you can pass a CoreDispatcher to winrt::resume_foreground (that implementation is in winrt/Windows.UI.Core.h). But you can also pass a DispatcherQueue to winrt::resume_foreground (that implementation is in winrt/Windows.System.h).

For the winrt::fire_and_forget RunAsync(DispatcherQueue queue) function, be sure to #include <winrt/Windows.System.h>, and using namespace winrt::Windows::System;`.

Thanks!
Steve

@stevewhims stevewhims added the product-question general "how do I do X" questions label May 5, 2020
@elda27
Copy link
Author

elda27 commented May 5, 2020

Thank you for reply.
I tried your answer but the problem isn't solved.

The resume_foreground accept by conversion operator to CoreDispatcher from DispatcherQueue.
So I need using namespace winrt::Windows::System;. (I seem the conversion operator is defined in winrt/Windows.System.h)

Is it right?

I tried the following code.
Where is wrong?

#include <iostream>
#include <cstdint>
#include <string>
#include <sstream>
#include <winrt/base.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.ApplicationModel.DataTransfer.h>

#include <random>
#include <sstream>

using namespace winrt;
using namespace winrt::Windows::System;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::ApplicationModel::DataTransfer;

#pragma comment(lib, "WindowsApp")

winrt::fire_and_forget run_async(DispatcherQueue queue)
{
  co_await winrt::resume_background();
  auto x = 10;
  auto y = 20;
  auto z = x * y;
  
  std::ostringstream oss;
  oss << "X:" << x << ",Y:" << y << "Z:" << z;
  /* I will run the something coroutine*/

  co_await winrt::resume_foreground(queue); ; // C2440, compile error, failed to solve override.

  DataPackage package{ nullptr };
  package.SetText(winrt::to_hstring(oss.str()));
  Clipboard::SetContent(package)
  Clipboard::Flush();
}

int main()
{
  winrt::init_apartment();
  
  auto controller{ DispatcherQueueController::CreateOnDedicatedThread() };
  
  run_async(controller.DispatcherQueue());

  return 0;
}

@stevewhims
Copy link
Member

stevewhims commented May 6, 2020

Hi, @elda27, I wasn't sure what kind of project you created but I notice you're explicitly linking WindowsApp.lib. See that link for details about why you shouldn't need to do that.

If you set things up as per this info: Visual Studio support for C++/WinRT, XAML, the VSIX extension, and the NuGet package, and if you create a project using one of the C++/WinRT project templates, then that's the recommended way.

There are very rare cases where the kind of project you create means you have to explicitly link WindowsApp.lib. But if you use a C++/WinRT project template, then you don't.

Anyway, in order to try and repro with something close to your setup, and based on that pragma, I thought I'd create a regular Windows Desktop Application project, instead of creating a C++/WinRT project. I pasted your code in, manually set C++17 as the standard, manually added the NuGet package (the NuGet package is described in the second link above), and added a semicolon after the call to Clipboard::SetContent. Once I'd done all that, the code compiled fine. So I wasn't able to repro your issue in my experiment.

Also, note that resume_foreground doesn't depend on a conversion operator. There are separate, different, implementations of resume_foreground in the two header files I mentioned.

My first recommendation would be to set up Visual Studio as described in that second link above. Then try creating a projected based on one of the C++/WinRT project templates that you get with the VSIX that's described at that link. And then see whether you can get your code to build in that project.

Thanks!
Steve

@elda27
Copy link
Author

elda27 commented May 6, 2020

I solve problems after I recreate new project and configure such as reference page.
The old project can't solve problems by same procedure so maybe have any wrong configuration.
Thank you very much.

@stevewhims
Copy link
Member

Thanks for the update! I'll go ahead and close the issue now.

-Steve

QuinnRadich pushed a commit that referenced this issue Apr 1, 2022
* Add 0.8.7 release notes

* address feedback

* formatting

* Update stable-channel.md

Co-authored-by: Steven White <[email protected]>
QuinnRadich pushed a commit that referenced this issue Jun 3, 2022
* Remove references to Reveal Highlight style

Removing reference to Reveal Highlight from the Reveal Focus style page.
- Ref in parens near top.
- Referred to in Related articles links.
The Reveal Highlight page was removed.

* Update date

* Revert "Update date"

This reverts commit da0ca4d.

* Update date on reveal-focus

* Fix month on date in header

* Update PT Run docs

* fixes

* layout improvement

* fixes, update calculator table

* improve image

* fix spelling in clac warning

* fix spaces

* Update composition-effects.md and fix compile

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* fix hyperbolic calc operations

* Update installer link from WinUI3 tutorial

* Improve the downloads page (#2350)

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* feedback

* remove VSIX and formatting

* Update downloads.md

* Update apply-rounded-corners.md

Wording changes and improved the P/Invoke signature to throw exceptions rather than return HRESULT.

* fixing typo (#2395)

* [BrokenLinksH2] Fixed the link path

Fixed the link filenames

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* [BrokenLinksH2] Fixed the link path

Fixed the link

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* resolve feedback

* update keyword list

* improvements

* fixes

* Info adjustments

* fix table

* Release appsdk 1.1 preview1 (#2332)

Publish docs to accompany Windows App SDK v1.1 preview 1

* Update release cadence table to 1.1.0-preview1 (#2408)

* Fix downloads link in 1.1 Preview 1 Rel Notes (#2409)

* Fix downloads link in 1.1 Preview 1 Rel Notes

* Once more

* Update preview-channel.md

Co-authored-by: Steven White <[email protected]>

* Add minimum .NET SDK version to 1.1-preview1 relnote (#2410)

* Update preview-channel.md

* Update preview-channel.md

* Update preview-channel.md

Co-authored-by: Steven White <[email protected]>

* new topic about config macros (#2411)

* polish (#2412)

* layout adjustments and requirements list update

* small fixes

* [BrokenLinksH2] Hid the broken link

Commented the broken link

* Add Arm64EC ABI doc (#2337)

* Author new Arm64EC ABI doc

* TOC (#2415)

* Fix link to arm abi (#2416)

* Fix broken download link (#2414)

* You can combine different WINRT_NATVIS settings (#2418)

* You can combine different WINRT_NATVIS settings

`WINRT_NATVIS` can also be set differently by different files. If any file enables `WINRT_NATVIS` (either explicitly or implicitly by defining `_DEBUG`), then the resulting module gets natvis support.

* Update macros.md

Co-authored-by: Steven White <[email protected]>

* Clarify when runtime is needed for push (#2419)

* Fix sub commands in "update" section

The "update all" section mistakenly left out that "update" is a sub-command of "source", as it does not work on its own as a top level winget command.

Additionally, these two update commands listed "C:\" at the beginning, which naturally only works if winget is located at the root of the C drive, which is not normally the case, nor do the other examples list this - thus, this has been removed.

* Additional 1.1-preview1 rel note comments (#2421)

* Additional 1.1-preview1 rel note comments

* Update preview-channel.md

Consistent "currently". Couple other minor tweaks.

* Resolving conflict

* Stwhi conflicts (#2422)

* TOC (#2415)

* Fix link to arm abi (#2416)

* Fix broken download link (#2414)

* You can combine different WINRT_NATVIS settings (#2418)

* You can combine different WINRT_NATVIS settings

`WINRT_NATVIS` can also be set differently by different files. If any file enables `WINRT_NATVIS` (either explicitly or implicitly by defining `_DEBUG`), then the resulting module gets natvis support.

* Update macros.md

Co-authored-by: Steven White <[email protected]>

* Clarify when runtime is needed for push (#2419)

Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>

* Addressing comments

* Consistency with the other change to "evaluating".

* Notifications and self-contained deployment clarifications

Co-authored-by: Steven White <[email protected]>
Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>

* Update what's new (#2424)

* Jken whatsnew mar22 (#2425)

* Update what's new

* Corrected broken links

* support nuance (#2426)

* Add 0.8.7 release notes (#2423)

* Add 0.8.7 release notes

* address feedback

* formatting

* Update stable-channel.md

Co-authored-by: Steven White <[email protected]>

* remove WinUI 3 margins

* latest available (#2428)

* fix broken link (#3764)

* naming convention (#2431)

* naming convention

* parens

* Updates to disadvantages. (#2432)

* Added instructions for hybrid CRT. (#2433)

* Added instructions for hybrid CRT.

* Update deploy-self-contained-apps.md

Co-authored-by: Steven White <[email protected]>

* Improved HybridCRT.props guidance. (#2434)

* Update Pivot recommendations

* fix link

* updating descriptions (#2437)

* typo

* Java path correction

As pointed out in issue #3721

* resolving conflicts

* Add note for winget upgrade (#2439)

* resolving build issues (#2440)

* resolving build issues

* fixing another issue

* fixing another issue 2

* fixing conflicts 2

Co-authored-by: Alvin Ashcraft <[email protected]>
Co-authored-by: htcfreek <[email protected]>
Co-authored-by: lindexi <[email protected]>
Co-authored-by: Sid Lama <[email protected]>
Co-authored-by: PRMerger16 <[email protected]>
Co-authored-by: PRMerger7 <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: KB <[email protected]>
Co-authored-by: Peter Torr (MSFT) <[email protected]>
Co-authored-by: PRMerger8 <[email protected]>
Co-authored-by: PRMerger19 <[email protected]>
Co-authored-by: Linda Spiller <[email protected]>
Co-authored-by: Shannon Leavitt <[email protected]>
Co-authored-by: MB <[email protected]>
Co-authored-by: Gabby Bilka <[email protected]>
Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>
Co-authored-by: Kade <[email protected]>
Co-authored-by: PRMerger3 <[email protected]>
Co-authored-by: John Kennedy <[email protected]>
Co-authored-by: Jim Walker (WINDOWS) <[email protected]>
Co-authored-by: PRMerger5 <[email protected]>
Co-authored-by: Mike Battista <[email protected]>
Co-authored-by: PRMerger9 <[email protected]>
QuinnRadich pushed a commit that referenced this issue Jun 3, 2022
* Remove references to Reveal Highlight style

Removing reference to Reveal Highlight from the Reveal Focus style page.
- Ref in parens near top.
- Referred to in Related articles links.
The Reveal Highlight page was removed.

* Update date

* Revert "Update date"

This reverts commit da0ca4d.

* Update date on reveal-focus

* Fix month on date in header

* Update PT Run docs

* fixes

* layout improvement

* fixes, update calculator table

* improve image

* fix spelling in clac warning

* fix spaces

* Update composition-effects.md and fix compile

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* fix hyperbolic calc operations

* Update installer link from WinUI3 tutorial

* Improve the downloads page (#2350)

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* Update downloads.md

* feedback

* remove VSIX and formatting

* Update downloads.md

* Update apply-rounded-corners.md

Wording changes and improved the P/Invoke signature to throw exceptions rather than return HRESULT.

* fixing typo (#2395)

* [BrokenLinksH2] Fixed the link path

Fixed the link filenames

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* [BrokenLinksH2] Fixed the link path

Fixed the link

* [BrokenLinksH2] Fixed the link path

Fixed the link path

* resolve feedback

* update keyword list

* improvements

* fixes

* Info adjustments

* fix table

* Release appsdk 1.1 preview1 (#2332)

Publish docs to accompany Windows App SDK v1.1 preview 1

* Update release cadence table to 1.1.0-preview1 (#2408)

* Fix downloads link in 1.1 Preview 1 Rel Notes (#2409)

* Fix downloads link in 1.1 Preview 1 Rel Notes

* Once more

* Update preview-channel.md

Co-authored-by: Steven White <[email protected]>

* Add minimum .NET SDK version to 1.1-preview1 relnote (#2410)

* Update preview-channel.md

* Update preview-channel.md

* Update preview-channel.md

Co-authored-by: Steven White <[email protected]>

* new topic about config macros (#2411)

* polish (#2412)

* layout adjustments and requirements list update

* small fixes

* [BrokenLinksH2] Hid the broken link

Commented the broken link

* Add Arm64EC ABI doc (#2337)

* Author new Arm64EC ABI doc

* TOC (#2415)

* Fix link to arm abi (#2416)

* Fix broken download link (#2414)

* You can combine different WINRT_NATVIS settings (#2418)

* You can combine different WINRT_NATVIS settings

`WINRT_NATVIS` can also be set differently by different files. If any file enables `WINRT_NATVIS` (either explicitly or implicitly by defining `_DEBUG`), then the resulting module gets natvis support.

* Update macros.md

Co-authored-by: Steven White <[email protected]>

* Clarify when runtime is needed for push (#2419)

* Fix sub commands in "update" section

The "update all" section mistakenly left out that "update" is a sub-command of "source", as it does not work on its own as a top level winget command.

Additionally, these two update commands listed "C:\" at the beginning, which naturally only works if winget is located at the root of the C drive, which is not normally the case, nor do the other examples list this - thus, this has been removed.

* Additional 1.1-preview1 rel note comments (#2421)

* Additional 1.1-preview1 rel note comments

* Update preview-channel.md

Consistent "currently". Couple other minor tweaks.

* Resolving conflict

* Stwhi conflicts (#2422)

* TOC (#2415)

* Fix link to arm abi (#2416)

* Fix broken download link (#2414)

* You can combine different WINRT_NATVIS settings (#2418)

* You can combine different WINRT_NATVIS settings

`WINRT_NATVIS` can also be set differently by different files. If any file enables `WINRT_NATVIS` (either explicitly or implicitly by defining `_DEBUG`), then the resulting module gets natvis support.

* Update macros.md

Co-authored-by: Steven White <[email protected]>

* Clarify when runtime is needed for push (#2419)

Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>

* Addressing comments

* Consistency with the other change to "evaluating".

* Notifications and self-contained deployment clarifications

Co-authored-by: Steven White <[email protected]>
Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>

* Update what's new (#2424)

* Jken whatsnew mar22 (#2425)

* Update what's new

* Corrected broken links

* support nuance (#2426)

* Add 0.8.7 release notes (#2423)

* Add 0.8.7 release notes

* address feedback

* formatting

* Update stable-channel.md

Co-authored-by: Steven White <[email protected]>

* remove WinUI 3 margins

* latest available (#2428)

* fix broken link (#3764)

* naming convention (#2431)

* naming convention

* parens

* Updates to disadvantages. (#2432)

* Added instructions for hybrid CRT. (#2433)

* Added instructions for hybrid CRT.

* Update deploy-self-contained-apps.md

Co-authored-by: Steven White <[email protected]>

* update page

* smal fixes

* Improved HybridCRT.props guidance. (#2434)

* Update Pivot recommendations

* fix link

* updating descriptions (#2437)

* typo

* Java path correction

As pointed out in issue #3721

* Add note for winget upgrade (#2439)

* resolving build issues (#2440)

* resolving build issues

* fixing another issue

* fixing another issue 2

* updating as per feedbafck (#2442)

* fix LayoutCycleException link

* Stwhi main/bootstrap cs 1 (#2444)

* C# APIs

* ready for review

* TOC

* formatting

* more corrections

* last corrections

* Update index.md

* Update docs for Windows App SDK 1.0.2  (#2441)

* Update 1.0.2 downloads

* Update downloads.md

* Add servicing-1.0.2 release notes and channel updates

* Update release-channels.md

* Update with CRT note

* Fix misdirected link

* Update hub/apps/windows-app-sdk/stable-channel.md

Co-authored-by: Gabby Bilka <[email protected]>
Co-authored-by: Steven White <[email protected]>
Co-authored-by: Gabby Bilka <[email protected]>
Co-authored-by: MatchaMatch <[email protected]>

* Added note about UseCrtSDKReferenceStaticWarning. (#2446)

* Link to sample parent directory rather than to the unpackaged sample. (#2447)

* Fix old link

* Adding a simple orientation page for toast notifications. Added desktop page back into TOC

* Forgot to add the overview page to the TOC

* Forgot to add the overview page to the TOC

* feedback and new shortcut

* small adjustment

* fix table

* fix tables

* spell fix

* Update Windows Runtime Component/Background Task migration (#2451)

* add links from UWP to Desktop WRCs

* update backgroundtask info

* add row for windows runtime components

* edits

* fix link

* fix support

* Edits

* edits

* Update feature-mapping-table.md

* Update creating-windows-runtime-components-in-csharp-and-visual-basic.md

* Update index.md

Co-authored-by: Steven White <[email protected]>

* Updates per PR review

* Fix spelling error

* Adding H1

* gitissue#3769-delete-image (#2461)

* fixing build issues

* fixing TBD (#2464)

Co-authored-by: Alvin Ashcraft <[email protected]>
Co-authored-by: htcfreek <[email protected]>
Co-authored-by: lindexi <[email protected]>
Co-authored-by: Sid Lama <[email protected]>
Co-authored-by: PRMerger16 <[email protected]>
Co-authored-by: PRMerger7 <[email protected]>
Co-authored-by: angelazhangmsft <[email protected]>
Co-authored-by: KB <[email protected]>
Co-authored-by: Peter Torr (MSFT) <[email protected]>
Co-authored-by: PRMerger8 <[email protected]>
Co-authored-by: PRMerger19 <[email protected]>
Co-authored-by: Linda Spiller <[email protected]>
Co-authored-by: Shannon Leavitt <[email protected]>
Co-authored-by: MB <[email protected]>
Co-authored-by: Gabby Bilka <[email protected]>
Co-authored-by: Matt Wojciakowski <[email protected]>
Co-authored-by: Raymond Chen <[email protected]>
Co-authored-by: Kade <[email protected]>
Co-authored-by: PRMerger3 <[email protected]>
Co-authored-by: John Kennedy <[email protected]>
Co-authored-by: Jim Walker (WINDOWS) <[email protected]>
Co-authored-by: PRMerger5 <[email protected]>
Co-authored-by: Mike Battista <[email protected]>
Co-authored-by: PRMerger9 <[email protected]>
Co-authored-by: PRMerger Service account 2 <[email protected]>
Co-authored-by: Ming Ho <[email protected]>
Co-authored-by: Gabby Bilka <[email protected]>
Co-authored-by: Vaheeshta Mehrshahi <[email protected]>
Co-authored-by: Drew Batchelor (he/him) <[email protected]>
Co-authored-by: Jeremy Danyow <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants