首页 > 其他分享 >WPF The calling thread cannot access this object because a different thread owns it.

WPF The calling thread cannot access this object because a different thread owns it.

时间:2024-07-18 20:30:20浏览次数:15  
标签:different because scaler thread ImgIdx System private using imgIdx

 

 

 public int ImgIdx
 {
     get
     {
         return imgIdx;
     }
     set
     {
         if(value!=imgIdx)
         {
             imgIdx = value;
             if(imgIdx<0)
             {
                 imgIdx = imgsCount - 1;
             }
             if(imgIdx>=imgsCount)
             {
                 imgIdx = 0;
             }
             OnPropertyChanged(nameof(ImgIdx));
             if(!isPaused)
             {
                 ImgUrl = imgsList[ImgIdx];
                 Title = ImgUrl;
             } 
         }
     }
 }

System.InvalidOperationException
HResult=0x80131509
Message=The calling thread cannot access this object because a different thread owns it.
Source=WindowsBase
StackTrace:
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.Window.set_Title(String value)
at WpfApp219.MainWindow.set_ImgIdx(Int32 value) in D:\C\WpfApp219\MainWindow.xaml.cs:line 77
at WpfApp219.MainWindow.Tmr_Elapsed(Object sender, ElapsedEventArgs e) in D:\C\WpfApp219\MainWindow.xaml.cs:line 104
at System.Timers.Timer.MyTimerCallback(Object state)

 

 

 

public int ImgIdx
{
    get
    {
        return imgIdx;
    }
    set
    {
        if(value!=imgIdx)
        {
            imgIdx = value;
            if(imgIdx<0)
            {
                imgIdx = imgsCount - 1;
            }
            if(imgIdx>=imgsCount)
            {
                imgIdx = 0;
            }
            OnPropertyChanged(nameof(ImgIdx));
            if(!isPaused)
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    ImgUrl = imgsList[ImgIdx];
                    Title = ImgUrl;
                })); 
            } 
        }
    }
}

 

 

 Application.Current.Dispatcher.BeginInvoke(new Action(() =>
 {
     ImgUrl = imgsList[ImgIdx];
     Title = ImgUrl;
 })); 

 

 

//xaml
<Window x:Class="WpfApp219.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp219"
        mc:Ignorable="d" KeyDown="Window_KeyDown" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image x:Name="img" Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
               MouseWheel="img_MouseWheel" MouseDown="img_MouseDown" MouseUp="img_MouseUp"
               MouseMove="img_MouseMove">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaler"/>
                    <TranslateTransform x:Name="translater"/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
    </Grid>
</Window>




//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApp219
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private string imgUrl;
        public string ImgUrl
        {
            get
            {
                return imgUrl;
            }
            set
            {
                if (value != imgUrl)
                {
                    imgUrl = value;
                    OnPropertyChanged(nameof(ImgUrl));
                    //this.Title = imgUrl;
                }
            }
        }

        private int imgIdx = 0;
        public int ImgIdx
        {
            get
            {
                return imgIdx;
            }
            set
            {
                if(value!=imgIdx)
                {
                    imgIdx = value;
                    if (imgIdx < 0)
                    {
                        imgIdx = imgsCount - 1;
                    }
                    if (imgIdx >= imgsCount)
                    {
                        imgIdx = 0;
                    }
                    OnPropertyChanged(nameof(ImgIdx));
                   
                    if (!isPaused)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            ImgUrl = imgsList[ImgIdx];
                            Title = ImgUrl;
                            ResetTransform();
                        }));
                    }
                }
            }
        } 
        
        private List<string> imgsList { get; set; }
        
        private int imgsCount = 0;
        private bool isPaused = false;
        private Point currentPt { get; set; }
        private bool isMoving = false;

        public MainWindow()
        {
            InitializeComponent();
            imgsList = new List<string>(Directory.GetFiles(@"..\..\Images"));
            imgsCount = imgsList.Count;
            ImgUrl = imgsList[imgIdx];            
            this.DataContext = this;
            System.Timers.Timer tmr = new System.Timers.Timer();
            tmr.Interval = 300;
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Start();
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ++ImgIdx;
        }

        private void ResetTransform()
        {
            scaler.ScaleX = 1.0;
            scaler.ScaleY = 1.0;
            scaler.CenterX = 0;
            scaler.CenterY = 0;
            translater.X = 0;
            translater.Y = 0;
        }

        private void img_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                scaler.ScaleX *= 1.2;
                scaler.ScaleY *= 1.2;
            }
            else
            {
                scaler.ScaleX /= 1.2;
                scaler.ScaleY /= 1.2;
            }
            scaler.CenterX = e.GetPosition(img).X;
            scaler.CenterY = e.GetPosition(img).Y;
        }

        private void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            currentPt = e.GetPosition(img);
        }

        private void img_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(e.ChangedButton==MouseButton.Left && e.ButtonState==MouseButtonState.Released && isMoving)
            {
                Point newPt = e.GetPosition(img);
                translater.X += (newPt.X - currentPt.X) * scaler.ScaleX;
                translater.Y += (newPt.Y - currentPt.Y) * scaler.ScaleY;
            }
        }

        private void img_MouseMove(object sender, MouseEventArgs e)
        {
            isMoving = true;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Down)
            {
                ++ImgIdx;
            }
            else if ((e.Key == Key.Up))
            {
                --ImgIdx; 
            }
            else if(e.Key==Key.Space)
            {
                isPaused=!isPaused;
            }
        }
    }
}

 

