1
我实现了一个简单的ProgressPresenterMVP异步进步的WinForms
public interface IProgressView
{
string Status { set; }
void SetProgress(int percentageDone);
void Display();
void Close();
event Action Closing;
}
class ProgressPresenter
{
private IProgressView m_view;
private ILongRunningTask m_task;
private bool m_completed;
public Progress(IProgressView view)
{
m_view = view;
}
public virtual void Display(ILongRunningTask task, string taskName)
{
m_task = task;
m_view.Status = taskName " is running";
m_view.Closing += OnClosing;
task.ProgressChanged += UpdateProgress;
task.Completed += Completed;
task.StartAsync();
m_view.Display();
m_view.Closing -= OnClosing;
task.ProgressChanged -= UpdateProgress;
task.Completed -= Completed;
}
protected virtual void UpdateProgress(object sender, ProgessEventArgs e)
{
m_view.SetProgress(e.AlreadyDone * 100/e.Total);
}
protected virtual void Completed()
{
m_completed = true;
m_view.Status = "Completed";
m_view.Close();
}
private virtual void OnClosing()
{
if (!m_completed) m_downloader.Cancel();
}
}
我的问题是,任务是在另一个线程运行并调用视图(作为Form
实现)抛出。我是否应该将每种方法都包装在表单中,例如
public string Status
{
set { Invoke(new Action(() => progressLabel.Text = value)); }
}
以防万一它可以从另一个线程中调用?或者Presenter有缺陷?
任何建议表示赞赏
THX的答案的一个例子。这两个选项都非常有用。 ATM我不想要一个真正的国际奥委会依赖,所以我推出了我自己的非常简单,因此我在第二个选项现在 – Firo
您可以使用Postharp作为代理类方面的替代! –