locked
Using System.Threading from one Metro Style page using C++

    Question

  • Hi All,

     

    I had just created one C# simple code that uses System.Threading on one MetroStyle application, and I would like to port it to C++, but it seems that it is not a so simple task. Could anybody give me some adivices?

    On the C# I just created on simple application and at the MainPage I created one button named "TheadStartButton". And this is my MainPage C# code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Threading.Tasks;

    using Windows.Foundation;

    using Windows.UI.Xaml;

    using Windows.UI.Xaml.Controls;

    using Windows.UI.Xaml.Data;

     

    namespace MetroStyle.SlimTests

    {

        partial class MainPage

        {

            public MainPage()

            {

                InitializeComponent();

            }

     

            private void TheadStartButton_Click(object sender, RoutedEventArgs e)

            {

                System.Threading.Thread MyThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadMethod));

                System.Threading.Thread.Sleep(2000);

                MyThread.Start();

            }

     

            void ThreadMethod()

            {

                Windows.UI.Popups.MessageDialog BoxOk = new Windows.UI.Popups.MessageDialog("Thread rodando...");

                BoxOk.ShowAsync().Start();

                //MyTextBlock.Text = "Limpo...";

            }

        }

     

        public class ThreadInfo

        {

            public string Message { get; set; }

     

            public ThreadInfo()

            {

     

            }

        }

     

    }

     When I try to do the same on the C++, I got a lot of compilation errors. This is the first one:

    Error 1 error C3766: 'System::Threading::Thread' must provide an implementation for the interface method 'void System::Runtime::InteropServices::_Thread::GetTypeInfoCount(unsigned int %)' c:\...\MyPage.xaml.cpp 166 1 MyPage

     

    This is the piece of code migrated to C++:

    void MyApp::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

    {

           System::Threading::Thread MyThread = new System::Threading::Thread(new System::Threading::ThreadStart(ThreadMethod));

           System::Threading::Thread::Sleep(2000);

           MyThread->Start();

    }

     

    void ThreadMethod()

    {

           Windows::UI::Popups::MessageDialog BoxOk = new Windows::UI::Popups::MessageDialog("Teste...");

           BoxOk->ShowAsync()->Start();

           ProductDoneTextBlock->Text = "Limpo...";

    }

    Monday, January 2, 2012 2:17 AM

Answers

All replies

  • The System.Threading namespace is a .Net framework API. Metro style apps using C++ are native code (do not use the .Net runtime), so you would need to use the Windows Runtime Threading API (Windows::System::Threading::ThreadPool). Raman discussed the threading options for C++ in an earlier thread:

    http://social.msdn.microsoft.com/Forums/en-MY/winappswithnativecode/thread/e8949b86-19dd-4cf3-8e04-c19c739a7a3a

     

    Thanks,

    -David


    Tuesday, January 3, 2012 8:58 PM
    Moderator
  • Thanks David, I was just confirming if it would be possible to use this API on C++ WinRT code as it was on C#, because we are encoutering some dificults to do the same using  Windows::System::Threading::ThreadPool.

    Thanks a lot,

    Leo.

    Wednesday, January 4, 2012 1:43 PM
  • Not a problem Leo. We're all learning together here. I appreciate the feedback as it let's us know where we need to improve. Windows::System::Threading::ThreadPool usage documentation is one of those areas at the moment. 

    -David

    Thursday, January 5, 2012 8:43 PM
    Moderator