some times we need pause thread to do some additional job:
c# thread pause example as below:
1. create a threadLoader usercontrol
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; using System.Net; using System.ComponentModel; namespace WzlyTool { /// <summary> /// Interaction logic for VideoDownLoader.xaml /// </summary> public partial class MailMsg : Grid { bool isBussy = true; public string StrFileName = ""; BackgroundWorker worker; public UserInfo ui; bool isTest; ResultInfo rt; public MailMsg() { InitializeComponent(); } public MailMsg(UserInfo ui,bool isTest) { this.ui = ui; this.isTest = isTest; InitializeComponent(); worker = new BackgroundWorker(); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.DoWork += new DoWorkEventHandler(worker_DoWork); if (!isTest) { BitmapImage b = new BitmapImage(new Uri(ui.picUrl, UriKind.RelativeOrAbsolute)); img.Source = b; } worker.RunWorkerAsync(); // bindData(); } void worker_DoWork(object sender, DoWorkEventArgs e) { isBussy = true; if (Common.isChangingAccount) { rt = new ResultInfo() { code = 3, isSucc = true, msg = "changing account........" }; return; } if (isTest) { rt = new ResultInfo() { code=1, isSucc =true, msg="OK" }; if (Common.rnd.Next(1, 9) >4) { rt.code = 3; rt. msg = "changing account........"; changeAccount(); } System.Threading.Thread.Sleep(1000); return; } else { if (Common.gMsgType == MsgType.chatMsg) { rt = BLL.SendMsg(ui.uid); } else { rt = BLL.SayHello(ui.uid); } if (rt.isSucc) { //save db rt.code = 1; rt.msg = "OK"; } } isBussy = false ; } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { lbLoadStatus.Content = "" + rt.code + " " + rt.msg ; isBussy = false; worker = null; } void changeAccount() { Common.isChangingAccount = true; System.Threading.Thread.Sleep(5000); Common.isChangingAccount = false ; } void testDownload() { WebClient wc = new WebClient(); byte[] b = wc.DownloadData("http://v11-tt.ixigua.com/d6daf13754d6d6250d442222be63e361/5b03c8a9/video/m/220c7dadd2606804a9eb1e88835c5f6165f1157546f0000ae2fd17a1f81/"); Stream fs = File.Create("D:\1.mp4"); fs.Write(b, 0, b.Length); // StreamWriter w = new StreamWriter(fs); // w.Write(b); fs.Flush(); fs.Close(); } } }
the data class:
public class UserInfo { public string picUrl {get;set; } public string uid { get; set; } public int age { get; set; } public string sex { get; set; } public string home { get; set; } public string nickname { get; set; } public string userDescinfo { get; set; } } public class ResultInfo { public int code { get; set; } public bool isSucc { get; set; } public string msg { get; set; } public object retObj { get; set; } }
public static class Common
{
public static bool isChangingAccount=false ;
public static Random rnd = new Random();
public static CookieContainer myCookieContainer = new CookieContainer();
public static MsgType gMsgType = MsgType.sayHi;
}
public enum MsgType {
sayHi=0, chatMsg=1
}
the main window to do:
BackgroundWorker worker; bool stopFlag = true ; public TestMainWind() { InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded); moreSearshBtnUI1.Hyperlink_Click += new EventHandler(moreSearshBtnUI1_Hyperlink_Click); worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.WorkerSupportsCancellation = true; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { btnStart.IsEnabled = true ; btnStart.Content = "satrt"; } void worker_DoWork(object sender, DoWorkEventArgs e) { while (!stopFlag) { System.Threading.Thread.Sleep(1000); while (Common.isChangingAccount) { System.Threading.Thread.Sleep(1000); } Dispatcher.Invoke(new Action (() => { for (int i = 0; i < myWrapPanel.Children.Count;i++ ) { // if(myWrapPanel.Children[i] as } UserInfo ui = new UserInfo(); myWrapPanel.Children.Add(new MailMsg(ui,true)); myScrollViewer.ScrollToEnd(); lbCnt.Content = "" + myWrapPanel.Children.Count; })); } }
the user control UI:
<Grid x:Class="WzlyTool.MailMsg" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Background="White" Name="gridRoot" Height="200" Width="200" Margin="2" RenderTransformOrigin="0.5,0.5" Opacity="0"> <Grid.RenderTransform> <ScaleTransform x:Name="t" ScaleX="1"/> </Grid.RenderTransform> <Grid.Triggers> <EventTrigger RoutedEvent="Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation From="0" To="1" Storyboard.TargetName="t" Storyboard.TargetProperty="ScaleX"></DoubleAnimation> <DoubleAnimation From="0" To="1" Storyboard.TargetName="t" Storyboard.TargetProperty="ScaleY"></DoubleAnimation> <DoubleAnimation From="0" To="1" Storyboard.TargetName="gridRoot" Storyboard.TargetProperty="Opacity"></DoubleAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Grid.Triggers> <Grid> <Rectangle Stroke="Gray" Fill="WhiteSmoke" StrokeThickness="2"></Rectangle> <Image Name="img" Stretch="UniformToFill"/> <Label Name="lbLoadStatus" Height="22"></Label> </Grid> </Grid>