C# 启动屏幕等待线程完成

C# 启动屏幕等待线程完成,c#,winforms,splash-screen,C#,Winforms,Splash Screen,我仍然对启动屏幕有问题。我不想使用属性SC.TopMost=true 现在我的应用场景如下: 在progeram.cs中: [STAThread] static void Main() { new SplashScreen(_tempAL);// where _tempAL is an arrayList Application.Run(new Form1(_tempAL)); } public SplashScreen(ArrayList _Data) { Displ

我仍然对启动屏幕有问题。我不想使用属性
SC.TopMost=true

现在我的应用场景如下:

在progeram.cs中:

[STAThread]
static void Main()
{
    new SplashScreen(_tempAL);// where _tempAL is an arrayList
    Application.Run(new Form1(_tempAL));
}
public SplashScreen(ArrayList _Data)
{
    DisplaySplash()
} 
private void DisplaySplash()
{
    this.Show();
    this.TopMost = true;
    this.CenterToScreen();
    this.SetTopLevel(true);

    _allServerNarrators = new string[10];
    for (int i = 0; i < _allServerNarrators.Length; i++)
        _allServerNarrators[i] = null;

    GetFromServer();

    this.Hide();
    _serverData = new ArrayList();
    _thisData.Add(_allServerNarrators);
    _thisData.Add(_serverNarrators);

}
private void GetFromServer()
{
    _serverNarrators = new ArrayList();
    string _file = "Suras.serverNar";

    if (!Directory.Exists("c:\\ASGAQuraan"))
        Directory.CreateDirectory("c:\\ASGAQuraan");

    while (counter < 4 && _serverFiles == null)
    {
        if (Download("c:\\ASGAQuraan", _ftpServerIP, _file))
        {
            StreamReader _strReader = new StreamReader
                         ("c:\\ASGAQuraan\\"+_file,System.Text.Encoding.Default);
            string _line = _strReader.ReadLine();
            string _word;

            while (true)
            {
                while (_line != null)
                {
                    _word = _line.Substring(0, _line.IndexOf("*"));
                    int _narId = Convert.ToInt32(_word);
                    _line = _line.Substring(2);
                    int k = 0;
                    _serverNarratorNode = new ArrayList();
                    while (true)
                    {
                        int ind = _line.IndexOf("*");
                        if (ind > 0 && ind < _line.Length)
                        {
                            string str = _line.Substring(0, (ind));
                            if (k == 0)
                            {
                                _allServerNarrators[_narId] = str;
                                _serverNarratorNode.Add(str);
                            }
                            else
                            {
                                _serverNarratorNode.Add(str);
                            }
                            _line = _line.Substring(ind + 1);
                            k++;
                        }
                        else
                        {
                            _line = null;
                            break;
                        }
                    }
                    _serverNarrators.Add(_serverNarratorNode);
                    _serverFiles = "added";
                }
                _line = _strReader.ReadLine();
                if (_line == null)
                {
                    break;
                }
            }
        }
        else
            counter++;
    }
}
在SplashScreen类中:

