2021년 5월 13일 목요일

c# wpf 다른쓰레드에서 메인UI 쓰레드 접근할경우

윈폼은 InvokeRequird 가 있어서 그거 쓰면되고 

wpf는 Dispather가 그일을 대신한다

유틸함수로 다음을 만들어놓고 쓰면됨


 using System;
 using System.Windows;
 using System.Windows.Threading;

 public static class DispatcherService
 {
        public static void Invoke(Action action)
        {
            Dispatcher dispatchObject = Application.Current != null ?
            Application.Current.Dispatcher : null;
            if (dispatchObject == null || dispatchObject.CheckAccess())
                action();
            else
                dispatchObject.Invoke(action);
        }

        public static T Invoke<T>(Func<T> func)
        {
            Dispatcher dispatchObject = Application.Current != null ?
            Application.Current.Dispatcher : null;
            if (dispatchObject == null || dispatchObject.CheckAccess())
                return func();
            else
                return dispatchObject.Invoke(func);
        }
 }


출처는.. 음.. 네이버에서.. -_-;;;;;

댓글 없음:

댓글 쓰기