Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Windows 7 Phone中的线程问题_Multithreading_Silverlight_Windows Phone 7_Thread Safety - Fatal编程技术网

Multithreading Windows 7 Phone中的线程问题

Multithreading Windows 7 Phone中的线程问题,multithreading,silverlight,windows-phone-7,thread-safety,Multithreading,Silverlight,Windows Phone 7,Thread Safety,嗨 我使用silverlight在基于Windows7手机的应用程序中工作。在我的一个UI类GameScreen.xaml中有以下方法。我正在调用构造函数游戏屏幕中的startTimer方法。问题是,当调用updateTime方法时 timeLabel.Text = "Time left: 00 : " + time; 行时,程序将在时间变量上抛出UnauthorizedAccessException private void startTimer() { timeT

嗨 我使用silverlight在基于Windows7手机的应用程序中工作。在我的一个UI类GameScreen.xaml中有以下方法。我正在调用构造函数游戏屏幕中的startTimer方法。问题是,当调用updateTime方法时

timeLabel.Text = "Time left:  00 : " + time;
行时,程序将在时间变量上抛出UnauthorizedAccessException

private void startTimer()
    {
        timeThread = new Thread(new ThreadStart(startThread));
        timeThread.Start();
    }

    public void startThread()
    {
        while (timeLeft > 0)
        {
            Thread.Sleep(1000);
            updateTime();                
            if (timePassed % 10 == 0)
            {
                findNextBGResource();
                changeBackgroundScene();
            }
        }            
    }


    private void updateTime()
    {
        // update the view
        String time = timeLeft.ToString();
        if (timeLeft < 10)
        {
            time = "0" + time;
        }
        if (doUpdateTime && timeLeft >= 0)
        {
            timeLabel.Text = "Time left:  00 : " + time;
        }
    }
private void startTimer()
{
timeThread=newthread(newthreadstart(startThread));
timeThread.Start();
}
public void startThread()
{
而(时间间隔>0)
{
睡眠(1000);
updateTime();
如果(时间传递%10==0)
{
findNextBGResource();
changeBackgroundScene();
}
}            
}
私有void updateTime()
{
//更新视图
字符串时间=timeLeft.ToString();
如果(时间间隔<10)
{
time=“0”+时间;
}
如果(doUpdateTime&&timeLeft>=0)
{
timeLabel.Text=“剩余时间:00:+时间;
}
}
在这方面有人能帮我吗


致以最诚挚的问候…

基本上,除了调度程序线程之外,您不能修改UI。两种选择:

  • 用于在调度程序线程中执行ui修改代码
  • 使用,而不是启动一个新线程和睡眠-这样“勾号”将在UI线程中已经出现

基本上,除了dispatcher线程之外,您不能修改UI。两种选择:

  • 用于在调度程序线程中执行ui修改代码
  • 使用,而不是启动一个新线程和睡眠-这样“勾号”将在UI线程中已经出现

您确定它在
时间
变量上,而不是
时间标签上吗?

除处理UI的线程外,您通常无法从其他线程编辑UI。

您确定它位于
时间
变量上,而不是
时间标签
上吗? 除了处理UI的线程外,通常不能从其他线程编辑UI