0

我有一个在系统启动后立即启动的移动应用程序(用于 Windows Mobile 6.1 的 .NET CF2)。

我想强制加载表单,然后他们调用 Web 服务。还有一个手动调用 WS 的按钮。

到目前为止,我还没有设法首先加载表单。首先,它调用服务,然后它们显示表单。

你能帮助我吗?

下面是我正在使用的代码。

//APP主窗体

使用系统;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.Threading;

namespace AtualizaColetores
{
    public partial class frmInicio : Form
    {
        public frmInicio()
        {
            InitializeComponent();
            Program.ShowHide.ShowTopStatusbar(false);



            ThreadPool.QueueUserWorkItem(delegate
            {
                Thread.Sleep(1000);

                //incia animação para indicar processamento em segundo plano
                Cursor.Current = Cursors.WaitCursor;
                Cursor.Show();

                Atualizador executar = new Atualizador();
                executar.AtualizaColetor();
            });


            try
            {
                Process firstProc = new Process();
                firstProc.StartInfo.FileName = @"\WOPT\RF_WOPT.exe";
                firstProc.EnableRaisingEvents = true;

                firstProc.Start();

                firstProc.WaitForExit();
                this.Activate();
                Form destino = new frmControles();
                destino.Show();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro na incialização do programa: " + ex.Message);
                return;
            }




            //Para a animação para indicar processamento em segundo plano
            Cursor.Current = Cursors.Default;
            Cursor.Show();
        }


    }
}

//此处的中心代码ODE下载并替换应用程序的当前版本,如果需要的话

using System;

using System.Collections.Generic;
using System.Text;
using System.IO;

using System.Xml;
using Ionic.Zip;

namespace AtualizaColetores
{
    class Atualizador
    {
        public void AtualizaColetor()
        {

            if (File.Exists(@"\WOPT\About.xml"))
            {
                string versaoAplicativo = "", LocalAplicativo = "", tipoColetor = "";

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(@"\WOPT\About.xml");

                XmlNodeList xnList = xmlDoc.GetElementsByTagName("VersaoRF");

                foreach (XmlNode xn in xnList)
                {
                    versaoAplicativo = xn["versaoAplicativo"].InnerText;
                    LocalAplicativo = xn["localAplicativo"].InnerText;
                    tipoColetor = xn["tipoColetor"].InnerText;
                }

                ConectorWOPT.WOPT executar = new ConectorWOPT.WOPT();

                //inserir tratamento de erro aqui.
                byte[] arquivo = executar.AtualizaColetores(LocalAplicativo, versaoAplicativo, tipoColetor);

                string caminhoWOPT = @"\WOPT";

                if (arquivo.Length > 0)
                {
                    try
                    {
                        MemoryStream ms = new MemoryStream(arquivo);
                        FileStream fs = new FileStream(
                        @"\novaVersao.zip", FileMode.Create);
                        ms.WriteTo(fs);
                        ms.Close();
                        fs.Close();
                        fs = null;
                        ms = null;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }

                    if (System.IO.Directory.Exists(caminhoWOPT))
                    {
                        try
                        {
                            System.IO.Directory.Delete(caminhoWOPT, true);

                            if (!System.IO.File.Exists(caminhoWOPT))
                            {
                                System.IO.Directory.CreateDirectory(caminhoWOPT);
                            }
                        }
                        catch (IOException e)
                        {
                            System.Windows.Forms.MessageBox.Show(e.Message);
                        }
                    }

                    if (File.Exists(@"\novaVersao.zip"))
                    {
                        using (ZipFile zip = ZipFile.Read(@"\novaVersao.zip"))
                        {
                            foreach (ZipEntry e in zip)
                            {
                                e.Extract(@"\WOPT\", ExtractExistingFileAction.OverwriteSilently);  // overwrite == true
                            }
                        }
                        System.IO.File.Delete(@"\novaVersao.zip");

                        System.Windows.Forms.MessageBox.Show("Coletor atualizado com sucesso");

                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Falha na atualização");
                    }
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("O aplicativo não está instalado neste coletor. Contate um supervisor.");
            }

        }
    }
}
4

1 回答 1

2

只需使用计时器或线程延迟处理即可。这些方面的东西:

public frmInicio()
{
    InitializeComponent();
    Program.ShowHide.ShowTopStatusbar(false);

    ThreadPool.QueueUserWorkItem(delegate
    {
        Thread.Sleep(1000); // or however long you need
        btnZip_Click(this, EventArgs.Empty);
    });
}

或者拨打电话OnActivate,检查第一次运行。

编辑 1

您的构造函数中有很多(IMO 太多)在进行。我敢打赌,Form 可见性问题与您在 Form 构造函数中创建一个 Process 并等待该过程完成后再继续这一事实有很大关系。如果该应用程序正在等待您的 Web 服务调用的输出,则线程创建毫无意义,因为您仍然使构造函数串行依赖于调用。

于 2013-04-12T14:18:25.207 回答