110,539
社区成员
发帖
与我相关
我的任务
分享
private void SetActiveControl()
{
if (this.InvokeRequired)
{
this.Invoke(new Action(SetActiveControl));
}
else
{
bool CalcManualCode = chbCalcManualCode.Checked;
if (CalcManualCode) this.ActiveControl = dgvServiceDetail;//设置dataGridView
else this.ActiveControl = txtManualCode;//设置文本框焦点
}
}
TaskHelper.GetInstance.StartTask(() =>
{
//设置方法
CheckMemberCard(mcvlist, cardCode);
}, new FrmPrompt(this));
//异步设置具体实现
/// <summary>
/// 单例对象
/// </summary>
public static TaskHelper GetInstance
{
get
{
if (null == instance)
{
Interlocked.CompareExchange(ref instance, new TaskHelper(), null);
}
return instance;
}
}
/// <summary>
/// 启动异步任务
/// </summary>
/// <param name="taskAction">要异步执行的操作委托</param>
/// <param name="promptForm">异步执行中显示的提示窗口(null表示不显示)</param>
/// <returns></returns>
public Task StartTask(Action taskAction, Form promptForm)
{
return Task.Factory.StartNew(() =>
{
ShowPrompt(promptForm);
try
{
taskAction();
}
finally
{
ClosePrompt(promptForm);
}
});
}
/// <summary>
/// 弹出提示窗口
/// </summary>
/// <param name="promptForm">窗口对象</param>
public void ShowPrompt(Form promptForm)
{
if (promptForm == null || promptForm.Owner == null)
return;
Form ownerForm = promptForm.Owner;
if (!IsFormCreated(ownerForm))
return;
ownerForm.BeginInvoke(new Action(() =>
{
if (!promptForm.Visible)
promptForm.ShowDialog();
}));
}
private void CheckMemberCard(List<MemberService.MemberCardView> mcvlist, string whereText)
{
....
CheckMemberCard(mcvid);
}
private void CheckMemberCard(Guid mcid, bool isLoadBill = false)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<Guid, bool>(CheckMemberCard), mcid, isLoadBill);
}
else
{
//调用设置焦点方法
SetActiveControl();
}
}
Task.Run(() => {
//这里写你复杂的耗时的业务逻辑
}).ContinueWith(t =>
{
//这里设置控件焦点
this.Invoke(new Action(() => { this.textBox1.Focus(); }));
});
Task.Run(() => {
this.Invoke(new Action(() => { this.textBox1.Focus(); }));
}).ContinueWith(t => { this.Invoke(new Action(() => { this.textBox2.Focus(); })); });
最后的焦点在 this.textBox2 上
Task.Run(() => {
this.Invoke(new Action(() => { this.textBox2.Focus(); }));
}).ContinueWith(t => { this.Invoke(new Action(() => { this.textBox1.Focus(); })); });
最后的焦点在 this.textBox1 上.