Created
January 22, 2014 07:56
-
-
Save AleksandrT/8555015 to your computer and use it in GitHub Desktop.
Fetches data (images,description) from an external web service asynchronous.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Main : Form | |
{ | |
private CheckInternetConnection iconn; | |
private const string ERROR_LOADING_IMAGE = "Kunde inte ladda bild!\n Välj 'Ladda om' i Meny ovan ellr vänta 30 sec.!"; | |
private const string ERROR_IMAGE_DATA = "Bilden hittades ej eller är korrupt!\n Välj 'Ladda om' i Meny ovan ellr vänta 30 sec.!"; | |
private const string ERROR_NO_NETWORK_ACCESS = "iBrowser hittar inte internet!"; | |
private const string MESSAGE_LOADING = "loading ..."; | |
public Main() | |
{ | |
InitializeComponent(); | |
MainApplicationWorkProcess(); | |
} | |
/// <summary> | |
///1. Turn off and despose Timer if is on. | |
///2. Hide panel with a Image on it. | |
///3. Show loading message. | |
///4. Check if the network is on. | |
/// 4.1 If networ is off , show error msg do noting! | |
/// | |
///5. Get Image info | |
///6. Show panel when image is fetched and there are no errors. | |
///7. Init and turn timer on. | |
/// </summary> | |
private void MainApplicationWorkProcess() | |
{ | |
if (timer1.Enabled) | |
{ | |
timer1.Stop(); | |
timer1.Dispose(); | |
} | |
pnlShow.Visible = false; | |
lblLoading.Visible = true; | |
lblLoading.Text = MESSAGE_LOADING; | |
iconn = new CheckInternetConnection(); | |
if (iconn.InternetConnected()) | |
{ | |
FetchImage fetchImage = new FetchImage(); | |
Model.Image _image = fetchImage.GetImage(); | |
if (_image != null) | |
{ | |
SetImage(_image); | |
} | |
else | |
{ | |
lblLoading.Text = ERROR_IMAGE_DATA; | |
} | |
} | |
else | |
{ | |
lblLoading.Text = ERROR_NO_NETWORK_ACCESS; | |
} | |
//30 secs | |
timer1.Interval = (3000) * 10; | |
timer1.Enabled = true; | |
timer1.Start(); | |
timer1.Tick += new EventHandler(laddaOmToolStripMenuItem_Click); | |
} | |
/// <summary> | |
/// While image is loading show loading message. | |
/// new Event AsyncCompleted has | |
/// </summary> | |
/// <param name="_image"></param> | |
private void SetImage(Model.Image _image) | |
{ | |
lblComment.Text = _image.Comment; | |
pictureBox1.WaitOnLoad = false; | |
pictureBox1.LoadAsync(_image.Url); | |
pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(pictureBox1_LoadCompleted); | |
} | |
/// <summary> | |
/// When Limage is fetched show panel with comments to. | |
/// If error during the work - show Error insted of Loading message | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) | |
{ | |
if (e.Error != null) | |
{ | |
lblLoading.Text = ERROR_LOADING_IMAGE; | |
} | |
else | |
{ | |
lblLoading.Visible = false; | |
pnlShow.Visible = true; | |
} | |
} | |
/// <summary> | |
/// Calll Om form | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void omToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
Om _om = new Om(); | |
_om.Show(); | |
} | |
/// <summary> | |
/// Close the app | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void avslutaToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
this.Close(); | |
} | |
/// <summary> | |
/// Call for a new image via menu. | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void laddaOmToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
MainApplicationWorkProcess(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment