靜網PWA視頻評論

關於Winform多線程方式登錄代碼

2024年01月05日

- txt下載

關於Winform多線程方式登錄代碼
為了方便大家學習相關的知識,YJBYS小編下面為你整理了關於Winform多線程方式登錄代碼,希望對你有所幫助。
bool restart = true;
while (restart)
{
Start(out restart);
}
(1):在主進程中不停的調用start.
void Start(out bool restart)
{
bool connected = false;
restart = false;
string[] userInfo = null;
try
{
//打開LoginInfoForm窗體,返回用戶名密碼。
userInfo = GetLoginInfo();// Splasher.GetLoginInfo();
if (userInfo == null)
{
loginOK = false;
return;
}
//啟動登陸後台線程。LoginProcessForm窗體啟動MySplashThread啟動不停的打開LoginProcessForm窗體。
Splasher.Show();
}
catch (Exception err)
{
DebugHelper.Write(err);
Splasher.ShowDialog("錄入登錄信息發生異常,請和管理員聯繫", MessageTipType.Error);
return;
}
eProcessState previousStepResult = eProcessState.Complete;
while (Splasher.StartExecute())
{
try
{
switch (Splasher.CurrentStep)
{
case eLoginState.__:
case eLoginState.__:
case eLoginState.__:
case eLoginState.__:
}
previousStepResult = eProcessState.Complete;
//結束執行
Splasher.EndExecute(previousStepResult);
}
catch (Exception error)
{
//略
}
}
}
//start中的對過程的控制。
static public void Show()
{
try
{
//當第二次調用的時候MySplashThread != null了這個時候
if (MySplashThread != null)
{
// public void Reset()
//{
// progressView.Reset();
//currentItemIndex = 0;
//}
//相當於觸發MySplashForm.Reset事件。
MySplashForm.Invoke(new MethodInvoker(MySplashForm.Reset));
//將窗體顯示出來。
MySplashForm.Invoke(new MethodInvoker(MySplashForm.Show));
return;
}
//當第一次的時候調用構造函數構造出窗體。構造出來以後myResetEvent.Set();通知
if (MySplashForm == null)
{
MySplashForm = new LoginProcessForm(myResetEvent);
}
//這裡只是每次都調用Application.Run(MySplashForm);作用相當於建立起信息循環隊列
MySplashThread = new Thread(new ThreadStart(Splasher.ShowThread));
MySplashThread.IsBackground = true;
MySplashThread.SetApartmentState(ApartmentState.STA);
MySplashThread.Start();
//等待直到myResetEvent.Set();
myResetEvent.WaitOne(5000);
}
catch (Exception err)
{
DebugHelper.Write(err);
}
}
//構造函數。
public LoginProcessForm(AutoResetEvent autoreset)
{
myResetEvent = autoreset;
Renderer = new SkinFormLoginFormRender();
InitializeComponent();
//this.TopMost = true;
//this.CloseBox = true;
CanMoveWindow = false;
labelVersion.Text = "Version:" + AssemblyInfoHelper.AssemblyFileVersion;
this.Load += new EventHandler(LoginProcessForm_Load);
}
delegate void EndExecuteCallback(eProcessState previousProcessResult);
static public void EndExecute(eProcessState previousProcessResult)
{
//winform中子線程不能更新主線程的`控制項。所以MySplashForm.InvokeRequired代表是否需要更新主線程控制項。
//如果需要更新的話MySplashForm.Invoke
if (MySplashForm.InvokeRequired)
{
//回調會繼續執行previousProcessResult,等下次的時候就會到下面去執行了。
MySplashForm.Invoke(new EndExecuteCallback(EndExecute), previousProcessResult);
}
else
{
//如果
MySplashForm.EndExecute(previousProcessResult);
}
}
///
/// 進入下一過程
///
///
///
public void EndExecute(eProcessState previousProcessResult)
{
//progressView為我們使用的用戶控制項
progressView.SetTip(processItems[currentItemIndex], previousProcessResult);
currentItemIndex++;
}
//關閉代碼
static public void Close()
{
try
{
if (MySplashThread == null) return;
if (MySplashForm == null) return;
try
{
MySplashForm.Invoke(new MethodInvoker(MySplashForm.Close));
}
catch (Exception err)
{
DebugHelper.Write(err);
}
MySplashThread.Abort();
}
catch (Exception err)
{
DebugHelper.Write(err);
}
finally
{
MySplashThread = null;
MySplashForm = null;
}
}

收藏

相關推薦

清純唯美圖片大全

字典網 - 試題庫 - 元問答 - 简体 - 頂部

Copyright © cnj8 All Rights Reserved.