//static int var1, var2, var3;
//static List<string> list;
public static void MyMethod(Object obj, int times)
{
List = doc1.Descendants("Order")
.Select(d => d.Value)
.ToList();
for (int i = 0; i < List.Count; i++)
{
try
{
var item = (from m in doc1.Elements("list").Elements("Order")
where m.Value == List[i]
select m);
item.Remove();
doc1.Save(path1);
var1 = doc1.Root.Descendants("Order").Count();
//DoSomethingHeavy with List[i]
doc2.Element("list2").Add(new XElement("Order", List[i]));
doc2.Save(path2);
var2= doc2.Root.Descendants("Order").Count();
}
catch (Exception)
{
doc3.Element("list3").Add(new XElement("Order", List[i]));
doc3.Save(path3);
var3 = doc3.Root.Descendants("Order").Count();
}
}
}
이것은 공용 정적 클래스의 MyMethod입니다. 실행을 일시 중지했다가 다시 시작하려면 BackgroundWorker에서 양식을 실행하고 싶습니다. myForm을에,trygrounderWorker가 try-catch 블록에서 중지 중
public class BackgroundLoading
{
public BackgroundWorker Bw;
public delegate void RunFunction();
public RunFunction thisFunction;
public BackgroundLoading(RunFunction newFunction)
{
thisFunction = newFunction;
Bw = new BackgroundWorker();
Bw.DoWork += new DoWorkEventHandler(Bw_DoWork);
Bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Bw_RunWorkerCompleted);
}
public void Start()
{
Bw.RunWorkerAsync();
}
void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
//an error occured
MessageBox.Show("an error occured: " + e.Error.Message);
}
if (e.Cancelled)
{
//an error occured
MessageBox.Show("Job cancelled");
}
else
{
MessageBox.Show("Job completed");
}
}
void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MessageBox.Show(" *");
}
void Bw_DoWork(object sender, DoWorkEventArgs e)
{
if (thisFunction != null)
thisFunction();
}
}
//class to implement
다음 :
나는이 클래스를 사용하고
private void button1_Click(object sender, EventArgs e)
{
BackgroundLoading bl = new BackgroundLoading(MyMethod());
bl.Start();
}
나는 BackgroundWorker에 정말 새로운 해요 아마 나는 그것을 올바른 방법으로 사용하지 않는. StopExecution 버튼을 클릭하면 모든 내 문서 (doc1, doc2, doc3)가 저장되지 않습니다. 그래서 try-catch 블록에서 멈추는 것을 피할 수 있습니까?
어떻게 * 정지 * 실행 요구를 처리하나요? – Tigran
이렇게하면 개인 무효 btnStop_Click (개체 보낸 사람, EventArgs 전자) { if (backgroundWorker.WorkerSupportsCancellation == true) { backgroundWorker.CancelAsync(); } } – Barbara
Bgw는 일시 중지/다시 시작에 적합하지 않습니다. 취소하려면 'MyMethod()'에 플래그를 표시해야합니다. –