在sperate线程中显示加载窗口时

在sperate线程中显示加载窗口时

本文介绍了在sperate线程中显示加载窗口时,主窗口窗体失去焦点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单独的帖子中显示加载窗口表单。我的问题是当我的任务结束时我通过调用Close()方法关闭窗口但是我的主窗口失去焦点以获得焦点我需要从任务栏中选择窗体。

这里是我的代码:



Hi, I am showing a loading window form in separate thread. My problem is when my task is over i am closing the window by calling Close() method but my main window is losing focus for getting focus back i need to select the form from the task bar.
Here is my code :

  Thread thread = new Thread(new ThreadStart(ShowLoading));
  thread.Start();

//perform heavy task

CloseLoading(); // this method will close the loading form

  this.Focus() // its not working.

//Note : after closing the loading form. the main form is losing focus.





请帮助我..我也通过调用this.Focus()进行检查CloseLoading()结束时的方法;方法,但它不工作。



Please help me.. i have also checked by calling this.Focus() method at the end of CloseLoading(); method but its not working.

推荐答案

using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace SecondaryFormByThread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Form2 instanceOfForm2;
        Thread thread;

        // this will hold the Data generated in Form2
        List<int> FormData = new List<int>();

        // show the instance of Form2 which will generate the data
        private void button1_Click(object sender, EventArgs e)
        {
            ShowLoading();
        }

        private void ShowLoading()
        {
            instanceOfForm2 = new Form2();

            // inject the reference to the List<int> in Form1 into the matching List<int> Form2
            instanceOfForm2.Form2Data = FormData;

            // inject the 'DataComplete method in Form1 into the matching Action in Form2
            instanceOfForm2.DataComplete = DataComplete;

            // use 'ShowDialog so Form2 will remain visible
            thread = new Thread(new ThreadStart(() => instanceOfForm2.ShowDialog()));

            thread.Start();
        }

        private void DataComplete(bool closeThreadOkay)
        {
           textBox1.Text = string.Format("finished: data count = {0}", FormData.Count);

           // any code after this call will not be executed !
           if (thread.IsAlive && closeThreadOkay) thread.Abort();
        }
    }
}

2。 Form2:Form2有一个TextBox,'textBox1。我选择使用BorderStyle.FixedToolWindow创建Form2,没有'Text,没有'CloseButton。

2. Form2: Form2 has a TextBox, 'textBox1. I chose to make Form2 with BorderStyle.FixedToolWindow, no 'Text, and no 'CloseButton.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace SecondaryFormByThread
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            this.TopLevel = true;
        }

        // list injected by Form1
        public List<int> Form2Data { set; get; }

        // method injected by Form1
        public Action<bool> DataComplete;

        private void Form2_Load(object sender, EventArgs e)
        {
            this.TopLevel = true;
        }

        // keep it simple ?: let Form2 appear 'inactive
        protected override bool ShowWithoutActivation
        {
            get { return true; }
        }

        private void Form2_Shown(object sender, EventArgs e)
        {
            // some kind of code that creates the data ...
            for (int i = 0; i < 100; i++)
            {
                // add to the Data that remain usable in 'Form1
                Form2Data.Add(i);

                Thread.Sleep(50);

                // simulate some kind of progress report
                textBox1.AppendText("-");
            }

            // use of invoke lets us do something in Form1's UI
            // without a cross-thread UI update error
            Invoke(new MethodInvoker(Finish));
        }

        private void Finish()
        {
            // call the method injected by Form1
            DataComplete(true);
        }
    }
}



这篇关于在sperate线程中显示加载窗口时,主窗口窗体失去焦点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:32