Multi account chang login with multi -thread

 void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            isBussy = true;

             if (Common.isChangingAccount) {
                rt = new ResultInfo() { code = 3, isSucc = false, msg = "now system change 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 = "now system change account";
                    changeAccount();
                }

                System.Threading.Thread.Sleep(1000);
                return;
            }
}
 void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            lbLoadStatus.Content = "" + rt.code + "  " + rt.msg ;
            isBussy = false;
            worker = null;

        }


        void changeAccount() {

            Common.isChangingAccount = true;

          //  Common.AccountList[2].status = "" + DateTime.Now;
           AccountInfo acc=  Common.AccountList.FirstOrDefault(n => n.account == "wgscd2");
           if (acc != null) {
               acc.status = "send complete";
               acc.reachDailyMax = true;
           }
           acc = Common.AccountList.FirstOrDefault(n => n.reachDailyMax ==false && n.loginTimes < 4 );
           if (acc != null)
           {
               acc.status = "login failed";
               acc.loginTimes += 1;
               // BLL.Login(acc.account,acc.pwd );
               changeAccount();
           }
           else {
               rt.msg = "all account send complete";
               Common.stopFlag = true;
               return;
           }

            System.Threading.Thread.Sleep(5000);
            Common.isChangingAccount = false ;


        }







 public static  class Common
    {
       public static bool stopFlag = true;
       public static bool  isChangingAccount=false ;
       public static Random rnd = new Random();
       public static CookieContainer myCookieContainer = new CookieContainer();
       public static MsgType gMsgType = MsgType.sayHi;
       private static ObservableCollection <AccountInfo> _AccountList;
       public static ObservableCollection <AccountInfo> AccountList{
       get{
           if(_AccountList==null){
           _AccountList=  BLL.getAccountList();
           }
           return _AccountList;
           }
       set {
        _AccountList=value;
       
       }
     }



    }


   public enum MsgType {
     sayHi=0, chatMsg=1
   
   }

    public static class AppConfigData{
        /// <summary>
        /// 登录用户名
        /// </summary>
        public static string uid;
        /// <summary>
        /// 登录用户密码
        /// </summary>
        public static string pwd;
        /// <summary>
        /// 发送消息内容
        /// </summary>
        public static string msgContent;
        public static  MsgType  sendMsgType ;


    
    }




   public static class LoginUser {

     

       public static  string uid { get; set; }
       public static string pwd { get; set; }
       public static string sex { get; set; }



   }






 public class AccountInfo:INotifyPropertyChanged
    {
      public   event PropertyChangedEventHandler PropertyChanged;
      private string _account;
      public  string account {

          get { return _account; }

          set
          {
              if (value != _account)
              {
                  _account = value;
                  //when changed ,notice
                  NotifyChanged("account");
              }
          
          }
        }

      private string _pwd;
      public string pwd
      {

          get { return _pwd; }

          set
          {
              if (value != _pwd)
              {
                  _pwd = value;
                  //when changed ,notice
                  NotifyChanged("pwd");
              }

          }
      }


      private string _status;
      public string status
      {

          get { return _status; }

          set
          {
              if (value != _status)
              {
                  _status = value;
                  //when changed ,notice
                  NotifyChanged("status");
              }

          }
      }

      private bool  _reachDailyMax;
      public bool  reachDailyMax
      {

          get { return _reachDailyMax; }

          set
          {
              if (value != _reachDailyMax)
              {
                  _reachDailyMax = value;
                  //when changed ,notice
                  NotifyChanged("reachDailyMax");
              }

          }
      }

      private int  _loginTimes;
      public int loginTimes
      {

          get { return _loginTimes; }

          set
          {
              if (value != _loginTimes)
              {
                  _loginTimes = value;
                  //when changed ,notice
                  NotifyChanged("loginTimes");
              }

          }
      }







      public void NotifyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }




    }

  

Account window:

ui:

<Window x:Class="WzlyTool.AccountWind"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AccountWind" Height="293" Width="619" WindowStartupLocation="CenterScreen">
    <Grid>
        <ListView Name="listView"  BorderThickness="0"  Background="White"   Margin="0,0,0,0" ItemsSource="{Binding}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="listView_SelectionChanged" SelectionMode="Single">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="account"  Width="122" DisplayMemberBinding="{Binding account}"/>
                    <GridViewColumn Header="pws"  Width="88" DisplayMemberBinding="{Binding pwd}"/>
                    <GridViewColumn Header="status"  Width="158" DisplayMemberBinding="{Binding status}"/>
                    <GridViewColumn Header="try login Times"  Width="128" DisplayMemberBinding="{Binding loginTimes}"/>
                    
                </GridView>
                              
            </ListView.View>
        </ListView>
        <Button Content="Button" Height="35" HorizontalAlignment="Left" Margin="314,100,0,0" Name="button1" VerticalAlignment="Top" Width="133" Click="button1_Click" />
    </Grid>
</Window>

  

code:

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.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WzlyTool
{
    /// <summary>
    /// Interaction logic for AccountWind.xaml
    /// </summary>
    public partial class AccountWind : Window
    {
        public AccountWind()
        {
            InitializeComponent();
            listView.DataContext = Common.AccountList;
            listView.ItemsSource = Common.AccountList;
            Loaded += new RoutedEventHandler(AccountWind_Loaded);
        }

        void AccountWind_Loaded(object sender, RoutedEventArgs e)
        {
            listView.ItemsSource = Common.AccountList;

        }

        ObservableCollection<AccountInfo> listData = new ObservableCollection<AccountInfo>(); 
        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            Common.AccountList[2].status = "" + DateTime.Now;


        }







    }
}

  

then main window

 void worker_DoWork(object sender, DoWorkEventArgs e)
        {


            while (!Common. stopFlag)
            {


                System.Threading.Thread.Sleep(1000);

                while (Common.isChangingAccount) { 

                   System.Threading.Thread.Sleep(1000);
            
                 }

                Dispatcher.Invoke(new Action (() => { 
                
                    for (int i = 0; i < 10;i++ )
                    {

                    if (Common .stopFlag) return;
                    UserInfo ui = new UserInfo();
                    myWrapPanel.Children.Add(new MailMsg(ui,true));
                    myScrollViewer.ScrollToEnd();
                    lbCnt.Content = "" + myWrapPanel.Children.Count;

                      //  if(myWrapPanel.Children[i] as 
                    }
                  
                }));
         

            }



        }

  

原文地址:https://www.cnblogs.com/wgscd/p/9166821.html