.net 从类开始停止线程

t5zmwmid  于 2023-11-20  发布在  .NET
关注(0)|答案(2)|浏览(179)

我想启动/停止一个线程,以便不阻塞UI使用按钮

  1. public partial class Program_Form : Form
  2. {
  3. readonly BackgroundWorker m_oWorker;
  4. [STAThread]
  5. private void Program_Form_Load(object sender, EventArgs e)
  6. {
  7. // long code here
  8. }
  9. private async void DGW6BtnPrint_Click(object sender, EventArgs e)
  10. {
  11. Work.Printer_ Print = new Work.Printer_();
  12. await Task.Run(() =>
  13. {
  14. Print.Print_File(this, dataGridView6, StatusText, progressBar1,
  15. variablesStatus);
  16. });
  17. }
  18. public void BTN6PPauza_Click(object sender, EventArgs e)
  19. {
  20. //What i had tried
  21. //_canceller.Dispose();
  22. //_canceller.Cancel();
  23. // variablesStatus = false;
  24. //thread2.break;
  25. //autoResetEvent.WaitOne();
  26. //thread2.Join();
  27. //_manualResetEvent.Reset();
  28. //thread2.Abort();
  29. //_pauseEvent.Reset();
  30. //variablesStatus = "Pause";
  31. //Print_Actions();
  32. }
  33. }

字符串
引用类:

  1. namespace OfficeTools.Work
  2. {
  3. class Printer_
  4. {
  5. public void Print_File(Program_Form callForm, DataGridView DGW,
  6. TextBox Status, ProgressBar Progress, bool variablesStatus)
  7. {
  8. foreach (DataGridViewRow Row in DGW.Rows)
  9. {
  10. file = DGW.Rows[Row.Index].Cells[4].Value.ToString();
  11. PrintFiles.Print_Word(file);
  12. }
  13. }
  14. }
  15. }


我怎么能开始停止暂停恢复线程,因为没有工作,从我已经尝试过,我认为问题是从foreach循环我从来没有使用过线程,我找不到一个例子类似于我的,以了解我应该怎么做。

lndjwyie

lndjwyie1#

我做了这个工作,我不知道这是否是正确的方式,但目前它的工作

  1. public partial class Program_Form : Form
  2. {
  3. readonly BackgroundWorker m_oWorker;
  4. CancellationTokenSource _tokenSource = null;
  5. [STAThread]
  6. private void Program_Form_Load(object sender, EventArgs e)
  7. {
  8. // long code here
  9. }
  10. private async void DGW6BtnPrint_Click(object sender, EventArgs e)
  11. {
  12. _tokenSource = new CancellationTokenSource();
  13. var token = _tokenSource.Token;
  14. Work.Printer_ Print = new Work.Printer_();
  15. await Task.Run(() =>
  16. {
  17. Print.Print_File(this, dataGridView6, StatusText, progressBar1, token);
  18. });
  19. }
  20. public void BTN6PPauza_Click(object sender, EventArgs e)
  21. {
  22. _tokenSource.Cancel();
  23. }
  24. }

字符串
引用类:

  1. namespace OfficeTools.Work
  2. {
  3. class Printer_
  4. {
  5. public void Print_File(Program_Form callForm, DataGridView DGW, TextBox Status, ProgressBar Progress, CancellationToken Token)
  6. {
  7. foreach (DataGridViewRow Row in DGW.Rows)
  8. {
  9. file = DGW.Rows[Row.Index].Cells[4].Value.ToString();
  10. PrintFiles.Print_Word(file);
  11. if (Token.IsCancellationRequested)
  12. {
  13. try
  14. {
  15. Winword.Quit(ref missing, ref missing, ref missing);
  16. winword = null;
  17. }
  18. catch { }
  19. return;
  20. }
  21. }
  22. }
  23. }
  24. }


亲切问候所有

展开查看全部
kadbb459

kadbb4592#

你的问题暗示你想使用Thread.SuspendThread.Resume方法。可能像这样:

  1. private volatile Thread _printThread;
  2. private async void DGW6BtnPrint_Click(object sender, EventArgs e)
  3. {
  4. Work.Printer_ Print = new Work.Printer_();
  5. await Task.Run(() =>
  6. {
  7. _printThread = Thread.CurrentThread;
  8. try
  9. {
  10. Print.Print_File(this, dataGridView6, StatusText, progressBar1,
  11. variablesStatus);
  12. }
  13. finally { _printThread = null; }
  14. });
  15. }
  16. public void BTN6PPauza_Click(object sender, EventArgs e)
  17. {
  18. var printThread = _printThread;
  19. if (printThread != null)
  20. {
  21. if (printThread.ThreadState.HasFlag(ThreadState.Running))
  22. {
  23. printThread.Suspend();
  24. }
  25. else if (printThread.ThreadState.HasFlag(ThreadState.Suspended))
  26. {
  27. printThread.Resume();
  28. }
  29. }
  30. }

字符串
这两种方法的文档包括几个不鼓励使用的警告:
Thread.Suspend已被弃用。使用System.Threading中的其他类,如MonitorMutexEventSemaphore,以同步线程或保护资源。
不要使用SuspendResume方法来同步线程的活动。挂起线程时,您无法知道线程正在执行什么代码。如果在安全权限评估期间挂起持有锁的线程,则AppDomain中的其他线程可能会被阻塞。如果在线程执行类构造函数时挂起线程,AppDomain中的其他线程试图使用该类时会被阻塞。2死锁很容易发生。
如果你想接受这些风险,那由你决定。如果你问我,你不应该。

注意:.NET Core和更高版本的平台不支持SuspendResume方法。在这些平台上,它们会引发PlatformNotSupportedException异常。仅当您针对.NET Framework平台时才可以使用它们。

展开查看全部

相关问题