相关疑难解决方法(0)

WPF中的Application.DoEvents()在哪里?

我有以下示例代码,每次按下按钮时缩放:

XAML:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Canvas x:Name="myCanvas">

        <Canvas.LayoutTransform>
            <ScaleTransform x:Name="myScaleTransform" />
        </Canvas.LayoutTransform> 

        <Button Content="Button" 
                Name="myButton" 
                Canvas.Left="50" 
                Canvas.Top="50" 
                Click="myButton_Click" />
    </Canvas>
</Window>
Run Code Online (Sandbox Code Playgroud)

*的.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        Console.WriteLine("scale {0}, location: {1}", 
            myScaleTransform.ScaleX,
            myCanvas.PointToScreen(GetMyByttonLocation()));

        myScaleTransform.ScaleX =
            myScaleTransform.ScaleY =
            myScaleTransform.ScaleX + 1;

        Console.WriteLine("scale {0}, location: {1}",
            myScaleTransform.ScaleX,
            myCanvas.PointToScreen(GetMyByttonLocation()));
    }

    private Point GetMyByttonLocation()
    {
        return new Point(
            Canvas.GetLeft(myButton),
            Canvas.GetTop(myButton));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

scale 1, location: …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf xaml

82
推荐指数
5
解决办法
8万
查看次数

Cancelling a pending task synchronously on the UI thread

Sometimes, once I have requested the cancellation of a pending task with CancellationTokenSource.Cancel, I need to make sure the task has properly reached the cancelled state, before I can continue. Most often I face this situation when the app is terminating and I want to cancel all pending task gracefully. However, it can also be a requirement of the UI workflow specification, when the new background process can only start if the current pending one has been fully …

.net c# multithreading task-parallel-library async-await

22
推荐指数
3
解决办法
5104
查看次数

什么等同于WPF应用程序中的Application.DoEvents()

从MSDN,似乎Application.DoEvents()在Windows.Forms中可用.什么是WPF中的等价物.

c# wpf doevents winforms

21
推荐指数
2
解决办法
3万
查看次数

哪些阻塞操作导致STA线程泵送COM消息?

当一个COM对象在STA线程上实例化时,该线程通常必须实现一个消息泵,以便为来回调用其他线程(见这里).

可以手动泵送消息,或者依赖于某些(但不是全部)线程阻塞操作在等待时自动泵送COM相关消息的事实.文档通常无助于决定哪个是哪个(参见相关问题).

如何确定线程阻塞操作是否会在STA上泵送COM消息?

到目前为止的部分清单:

阻断其业务泵*:

阻止泵送的操作:

*注意Noseratio的答案说,即使是操作泵,也是为非常有限的未公开的COM特定消息集.

c# com interop message-pump

17
推荐指数
1
解决办法
3534
查看次数

暂停类似MessageBox.Show()的窗口

我在WPF中实现了一种脚本语言,该语言允许用户编写多个并发操作的脚本。这些动作是使用async / await来实现的,所有这些动作都在主线程上运行。

我现在将逐步调试器引入该脚本语言。当前,有一个与脚本引擎关联的窗口,以及与步骤调试器不同的窗口。

我试图在执行脚本操作之前在引擎窗口中停止进程,同时不阻止调试器窗口上的交互。MessageBox.Show()为此特别有用,因为它会暂停动画,动画本身可能会在完成时开始新的动作。

Thread.Sleep()当然根本不起作用,因为每个人都在主线程上。

C#中是否有类似于MessageBox.Show()但没有UI的暂停机制?我试过调用Dispatcher.DisableProcessing(),我可以宣誓可以暂停引擎窗口,但是现在出现错误:

“调度程序处理已暂停,但消息仍在处理中”

我认为理想情况下,我想做这样的事情:

//Engine Window
private debugger;
void RunAction(ScriptAction action){
 if(action.BreakPoint){
  var pauserObject = PauseProcessesOnThisWindow();
  debugger.break(action, pauseObject);
}
}

//Debugger Window

void Continue_Click(){
 pauseObject.release();
}
Run Code Online (Sandbox Code Playgroud)

如果必须使用MessageBox方法,则可以看到从另一个窗口关闭它很棘手,更不用说在调试器窗口中具有某些控件是一个笨拙的UI,但是仍然需要用户从引擎窗口。

c# wpf dispatcher async-await

2
推荐指数
1
解决办法
312
查看次数