c# - 如何从页面上的线程关闭窗口?

标签 c# wpf multithreading xaml mvvm

我的应用程序有一个窗口,我将带有页面的框架放入网格中。与在 Page1.cs 中相比,我以“while”开始线程。执行后必须关闭页面窗口。

我不能使用这个方法 Application.Current.Shutdown()

因为这会关闭所有窗口,所以如果我在我的应用程序中有几个窗口,在此方法之后所有窗口都将关闭。

我考虑过使用 MVVM,但我不知道该怎么做。

我想从页面上的线程关闭一个窗口。(例如在方法 update() 中)

主窗口.xaml

<Windowx:Class="WpfApplication1.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:WpfApplication1"
  mc:Ignorable="d"
  Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Frame Source "Page1.xaml">

    </Frame>
  </Grid>
</Window>

代码页 1.xaml

<Page x:Class="WpfApplication1.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WpfApplication1"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Page1">
  <Grid>
  </Grid>
</Page>

然后我在 Page1.cs 中启动线程

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

namespace WpfApplication1
{
    /// 
    /// Interaction logic for Page1.xaml
    /// 
    public partial class Page1 : Page
    {
        bool isTrue = false;
        int i;
        private MainWindow vm = new MainWindow();
        public Page1()
        {
            InitializeComponent();
            Thread thread = null;

            thread = new Thread(() => update());
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
        }

        void update()
        {

            while (i<50000)
            {
                i++;

            }
            ***//How I can close window from this place***
            Application.Current.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(Application.Current.MainWindow.Close)); // this don't work 

        }
    }
}

最佳答案

您有权访问 vm(或页面)以关闭。例如:

vm.Dispatcher.Invoke(() => vm.Close());

从其他想法我认为这条线是你的问题

private MainWindow vm = new MainWindow();

如果您在此处创建 MainWindow 的 另一个实例,那么显然 vm 是无用的,然后您必须以与您的方式非常相似的方式进行操作:

Application.Current.MainWindow.Dispatcher.Invoke(() =>
{
    Application.Current.MainWindow.Close();
});

我必须尝试查看问题所在,即访问主窗口的调度程序。

这是可行的解决方案(使用 UserControl,应该无关紧要):

public partial class UserControl1 : UserControl
{
    private Dispatcher _dispatcher; // store dispatcher when constructor is called

    public UserControl1()
    {
        InitializeComponent();
        _dispatcher = Dispatcher;

        var thread = new Thread(() => update());
        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();
    }

    void update()
    {
        // simulate some work
        Thread.Sleep(1000);
        // close
        _dispatcher.Invoke(() => Application.Current.MainWindow.Close());
    }
}

关于c# - 如何从页面上的线程关闭窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990941/

相关文章:

c# - FileHelpers 库能否编写包含可为空字段的类以及读取它们?

c# - 连接到 tfs 并下载其中存在的文件 VS2010

c# - WPF格式显示的文字?

c# - 如何创建一个用线条连接两个列表项的控件?

Java 并发 : Threads notifications

linux - 多线程 epoll 服务器 : wake up N threads sleeping on the same epoll fd

c# - 在 asp.net 中持久化用户控制值后回发

WPF 按钮在单击期间更改背景

Java 线程 : ExecutorService delay between threads

c# - 如何使标签在 WinForms 中居中?