private void DoWork(object state)
        {
            var count = Interlocked.Increment(ref _executionCount);

            _logger.LogInformation(
                "Timed Hosted Service is working. Count: {Count}", count);

            try
            {
                var unsplashApi  = new UnsplashApiClient(_config);
                var wallpaperCom = (IDesktopWallpaper)(new DesktopWallpaperClass());
                for (uint i = 0; i < wallpaperCom.GetMonitorDevicePathCount(); i++)
                {
                    var monitorId = wallpaperCom.GetMonitorDevicePathAt(i);

                    if (string.IsNullOrEmpty(monitorId))
                    {
                        continue;
                    }

                    var photo      = unsplashApi.GetRandomDesktopBackground().GetAwaiter().GetResult();
                    var outputPath = _config["outputPath"];
                    var imagePath  = string.IsNullOrEmpty(outputPath)
                        ? Path.Combine(Path.GetTempPath(), "wallpaper.jpg")
                        : Path.Combine(outputPath, $"{DateTime.Now:yyyyMMddHHmm}_{photo.Id}.jpg");
                    WallpaperHelper.SetWallpaper(wallpaperCom, monitorId, imagePath, new Uri(photo.Urls.Full));
                    unsplashApi.CallDownloadTrackingEndpoint(photo);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }
        }
예제 #2
0
        public async static Task Main(string[] args)
        {
            var accessKey   = LoadAccessKey();
            var unsplashApi = new UnsplashApiClient(accessKey);
            var photo       = await unsplashApi.GetRandomDesktopBackground();

            WallpaperHelper.SetWallpaper(new Uri(photo.Urls.Full));
            unsplashApi.CallDownloadTrackingEndpoint(photo);
        }