相关疑难解决方法(0)

使用不同的线程更新GUI(WPF)

这里有一个问题,我不知道如何修复.我正在做一个涉及GUI和串行数据的小项目.GUI由主线程运行,并且由于保存我的传入串行数据的数据变量需要不断更新,因此这些变量将在第二个线程中更新.问题是当我需要更新GUI上的一些文本框时,需要使用辅助线程中的数据更新这些文本框,这就是我的问题所在.我不能直接从辅助线程更新它们,我不知道如何从我的辅助线程传输数据,并制定一个从主线程更新它们的系统.我把我的代码放在下面:

任何帮助都会很棒.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO.Ports;
using System.Threading;

namespace GUIBike
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string inputdata;
        public static int MaximumSpeed, maximumRiderInput, RiderInput, Time, CurrentSpeed, DistanceTravelled, MaximumMotorOutput, MotorOutput, InputSpeed;
        public static string SaveDataString;
        public Thread Serial;
        public static SerialPort SerialData; …
Run Code Online (Sandbox Code Playgroud)

c# user-interface multithreading sharing

38
推荐指数
5
解决办法
13万
查看次数

如何在异步方法中向UI提供反馈?

我一直在开发一个Windows表单项目,我有10个任务要做,我想在async某种程度上这样做.当用户单击按钮并且我调用async方法执行此操作时,这些任务将会出现.在我的代码中,我已经有了这些进程的参数列表.

我的问题是:

A)如何转换我的代码并行运行所有进程?(我想实现async/await)

B)如何向我的UI应用程序提供反馈?

下面的代码是我试过的:

我的按钮调用方法来启动进程

private void button1_Click(object sender, EventArgs e)
{
    // almost 15 process
    foreach (var process in Processes)
    {
        // call a async method to process
        ProcessObject(process);
    }
}
Run Code Online (Sandbox Code Playgroud)

模拟我的进程获取参数的方法

private async void ProcessObject(ProcessViewModel process)
{
    // this is my loop scope, which I need to run in parallel
    {
       // my code is here

       // increment the progress of this process
       process.Progress++;

       // feedback to UI (accessing the UI controls)
       UpdateRow(process); …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous task winforms

8
推荐指数
2
解决办法
2019
查看次数