[STAThread]
static void Main()
{
    new SplashScreen(_tempAL);// where _tempAL is an arrayList
    Application.Run(new Form1(_tempAL));
}
public SplashScreen(ArrayList _Data)
{
    DisplaySplash()
} 
private void DisplaySplash()
{
    this.Show();
    this.TopMost = true;
    this.CenterToScreen();
    this.SetTopLevel(true);

    _allServerNarrators = new string[10];
    for (int i = 0; i < _allServerNarrators.Length; i++)
        _allServerNarrators[i] = null;

    GetFromServer();

    this.Hide();
    _serverData = new ArrayList();
    _thisData.Add(_allServerNarrators);
    _thisData.Add(_serverNarrators);

}
private void GetFromServer()
{
    _serverNarrators = new ArrayList();
    string _file = "Suras.serverNar";

    if (!Directory.Exists("c:\\ASGAQuraan"))
        Directory.CreateDirectory("c:\\ASGAQuraan");

    while (counter < 4 && _serverFiles == null)
    {
        if (Download("c:\\ASGAQuraan", _ftpServerIP, _file))
        {
            StreamReader _strReader = new StreamReader
                         ("c:\\ASGAQuraan\\"+_file,System.Text.Encoding.Default);
            string _line = _strReader.ReadLine();
            string _word;

            while (true)
            {
                while (_line != null)
                {
                    _word = _line.Substring(0, _line.IndexOf("*"));
                    int _narId = Convert.ToInt32(_word);
                    _line = _line.Substring(2);
                    int k = 0;
                    _serverNarratorNode = new ArrayList();
                    while (true)
                    {
                        int ind = _line.IndexOf("*");
                        if (ind > 0 && ind < _line.Length)
                        {
                            string str = _line.Substring(0, (ind));
                            if (k == 0)
                            {
                                _allServerNarrators[_narId] = str;
                                _serverNarratorNode.Add(str);
                            }
                            else
                            {
                                _serverNarratorNode.Add(str);
                            }
                            _line = _line.Substring(ind + 1);
                            k++;
                        }
                        else
                        {
                            _line = null;
                            break;
                        }
                    }
                    _serverNarrators.Add(_serverNarratorNode);
                    _serverFiles = "added";
                }
                _line = _strReader.ReadLine();
                if (_line == null)
                {
                    break;
                }
            }
        }
        else
            counter++;
    }
}
公共SplashScreen(ArrayList\u数据)
{
displayplash()
} 
私有void displaysh()
{
this.Show();
this.TopMost=true;
此.CenterToScreen();
此参数为.SetTopLevel(true);
_AllServer叙述者=新字符串[10];
对于(int i=0;i<\u allserver叙述者.Length;i++)
_AllServer叙述者[i]=null;
GetFromServer();
this.Hide();
_serverData=newarraylist();
_thisData.Add(_allserver叙述者);
_thisData.Add(_server叙述者);
}
私有void GetFromServer()
{
_Server叙述者=新的ArrayList();
字符串_file=“Suras.serverNar”;
如果(!Directory.Exists(“c:\\ASGAQuraan”))
目录.CreateDirectory(“c:\\ASGAQuraan”);
while(计数器<4&&U serverFiles==null)
{
如果(下载(“c:\\ASGAQuraan”,\u ftpServerIP,\u文件))
{
StreamReader\u strReader=新的StreamReader
(“c:\\ASGAQuraan\\”+_文件,System.Text.Encoding.Default);
字符串_line=_strReader.ReadLine();
字串;
while(true)
{
while(_line!=null)
{
_word=_line.Substring(0,_line.IndexOf(“*”);
int _narId=转换为32(_字);
_行=_行。子字符串(2);
int k=0;
_ServerAttendatorNode=新的ArrayList();
while(true)
{
int ind=_line.IndexOf(“*”);
如果(ind>0&&ind<\u行长度)
{
string str=_line.Substring(0,(ind));
如果(k==0)
{
_所有服务器叙述者[\u narId]=str;
_添加(str);
}
其他的
{
_添加(str);
}
_line=_line.Substring(ind+1);
k++;
}
其他的
{
_行=空;
打破
}
}
_添加(\u ServerAttenderNode);
_serverFiles=“已添加”;
}
_line=_strReader.ReadLine();
如果(_line==null)
{
打破
}
}
}
其他的
计数器++;
}
}
我想要的是splashscreen类中的一些东西,它会一直等到线程完成


有关更多详细信息,请告诉我我需要告诉您什么。

在两个线程之间进行跟踪有点令人困惑,但我想尝试一下并说一下

我不完全理解您在这里的设计,但如果问题是当您启动第二个应用程序时,启动屏幕窗体会变成白色。。。这很可能是因为启动屏幕正忙于执行GetFromServer()中的所有代码。忙得连重新粉刷自己的时间都没有


为了解决这个问题,我建议您使用来执行GetFromServer方法。这将在一个单独的线程中运行该方法,并让窗体的线程自由地重新绘制自身。

您确实应该提供有关问题的更多详细信息。我可能完全错了,但我要在黑暗中试一试。从我想象的情况来看,你想要,你想要启动屏幕显示,在另一个线程中进行一些处理,然后启动屏幕在完成后消失

为此,您需要将
GetFromServer()
调用移动到
BackgroundWorker
。然后移动鼠标

    this.Hide();
    _serverData = new ArrayList();
    _thisData.Add(_allServerNarrators);
    _thisData.Add(_serverNarrators);
将代码添加到
BackgroundWorker\u runworker completed
事件处理程序

要使用
BackgroundWorker

1) 初始化
BackGroundWorker

  BackgroundWorker myWorker = new BackgroundWorker();
2) 添加事件处理程序

  myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);
  //put the work you want done in this one

  myWorker.RunWorkerCompleted += 
      new RunWorkerCompletedEventHandler(myWorker_RunWorkerCompleted);
  //this gets fired when the work is finished
3) 向事件处理程序添加代码

4) 调用
myWorker.RunWorkerAsync()
开始工作


另外,您似乎没有对传递给启动屏幕构造函数的
ArrayList
执行任何操作。这是有意的吗?

同样的问题,同样的答案:

NET框架对启动屏幕具有出色的内置支持。启动新的WF项目,项目+添加引用,选择Microsoft.VisualBasic。添加一个新表单,称之为frmSplash。打开Project.cs并使其如下所示:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WindowsFormsApplication1 {
  static class Program {
    [STAThread]
    static void Main(string[] args) {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      new MyApp().Run(args);
    }
  }
  class MyApp : WindowsFormsApplicationBase {
    protected override void OnCreateSplashScreen() {
      this.SplashScreen = new frmSplash();
    }
    protected override void OnCreateMainForm() {
      // Do your time consuming stuff here...
      //...
      System.Threading.Thread.Sleep(3000);
      // Then create the main form, the splash screen will close automatically
      this.MainForm = new Form1();
    }
  }
}

通过在调用Application.Run()之前创建UI,您已经进入了危险区域。Run本质上是程序的消息泵。通过在启动应用程序的消息泵之前显示UI,可以使典型的UI交互实际上不可能在不成熟的UI上进行。对于启动屏幕而言,这似乎不相关,但如果(例如)有人请求在单击启动屏幕时使其消失,或者您想使用BackgroundWorker,则这将很重要

可以通过在初始屏幕中创建消息泵(通过调用ShowDialog()而不是Show()使其成为模态)来解决这些问题,但在处理问题时,这就是治疗症状,其实并不难

在这种情况下,我强烈鼓励。该框架提供了您需要的支持。虽然Microsoft.VisualBasic命名空间中的功能对于C#程序员来说并不总是很容易发现,但对于此类情况,它们可以真正节省时间和生命

祝你好运

不幸的是,我没有