1

Boas pessoal,

Tenho uma window em ToolBox WindowStyle="ToolWindow" que serve apenas para dar informações ao utilizador o que se está a passar no BackgroundWorker

Mas estou a ter um problema, eu por exemplo tenho este seguinte codigo que está no evento Activated:

private void WindowDocumentPriceCheck_Activated(object sender, EventArgs e)
{
    this.Activated -= WindowDocumentPriceCheck_Activated;
    BackgroundWorker workerFillTheLists = new BackgroundWorker();
    workerFillTheLists.DoWork += this.workerFillTheLists_DoWork;
    workerFillTheLists.RunWorkerCompleted += this.workerFillTheLists_RunWorkerCompleted;
    this.windowWaitForWorker = new WindowWaitForWorker("Informação", "Por favor aguarde!");
    this.windowWaitForWorker.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    this.windowWaitForWorker.Closed += this.WindowWaitForWorker_Closed;
    workerFillTheLists.RunWorkerAsync();
    // Quero que o utilizador não tenha a hipotesses de carregar nos botões da janela anterior dai estar com ShowDialog();
    this.windowWaitForWorker.ShowDialog();
}

O problema está é quando a Window windowWaitForWorker fecha ele não da o focus a window anterior.

Já tentei varias coisas, deste adicionar o evento Closed activar ou dar focus e nada

private void WindowWaitForWorker_Closed(object sender, EventArgs e)
{
    this.Activate();
    this.Topmost = true;  // important
    this.Topmost = false; // important
    this.Focus();
    this.textBoxBarCode.Focus();
}

Aqui fica o codigo que está por traz da WindowWaitForWorker

public partial class WindowWaitForWorker : Window
{
    private const int GWL_STYLE = -16;
    private const int WS_SYSMENU = 0x00080000;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    /// <summary>
    /// Consctuctor that will receive the title and message to show
    /// </summary>
    /// <param name="title">Title for the window</param>
    /// <param name="message">Message for the user</param>
    public WindowWaitForWorker(string title, string message)
    {
        InitializeComponent();
        this.Title = title;
        this.textBoxMessage.Text = message;
        this.Loaded += this.WindowWaitForWorker_Loaded;
    }

    /// <summary>
    /// Updates the message for the user to see
    /// </summary>
    /// <param name="message">Message for the user</param>
    public void UpdateMessage(string message)
    {
        this.textBoxMessage.Text = message;
    }

    private void WindowWaitForWorker_Loaded(object sender, RoutedEventArgs e)
    {
        var hwnd = new WindowInteropHelper(this).Handle;
        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
    }
}

Nada do que tenha tentado funcionou, porque queria que a janela tenha o focus assim que a WindowWaitForWorker seja fechada AKA quando termina o trabalho do BackgroundWorker

1 Resposta 1

0

Bom a situação ficou resolvida, iniciando a WindowWaitForWorker na sua propria Thread

try
{
    Thread newWindowThread = new Thread(new ThreadStart(() =>
    {
        Dispatcher.Invoke(new Action(() =>
        {
            this.windowWaitForWorker = new WindowWaitForWorker("Informação", "Por favor aguarde!");
            this.windowWaitForWorker.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.windowWaitForWorker.ShowDialog();
        }));
    }));
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.IsBackground = false;
    newWindowThread.Start();
}
catch (Exception ex)
{
}

isto têm que estar dentro do BackgroundWorker DoWork

Sinceramente não consigo perceber a razão de isto acontecer, sempre usei estes metodos em WinForm e sempre fez tudo como deve ser, mas parece que em WPF têm que ser feito assim.

Esta resposta foi-me dada em outro sitio que perguntei, aqui fica o link a onde se encontra esta informação: Where did the answer came from

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .