使线程在WinRT中运行 [英] Make Thread running in WinRT

查看:74
本文介绍了使线程在WinRT中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在学习WinRT和METRO Dev。我想创建3个线程,分开工作。

I'm Learning WinRT and METRO Dev. I want to create 3 thread, working separatly.

我希望每个线程更新一个文本块文本。我有Silverlight代码,但我不知道如何在WinRT中翻译它

I want that each thread update a textblock text. I've got the Silverlight Code but i don't know how to translate it in WinRT

这是我的代码:

XAML:

 < Button Click =" Button_Click" Content =" Toggle Threads">< / Button>

               &NBSP; < TextBlock x:Name =" Data1" Canvas.Top =" 50">主题1< / TextBlock>

              &NBSP;&NBSP; < TextBlock x:Name =" Data2" Canvas.Top =" 100">主题2< / TextBlock>

              &NBSP;&NBSP; < TextBlock x:Name =" Data3" Canvas.Top =" 150">主题3< / TextBlock>

 <Button Click="Button_Click" Content="Toggle Threads"></Button>
                <TextBlock x:Name="Data1" Canvas.Top="50">Thread 1</TextBlock>
                <TextBlock x:Name="Data2" Canvas.Top="100">Thread 2</TextBlock>
                <TextBlock x:Name="Data3" Canvas.Top="150">Thread 3</TextBlock>

C#:

私有线程_thread1;

private Thread _thread2;

private Thread _thread3;

private Thread _thread1;
private Thread _thread2;
private Thread _thread3;

private static System.Windows.Controls.TextBlock _data1;

private static System.Windows.Controls.TextBlock _data2;
$
private static System.Windows.Controls.TextBlock _data3;

private static System.Windows.Controls.TextBlock _data1;
private static System.Windows.Controls.TextBlock _data2;
private static System.Windows.Controls.TextBlock _data3;

private static int _count1 = 0;

private static int _count2 = 0;

private static int _count3 = 0;

private static int _count1 = 0;
private static int _count2 = 0;
private static int _count3 = 0;

private static bool _done = true;

private static bool _done = true;

public bool完成

{

   得到{return _done; }
   设置{_done = value; }
}

public bool Done
{
    get { return _done; }
    set { _done = value; }
}

public MainPage()

{

     InitializeComponent();

 

    _data1 = Data1;

    _data2 = Data2;

     _data3 = Data3;

}

public MainPage()
{
     InitializeComponent();
 
    _data1 = Data1;
    _data2 = Data2;
    _data3 = Data3;
}

private void StartThreads()

{

  &NBSP;   _done = false;

private void StartThreads()
{
    _done = false;

     _thread1 =新主题(DoThread1);

    _thread1.Start();

    _thread1 = new Thread(DoThread1);
    _thread1.Start();

    _thread2 =新主题(DoThread2);

     _thread2.Start();

    _thread2 = new Thread(DoThread2);
    _thread2.Start();

    _thread3 =新主题(DoThread3);

    _thread3.Start();&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

}

    _thread3 = new Thread(DoThread3);
    _thread3.Start();     
}

public static void DoThread1()

{

    while(!_done)

    {

         _count1 ++;

        _data1.Dispatcher.BeginInvoke(delegate()

    {

    _data1.Text = _count1.ToString();

  

    });
$
   System.Threading.Thread.Sleep(100 );;
 }

}

public static void DoThread1()
{
    while (!_done)
    {
        _count1++;
        _data1.Dispatcher.BeginInvoke(delegate()
    {
    _data1.Text = _count1.ToString();
  
     });
  System.Threading.Thread.Sleep(100);
 }
}

public static void DoThread2()

{

  while(!_done)

  {

   _count2 ++;

   _data2.Dispatcher.BeginInvoke(delegate()

   {

    _data2.Text = _count2.ToString();

    

  });
$
   System.Threading.Thread.Sleep(200);

 }

}

public static void DoThread2()
{
 while (!_done)
 {
  _count2++;
  _data2.Dispatcher.BeginInvoke(delegate()
  {
   _data2.Text = _count2.ToString();
    
  });
  System.Threading.Thread.Sleep(200);
 }
}

public static void DoThread3()

{

  while(!_done)

  {

   _count3 ++;

   _data3。 Dispatcher.BeginInvoke(delegate()

   {

    _data3.Text = _count3.ToSt ring();

   

  });
$
   System.Threading.Thread .Sleep(300);

 }

}

public static void DoThread3()
{
 while (!_done)
 {
  _count3++;
  _data3.Dispatcher.BeginInvoke(delegate()
  {
   _data3.Text = _count3.ToString();
   
  });
  System.Threading.Thread.Sleep(300);
 }
}

private void Button_Click(object sender,RoutedEventArgs e)

{

  if(false == _done)

   _done = true;

  else

   StartThreads(); 

}

private void Button_Click(object sender, RoutedEventArgs e)
{
 if (false == _done)
  _done = true;
 else
  StartThreads(); 
}

我已经尝试了Async和Await的一些事情  ;但我没有发现任何有趣的东西。


 

I have trying a few things with Async and Await but i've found nothing interesting.
 

Adrien Bruyat

Adrien Bruyat

推荐答案

嗨阿德里安 - 你没有用于管理类似线程的API。我怀疑如果你想模仿这个,你最好的选择是使用任务并行库任务。 
Hi Adrian - you have none of the APIs to manage threads like that. I suspect that if you want to emulate this, your best option is to use Task Parallel Library tasks. 


这篇关于使线程在WinRT中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