How to hide/close Splash screen WPF window using child thread?

  • Thread starter Thread starter Vlad Mishyn
  • Start date Start date
V

Vlad Mishyn

Guest
Hello;)

I am making WPF C# aplication to parse GOOGLE pages and make some predictions based on that info.(university project)

My application will have 3 forms: SplashScreen, SearchScreen, Result Screen.

I use SplashScreen in order to load some recent google stock data and parse it.

Because of the amount of data i need to work with, my app uses 4 child threads to load and parse each page separately.

The problem is that splash screen has to be open till last thread finishes parsing.

After last thread finishes, app has to create new window and pass data to it, and close first window.

I sucesfully created new window with child thread but i can't close/hide previous one.

I am complitely aware that window has to be closed with main thread but in my case it's simply imposible.

Not to mention in windows forms i neved had issues with that.

Does anyone know what can i do with that?

The last method which colects all data and closes previous form is GroupData() but it didn't work:(



using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Threading;
using System.Windows.Media;
using System.Windows.Resources;
using System.Windows.Threading;
using System.Windows.Media.Imaging;



namespace ASIX
{
public partial class CloseDelagate : Window
{


int counter = 0;

int finished = 0;

int iterator1, iterator2, iterator3, iterator4;

string[] data1 = new string[5];

string[] data2 = new string[5];

string[] data3 = new string[5];

string[] data4 = new string[5];

string[,] ArrayData = new string[4, 5];


// 1 2 3 4 5 6 7 8
string[] CompanyNames = { "GOOGLE", "APPLE", "WINDOWS", "Intel", "AMD", "Facebook", "AMAZON", "Johnson & Johnson" };
string[] urlList = {
"google stock - Google Search",
"apple stock - Google Search",
"windows stock - Google Search",
"intel stock - Google Search",
"amd stock - Google Search",
"facebook stock - Google Search",
"amazon stock - Google Search",
"johnson stock - Google Search",
};


DispatcherTimer dispatcherTimer = new DispatcherTimer();
Thread ThreadServer1, ThreadServer2, ThreadServer3, ThreadServer4;


public CloseDelagate()
{
InitializeComponent();

dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(17);
dispatcherTimer.Start();


if (File.Exists("config1.txt"))
{

//read data
int var = Int32.Parse(File.ReadAllText(@"config1.txt", Encoding.UTF8).ToString());

if (var == 4)
{
iterator1 = 0;
iterator2 = 1;
iterator3 = 2;
iterator4 = 3;

// delete

File.Delete(@"config1.txt");
using (StreamWriter sw = new StreamWriter(@"config1.txt", true, System.Text.Encoding.Default))
{ sw.WriteLine("8"); }

}


if (var == 8)
{
iterator1 = 4;
iterator2 = 5;
iterator3 = 6;
iterator4 = 7;

// delete

File.Delete(@"config1.txt");
using (StreamWriter sw = new StreamWriter(@"config1.txt", true, System.Text.Encoding.Default))
{ sw.WriteLine("4"); }

}
}else{

using (StreamWriter sw = new StreamWriter(@"config1.txt", true, System.Text.Encoding.Default))
{ sw.WriteLine("8"); }
iterator1 = 0;
iterator2 = 1;
iterator3 = 2;
iterator4 = 3;

}


// START
ThreadServer1 = new Thread(new ThreadStart(FetchData1));
ThreadServer1.SetApartmentState(ApartmentState.STA);
ThreadServer2 = new Thread(new ThreadStart(FetchData2));
ThreadServer2.SetApartmentState(ApartmentState.STA);
ThreadServer3 = new Thread(new ThreadStart(FetchData3));
ThreadServer3.SetApartmentState(ApartmentState.STA);
ThreadServer4 = new Thread(new ThreadStart(FetchData4));
ThreadServer4.SetApartmentState(ApartmentState.STA);

ThreadServer1.Start();
ThreadServer2.Start();
ThreadServer3.Start();
ThreadServer4.Start();
}



public void FetchData1()
{

StringBuilder html = new StringBuilder();
StringBuilder parser = new StringBuilder();


//page download
using (WebClient client = new WebClient())
{
client.DownloadFile(urlList[iterator1], @"1.html");
}

//open as HTML
html.Append(File.ReadAllText(@"1.html", Encoding.UTF8));

parser.Clear();
parser.Append(html.ToString());


if ((html.ToString().Contains("Performance Outlook</div>") &&
html.ToString().Contains("Day's Range")
))
{

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Previous Close"), 140));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Wnd") + 5));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

data1[0] = parser.ToString();
data1[1] = "none";
data1[2] = "none";
data1[3] = "none";
data1[4] = CompanyNames[iterator1];

}
else
{

parser.Clear();
parser.Append(html.ToString());


//1
parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("class=\"rQMQod") - 30, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));


parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

if (parser.ToString().Contains("�"))
{
parser.Replace(parser.ToString(), parser.ToString().Replace("�", " "));
}

data1[0] = parser.ToString();

//2-3
parser.Clear();
parser.Append(html.ToString());

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("<span class=\"rQMQod") + 13, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));

data1[1] = parser.ToString().Substring(0, parser.ToString().IndexOf("("));

parser.Replace(parser.ToString(), parser.ToString().Substring(parser.ToString().IndexOf("("), 20));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("(") + 1));

data1[2] = parser.ToString().Substring(0, parser.ToString().IndexOf(")"));

//4
if (data1[1].Contains("+")) { data1[3] = "GREEN"; } else { data1[3] = "RED"; }
//5
data1[4] = CompanyNames[iterator1];

}

finished++;
GroupData();
}


public void FetchData2()
{

StringBuilder html = new StringBuilder();
StringBuilder parser = new StringBuilder();

//page download
using (WebClient client = new WebClient())
{
client.DownloadFile(urlList[iterator2], @"2.html");
}

//open as HTML
html.Append(File.ReadAllText(@"2.html", Encoding.UTF8));

parser.Clear();
parser.Append(html.ToString());


if ((html.ToString().Contains("Performance Outlook</div>") &&
html.ToString().Contains("Day's Range")
))
{

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Previous Close"), 140));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Wnd") + 5));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

data2[0] = parser.ToString();
data2[1] = "none";
data2[2] = "none";
data2[3] = "none";
data2[4] = CompanyNames[iterator2];

}
else
{

parser.Clear();
parser.Append(html.ToString());


//1
parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("class=\"rQMQod") - 30, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));


parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

if (parser.ToString().Contains("�"))
{
parser.Replace(parser.ToString(), parser.ToString().Replace("�", " "));
}

data2[0] = parser.ToString();

//2-3
parser.Clear();
parser.Append(html.ToString());

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("<span class=\"rQMQod") + 13, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));

data2[1] = parser.ToString().Substring(0, parser.ToString().IndexOf("("));

parser.Replace(parser.ToString(), parser.ToString().Substring(parser.ToString().IndexOf("("), 20));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("(") + 1));

data2[2] = parser.ToString().Substring(0, parser.ToString().IndexOf(")"));

//4
if (data2[1].Contains("+")) { data2[3] = "GREEN"; } else { data2[3] = "RED"; }
//5
data2[4] = CompanyNames[iterator2];

}

finished++;
GroupData();
}



public void FetchData3()
{

StringBuilder html = new StringBuilder();
StringBuilder parser = new StringBuilder();

//page download
using (WebClient client = new WebClient())
{
client.DownloadFile(urlList[iterator3], @"3.html");
}

//open as HTML
html.Append(File.ReadAllText(@"3.html", Encoding.UTF8));

parser.Clear();
parser.Append(html.ToString());


if ((html.ToString().Contains("Performance Outlook</div>") &&
html.ToString().Contains("Day's Range")
))
{

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Previous Close"), 140));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Wnd") + 5));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

data3[0] = parser.ToString();
data3[1] = "none";
data3[2] = "none";
data3[3] = "none";
data3[4] = CompanyNames[iterator3];

}
else
{

parser.Clear();
parser.Append(html.ToString());


//1
parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("class=\"rQMQod") - 30, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));


parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

if (parser.ToString().Contains("�"))
{
parser.Replace(parser.ToString(), parser.ToString().Replace("�", " "));
}

data3[0] = parser.ToString();

//2-3
parser.Clear();
parser.Append(html.ToString());

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("<span class=\"rQMQod") + 13, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));

data3[1] = parser.ToString().Substring(0, parser.ToString().IndexOf("("));

parser.Replace(parser.ToString(), parser.ToString().Substring(parser.ToString().IndexOf("("), 20));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("(") + 1));

