Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
QuestionScan Image Pin
Hossein Afyuoni4-Oct-08 19:19
Hossein Afyuoni4-Oct-08 19:19 
AnswerRe: Scan Image Pin
DaveyM694-Oct-08 23:01
professionalDaveyM694-Oct-08 23:01 
AnswerRe: Scan Image Pin
Mark Salsbery6-Oct-08 6:53
Mark Salsbery6-Oct-08 6:53 
QuestionManage Print Event Pin
Laji594-Oct-08 18:53
Laji594-Oct-08 18:53 
AnswerRe: Manage Print Event Pin
Jaffer Mumtaz4-Oct-08 20:32
Jaffer Mumtaz4-Oct-08 20:32 
QuestionHow to print RichTextBox Pin
Laji594-Oct-08 18:46
Laji594-Oct-08 18:46 
AnswerRe: How to print RichTextBox Pin
DaveyM694-Oct-08 22:56
professionalDaveyM694-Oct-08 22:56 
Question.Net threading: get the concept, no idea how to implement Pin
MarkGMcM4-Oct-08 14:58
MarkGMcM4-Oct-08 14:58 
HI,

I have a little application that downloads Adam Carolla podcasts. Right now, it does not contain threading. I have read a lot of articles and get the concept, but I am drawing a blank when it comes to actually implementing it in my application.

If anyone could take the time to add threading to this app, I would greatly appreciate it. If you're a fan of the Ace Man, feel free to use this. I am really hoping to understand this via my real life example so I may truly grasp the concept enough to be able to do this myself on other apps.

I only want to download two MP3s at a time. This is .Net 1.1 code.

Regards,

Mark


the code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using Pantheon.Software.Networking;


namespace FetchAceMan
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnFetchAceMan;
		private System.Windows.Forms.ListBox listContents;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.Label lblProgress;
		private System.Windows.Forms.Label lblActivityLog;
		private System.Windows.Forms.Label lblURLList;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
                // Removed for brevity
		#endregion

		#region Handler for the "Get the URLs" button
		private void btnFetchAceMan_Click(object sender, System.EventArgs e)
		{
			GetTheURLs();
			GetTheMP3s();
		}
		#endregion

		private void GetTheURLs()
		{
			string strAceManRSS = "http://www.971freefm.com/pages/podcast/43.rss";
			
			XmlDocument doc = new XmlDocument();
			doc.Load(strAceManRSS);

			XPathNavigator nav = doc.CreateNavigator();
			XPathExpression expr;
			expr = nav.Compile("/rss/channel/item/link");
			XPathNodeIterator iterator = nav.Select(expr);
			listContents.Items.Clear();
			try
			{
				while (iterator.MoveNext())
				{
					XPathNavigator nav2 = iterator.Current.Clone();
					listContents.Items.Add(nav2.Value.Trim());
				}
			}
			catch(Exception ex) 
			{
				textBox6.Text += ex.Message + System.Environment.NewLine;
				textBox6.Focus();
				textBox6.Select(textBox6.TextLength, 0);
				textBox6.ScrollToCaret();
			}
			finally
			{
				listContents.Refresh();
			}
		}

		private void GetTheMP3s()
		{
			this.textBox6.Clear();
			try 
			{
				// Determine whether the directory exists.
				string targetDir = @"c:\AdamCarollaShow";
				if (!Directory.Exists(targetDir)) 
				{
					// Try to create the directory.
					DirectoryInfo di = Directory.CreateDirectory(targetDir);
				}
			} 
			catch (Exception ex) 
			{
				textBox6.Text += ex.Message + System.Environment.NewLine;
				textBox6.Focus();
				textBox6.Select(textBox6.TextLength, 0);
				textBox6.ScrollToCaret();
			} 
			finally {}

			// Open or Create a new file.
			FileStream fs = new FileStream(@"C:\Program Files\McMorrine\Get the AceMan\LastDownload.txt", FileMode.OpenOrCreate);

			// Create a reader and specify the encoding.
			StreamReader r = new StreamReader(fs, Encoding.UTF8);
			int lastFile = int.Parse(r.ReadLine());
			r.Close();
			fs.Close();

			int numItems = listContents.Items.Count;
			int theItem = numItems - 1;
			while (theItem >= 0)
			{
				DoDownload(listContents.Items[theItem].ToString(), lastFile);
				theItem --;
			}
			textBox6.Text += "****" + System.Environment.NewLine + 
				"All of the files have been downloaded." + System.Environment.NewLine +
				"They can be found in the C:\\AdamCarollaShow directory" + System.Environment.NewLine;
			textBox6.Focus();
			textBox6.Select(textBox6.TextLength, 0);
			textBox6.ScrollToCaret();
		}
		

		#region DoDownload file downloading routine
		private void DoDownload(string theItem, int lastFile)
		{
			try
			{
				FileDownloader downloader = new FileDownloader();
				downloader.DownloadCompleted += new FileDownloadCompletedEventHandler(downloader_DownloadCompleted);
				downloader.DownloadProgressChanged += new FileDownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
				downloader.DownloadStatusChanged += new FileDownloadStatusChangedEventHandler(downloader_DownloadStatusChanged);
				
				// Parse the URL to get the name of the file for purposes
				// of writing to the log and to compare with the last file downloaded
				String strFile = theItem;
				int intFileExt = strFile.IndexOf(".mp3");
				int intFileNameStart = strFile.LastIndexOf("/");
				
				// This is the filename to be compared to the passed integer to decide whether to download the file
				int fileNameForDownload = int.Parse(strFile.Substring(intFileNameStart +1, intFileExt - (intFileNameStart + 1)));
				
				// This is the filename to display in the log
				string strFileName = strFile.Substring(intFileNameStart +1);
				
				this.textBox6.Text += "Filename to download: " + strFileName + System.Environment.NewLine;
				textBox6.Focus();
				textBox6.Select(textBox6.TextLength, 0);
				textBox6.ScrollToCaret();

				if (fileNameForDownload > lastFile)
				{
					this.textBox6.Text += "Download the file " + strFileName + "." + System.Environment.NewLine;
					textBox6.Focus();
					textBox6.Select(textBox6.TextLength, 0);
					textBox6.ScrollToCaret();

					// Open or Create a new file.
					FileStream fs = new FileStream(@"C:\Program Files\McMorrine\Get the AceMan\LastDownload.txt", FileMode.Create);

					// Create a writer and specify the encoding.
					StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
					w.Write(fileNameForDownload.ToString());
					w.Flush();
					w.Close();
					downloader.Download(new Uri(theItem), @"C:\AdamCarollaShow");
				}
				else
				{
					this.textBox6.Text += "Already have " + strFileName + ", skip the download." + System.Environment.NewLine;
					textBox6.Focus();
					textBox6.Select(textBox6.TextLength, 0);
					textBox6.ScrollToCaret();
					return;
				}
			}
			catch(Exception ex) 
			{
				textBox6.Text += ex.Message + System.Environment.NewLine;
				textBox6.Focus();
				textBox6.Select(textBox6.TextLength, 0);
				textBox6.ScrollToCaret();
			}
		}
		#endregion

		#region downloader_DownloadCompleted
		private void downloader_DownloadCompleted(object sender, FileDownloadCompletedEventArgs e)
		{
			this.progressBar1.Value = 0;
			textBox6.Text += "Download complete." + System.Environment.NewLine;
			textBox6.Focus();
			textBox6.Select(textBox6.TextLength, 0);
			textBox6.ScrollToCaret();
		}
		#endregion

		#region downloader_DownloadStatusChanged
		private void downloader_DownloadStatusChanged(object sender, FileDownloadStatusChangedEventArgs e)
		{
			textBox6.Text += e.Message + System.Environment.NewLine;
			textBox6.Focus();
			textBox6.Select(textBox6.TextLength, 0);
			textBox6.ScrollToCaret();
		}
		#endregion

		#region downloader_DownloadProgressChanged
		private void downloader_DownloadProgressChanged(object sender, FileDownloadProgressChangedEventArgs e)
		{
			this.progressBar1.Value = e.ProgressPercentage;
		}
		#endregion
	}
}

AnswerRe: .Net threading: get the concept, no idea how to implement Pin
DaveyM694-Oct-08 23:15
professionalDaveyM694-Oct-08 23:15 
QuestionC# web app / class library question Pin
circonian4-Oct-08 10:48
circonian4-Oct-08 10:48 
AnswerRe: C# web app / class library question Pin
Vimalsoft(Pty) Ltd4-Oct-08 23:38
professionalVimalsoft(Pty) Ltd4-Oct-08 23:38 
QuestionSpelling Control Pin
Bob X4-Oct-08 8:50
Bob X4-Oct-08 8:50 
QuestionConnecting to a c# Local Database Pin
Alex Grose4-Oct-08 3:11
Alex Grose4-Oct-08 3:11 
AnswerRe: Connecting to a c# Local Database Pin
TALHAKOSEN4-Oct-08 3:31
TALHAKOSEN4-Oct-08 3:31 
GeneralRe: Connecting to a c# Local Database Pin
Alex Grose4-Oct-08 3:46
Alex Grose4-Oct-08 3:46 
GeneralRe: Connecting to a c# Local Database Pin
Alex Grose4-Oct-08 4:23
Alex Grose4-Oct-08 4:23 
AnswerRe: Connecting to a c# Local Database Pin
leckey4-Oct-08 5:57
leckey4-Oct-08 5:57 
GeneralRe: Connecting to a c# Local Database Pin
Alex Grose4-Oct-08 6:01
Alex Grose4-Oct-08 6:01 
GeneralRe: Connecting to a c# Local Database Pin
Mark Salsbery4-Oct-08 7:42
Mark Salsbery4-Oct-08 7:42 
GeneralRe: Connecting to a c# Local Database Pin
Malcolm Smart4-Oct-08 7:45
Malcolm Smart4-Oct-08 7:45 
GeneralRe: Connecting to a c# Local Database Pin
Alex Grose4-Oct-08 8:58
Alex Grose4-Oct-08 8:58 
GeneralRe: Connecting to a c# Local Database Pin
Paul Conrad4-Oct-08 15:34
professionalPaul Conrad4-Oct-08 15:34 
AnswerRe: Connecting to a c# Local Database Pin
funklet4-Oct-08 8:49
funklet4-Oct-08 8:49 
AnswerRe: Connecting to a c# Local Database Pin
jzonthemtn5-Oct-08 3:42
jzonthemtn5-Oct-08 3:42 
QuestionFail to set port using SetMulticastGroup() of IMulticastConfig to "MPEG-2 Multicast Receiver" filter Pin
Andy Rama4-Oct-08 1:18
Andy Rama4-Oct-08 1:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.