Introduction
the demo with GIF animation ,multithreading,and use httpwatch to get the url of flv file
Background
c# wpf httpwatch
Points of Interest
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?
History
Keep a running update of any changes or improvements you've made here.
the demo includes:
1.gif animation in wpf
the first problem is how to show gif aninamation in wpf ?
here I use a library called "GifImageLib". :) this is a prefect work! it works fine !
2.multithreading in wpf
the multithreading in wpf called "Dispatcher" . After I searched in msdn.I wrote the code for multithreading.
Code
BackgroundWorker _backgroundWorker=null;
string flvUrl = string.Empty;
string strUrl = string.Empty;
public Window1()
{
InitializeComponent();
}
private delegate void DoWorkDelegate(int time);
private void btnGet_Click(object sender, RoutedEventArgs e)
{
/**/////first try , gif stoped
//imageLoad.Visibility = Visibility.Visible;
//new Thread(() =>
//{
// Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
// {
// Thread.Sleep(5000);
// imageLoad.Visibility = Visibility.Hidden;
// tbWait.Visibility = Visibility.Hidden;
// }));
//}).Start();
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork+=new DoWorkEventHandler(DoWork);
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FinsihWork);
BeforeWork();
_backgroundWorker.RunWorkerAsync();
_backgroundWorker.Dispose();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
//fake the process
for (int i = 0; i < 10; i ++)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
WorkProgress.Value = i*10;
}));
Thread.Sleep(100);
}
try
{
if(strUrl!=string.Empty)
parseUrl(strUrl);
}
catch
{
flvUrl="error";
}
}
private void BeforeWork()
{
imageLoad.Visibility = Visibility.Visible;
WorkProgress.Visibility = Visibility.Visible;
WorkProgress.Maximum = 100;
WorkProgress.Minimum = 0;
WorkProgress.Value = 0;
btnGet.IsEnabled = false;
strUrl = textBox1.Text;
}
private void FinsihWork(object sender,RunWorkerCompletedEventArgs e)
{
WorkProgress.Visibility = Visibility.Hidden;
imageLoad.Visibility = Visibility.Hidden;
btnGet.IsEnabled = true;
if (flvUrl != "error")
tbResult.Text = flvUrl;
}
this is BackgroundWorker demo.
_backgroundWorker.DoWork+=new DoWorkEventHandler(DoWork);
you can coding your work which needed long time.
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FinsihWork);
you can coding which you needed after the long time task.
3.use httpwatch to get the url of flv file
you can get the free httpwatch version from httpwatch.com . and you only need add com preference(httpwatch) to project
the code I using c#
Code
private bool UrlExistsUsingHttpWebRequest(string url)
{
try
{
System.Net.WebRequest.Create(url);
return true;
}
catch
{
return false;
}
}
private void tbResult_MouseDown(object sender, MouseButtonEventArgs e)
{
if (flvUrl != "error")
System.Diagnostics.Process.Start(flvUrl);
}
Ok,finished
中文版