C # WPF, как запустить асинхронную задачу с потоком по нажатию кнопки - PullRequest
0 голосов
/ 17 ноября 2018

У меня возникла проблема с запуском асинхронной задачи с потоком, который вызывается при нажатии кнопки, и я не знаю, как ее решить.

При запуске он остановится сразу после нажатия кнопки Загрузить.перепробовал несколько вещей, но ничего не получалось.Один раз это работало, но только до "VideosInsertRequest_ProgressChanged", но я не помню, что я сделал xD Всегда получаю сообщение об ошибке, что не могу получить доступ к объекту ...

Код:

        private void UploadVideo_Click(object sender, EventArgs e)
    {
        StatusLabel.Content = "Upload Video...";
        Thread thead = new Thread(() =>
        {
            VideoUpload().Wait();
        });
        thead.IsBackground = true;
        thead.Start();
    }

    private async Task VideoUpload()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = VideoTitle.Text;
        video.Snippet.Description = VideoDesc.Text;
        string[] tags = Regex.Split(VideoTags.Text, ",");
        video.Snippet.Tags = tags;
        video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = VideoPrivacy.Text; // or "private" or "public"
        var filePath = VideoPath.Text; // Replace with path to actual movie file.

        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged += VideosInsertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived += VideosInsertRequest_ResponseReceived;

            await videosInsertRequest.UploadAsync();
        }
    }

    void VideosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                StatusLabel.Content = String.Format("{0} bytes sent.", progress.BytesSent);
                break;

            case UploadStatus.Failed:
                StatusLabel.Content = String.Format("An error prevented the upload from completing.{0}", progress.Exception);
                break;
        }
    }

    void VideosInsertRequest_ResponseReceived(Video video)
    {
        StatusLabel.Content = string.Format("Video id '{0}' was successfully uploaded.", video.Id);
    }

Ответы [ 2 ]

0 голосов
/ 17 ноября 2018

Похоже, вы пытаетесь получить доступ к элементу пользовательского интерфейса в другом потоке.Вы можете использовать диспетчер для выполнения этого кода в потоке пользовательского интерфейса.Попробуйте это:

void VideosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
    switch (progress.Status)
    {
        case UploadStatus.Uploading:
            UpdateStatus(String.Format("{0} bytes sent.", progress.BytesSent));
            break;

        case UploadStatus.Failed:
            UpdateStatus(String.Format("An error prevented the upload from completing.{0}", progress.Exception));
            break;
    }
}

void VideosInsertRequest_ResponseReceived(Video video)
{
    UpdateStatus(string.Format("Video id '{0}' was successfully uploaded.", video.Id));
}

private void UpdateStatus(string status)
{
    StatusLabel.Dispatcher.BeginInvoke(new Action(() => { StatusLabel.Content = status; }));
}
0 голосов
/ 17 ноября 2018

вам нужно использовать ICommand с RelayCommandAsync и связать команду Button с этой icommand. лучше использовать MVVM в wpf для создания класса viewmodel

public class MoneyPageViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        private ICommand _readAdminVersions;

        public ICommand readAdminVersions
        {
            get
            {
                return _readAdminVersions ??
                       (_readAdminVersions = new 
                        RelayCommandAsync(executeReadAdminVersions, (c) => true));
            }
        }
        private async Task executeReadAdminVersions()
        {//your code goes here
        }
    }  

если вы застряли в части INotifyPropertyChanged, просто видите простую реализацию в сети, эта автоматически генерируется реактивным мозгом ReSharper или, может быть, против 2017 года не знаю:)
а вот класс relayCommandAsync:

public class RelayCommandAsync : ICommand
    {
        private readonly Func<Task> _execute;
        private readonly Predicate<object> _canExecute;
        private bool isExecuting;

        public RelayCommandAsync(Func<Task> execute) : this(execute, null) { }

        public RelayCommandAsync(Func<Task> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (!isExecuting && _canExecute == null) return true;
            return (!isExecuting && _canExecute(parameter));
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public async void Execute(object parameter)
        {
            isExecuting = true;
            try { await _execute(); }
            finally { isExecuting = false; }
        }
    }  

добавить viewmodel в xaml:

        <Grid.Resources>
            <model:MoneyPageViewModel x:Key="MoneyPageViewModel"></model:MoneyPageViewModel>
        </Grid.Resources>

затем привязать кнопку к этой команде:

<Button Command="{Binding readAdminVersions , Source={StaticResource MoneyPageViewModel}}>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...