标签:different,because,scaler,thread,ImgIdx,System,private,using,imgIdx
From: https://www.cnblogs.com/Fred1987/p/18310383

相关文章

  • Java开发手册中-避免Random实例被多线程使用、多线程下Random与ThreadLoacalRandom性
    场景Java中使用JMH(JavaMicrobenchmarkHarness微基准测试框架)进行性能测试和优化:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/131723751参考以上性能测试工具的使用。Java开发手册中有这样一条:【推荐】避免Random实例被多线程使用,虽然共享该实例是线程......
  • 从ThreadLocal底层源码一直聊到内存泄漏,干货满满!!
    小强最近在疯狂补习高并发的相关知识,但是在学到threadLocal时有点力不从心了,尤其是threadLocal的底层架构和如何导致内存泄漏,今天我们帮小强一把!!把这一块彻底聊清楚!!!文章目录1.threadLocal的前世今生1.为什么要使用threadLocal2.threadLocal和Synchonized的比较3.使用场......
  • pthread_cond_wait
    介绍pthread_cond_wait是一个阻塞函数,它会将当前线程放入条件变量的等待队列中,并自动释放与之关联的互斥锁。当条件变量被其他线程用pthread_cond_signal或pthread_cond_broadcast唤醒后,pthread_cond_wait会自动重新获取互斥锁,然后再返回。这个机制可以避免死锁,并确保......
  • Qt UI线程中使用QThread::sleep有什么影响
    在Qt中,QThread::sleep 是一个静态函数,用于让当前线程休眠指定的时间,以实现线程暂停的目的。当在UI线程中调用 QThread::sleep 函数时,会导致UI线程在指定的时间内被阻塞,即界面无法响应用户的操作,直到休眠时间结束。因此,在UI线程中使用 QThread::sleep 可能会导致界面冻结,影响......
  • C++11 标准库 线程库<thread>梳理
    目录<thread>this_thread命名空间1.get_id()2.sleep_for()3.sleep_until()4.yield()thread类构造函数:类方法1.get_id()2.join()3.detach()4.joinable()5.operator=6.hardware_concurrency(static)多线程的两种计算场景<thread>this_thread命名空间在C++11中不仅添加......
  • STAThread与Windows UI线程模型
    STAThread与WindowsUI线程模型1.STAThread属性标注Main方法,使主线程运行在STA模式。用于兼容需要STA环境的COM组件,特别是UI组件。2.线程模式STA(SingleThreadedApartment)一个线程处理所有STA组件。UI线程通常为STA,负责消息处理和UI更新。MTA(MultiThreaded......
  • Java笔记之ThreadLocal
    定义:ThreadLocal叫做线程变量,该变量对其他线程而言是隔离的,是当前线程独有的变量。ThreadLocal为变量在每一个线程中都创建了一个副本,并且该副本只能当前Thread使用,因此不存在多线程共享的问题。原理:Thread类有一个类型ThreadLocalMap的实例变量ThreadLocals,每个线程都有一个......
  • Cuda编程:__syncthreads运行时API在访问共享内存时的使用
    该运行时API的作用作为在访问共享内存时作为线程块内的同步机制出现,保证同一线程块内所有线程到程序运行到这个运行时API调用时都能运行完毕(注意,该API不能同步不同线程块内的线程),例如下列Cuda静态共享内存使用代码示例程序中的第23行所示:/*************************************......
  • mormot.rest.core--TRestRunThreads
    mormot.rest.core--TRestRunThreads{************TRestRunThreads-REST实例的多线程处理}///访问TRest实例的多线程处理TRestRunThreads=class(TSynPersistentLock)protectedfOwner:TRest;//TRest实例的所有者fBackgroundTimer:TRest......
  • ThreadPoolExector
    JavaThreadPool使用线程池的好处:减少资源的浪费:创建、销毁、切换线程需要消耗系统资源,通过使用线程池可以降低消耗。增加可管理度:通过线程池的同一管理,能够实现线程的更好的管理。提高相应速度:当任务到来时,无需在创建线程,直接就能对任务进行反馈Java线程池的使用线程池......