data3[2] = parser.ToString().Substring(0, parser.ToString().IndexOf(")"));

//4
if (data3[1].Contains("+")) { data3[3] = "GREEN"; } else { data3[3] = "RED"; }
//5
data3[4] = CompanyNames[iterator3];

}

finished++;
GroupData();
}

public void FetchData4()
{

StringBuilder html = new StringBuilder();
StringBuilder parser = new StringBuilder();

//page download
using (WebClient client = new WebClient())
{
client.DownloadFile(urlList[iterator4], @"4.html");
}

//open as HTML
html.Append(File.ReadAllText(@"4.html", Encoding.UTF8));

parser.Clear();
parser.Append(html.ToString());


if ((html.ToString().Contains("Performance Outlook</div>") &&
html.ToString().Contains("Day's Range")
))
{

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Previous Close"), 140));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("Wnd") + 5));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

data4[0] = parser.ToString();
data4[1] = "none";
data4[2] = "none";
data4[3] = "none";
data4[4] = CompanyNames[iterator4];

}
else
{

parser.Clear();
parser.Append(html.ToString());


//1
parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("class=\"rQMQod") - 30, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));


parser.Replace(parser.ToString(), parser.ToString()
.Substring(0, parser.ToString().IndexOf("<")));

if (parser.ToString().Contains("�"))
{
parser.Replace(parser.ToString(), parser.ToString().Replace("�", " "));
}

data4[0] = parser.ToString();

//2-3
parser.Clear();
parser.Append(html.ToString());

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("<span class=\"rQMQod") + 13, 50));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf(">") + 1));

data4[1] = parser.ToString().Substring(0, parser.ToString().IndexOf("("));

parser.Replace(parser.ToString(), parser.ToString().Substring(parser.ToString().IndexOf("("), 20));

parser.Replace(parser.ToString(), parser.ToString()
.Substring(parser.ToString().IndexOf("(") + 1));

data4[2] = parser.ToString().Substring(0, parser.ToString().IndexOf(")"));

//4
if (data4[1].Contains("+")) { data4[3] = "GREEN"; } else { data4[3] = "RED"; }
//5
data4[4] = CompanyNames[iterator4];

}

finished++;
GroupData();
}



public void GroupData()
{
if (finished == 4)
{

ArrayData[0, 0] = data1[0];
ArrayData[0, 1] = data1[1];
ArrayData[0, 2] = data1[2];
ArrayData[0, 3] = data1[3];
ArrayData[0, 4] = data1[4];

ArrayData[1, 0] = data2[0];
ArrayData[1, 1] = data2[1];
ArrayData[1, 2] = data2[2];
ArrayData[1, 3] = data2[3];
ArrayData[1, 4] = data2[4];


ArrayData[2, 0] = data3[0];
ArrayData[2, 1] = data3[1];
ArrayData[2, 2] = data3[2];
ArrayData[2, 3] = data3[3];
ArrayData[2, 4] = data3[4];

ArrayData[3, 0] = data4[0];
ArrayData[3, 1] = data4[1];
ArrayData[3, 2] = data4[2];
ArrayData[3, 3] = data4[3];
ArrayData[3, 4] = data4[4];

dispatcherTimer.Stop();
Window2 form = new Window2();
form.setParameters(ArrayData);
form.Show();
System.Windows.Threading.Dispatcher.Run();


Dispatcher.BeginInvoke((Action)(() =>
{
this.Close();
}));



// Dispatcher.BeginInvoke(new ThreadStart(delegate { this.Close(); }));

}



}




private void dispatcherTimer_Tick(object sender, EventArgs e)
{

if (counter == 0) {


Uri resourceUri = new Uri("Images/spinner1.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;


}
if (counter == 1) {
Uri resourceUri = new Uri("Images/spinner2.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 2) {
Uri resourceUri = new Uri("Images/spinner3.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 3) {
Uri resourceUri = new Uri("Images/spinner4.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 4) {
Uri resourceUri = new Uri("Images/spinner5.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 5) {
Uri resourceUri = new Uri("Images/spinner6.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 6) {
Uri resourceUri = new Uri("Images/spinner7.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}
if (counter == 7) {
Uri resourceUri = new Uri("Images/spinner1.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
BorderImage.Background = brush;
}


if (counter == 7) { counter = 0; }
counter++;



}

}
}

Continue reading...
 
Back
Top