将参数从UI(WinForms)传递到后台线程 [英] Pass parameters from UI (WinForms) to background thread

查看:83
本文介绍了将参数从UI(WinForms)传递到后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

假设我有一个创建后台线程并进行处理的课程。

我想改变例如 属性值 名为"EnableDebugMode"。

想法是在不中断线程执行的情况下进行更改。

Let's say I have a class which creates a background thread and do some processing.
I want to change for example a property value named "EnableDebugMode".
The idea is to make a change without interrupting thread execution.

线程内部有一堆if语句将一些调试信息写入UI控件。

There are a bunch of if statements inside thread that write some debug info to UI controls.

if (EnableDebugMode)
{
  // update UI
  syncContext.Send( ....)
}

这个工作正常。

但是现在我想从UI控制我的线程的执行。 (EnterDebugMode,将扫描速率从1000ms更改为2000ms,将新项添加到线程中的列表中,...)

But Now I want to control the execution of my thread from UI. (EnterDebugMode, change scan rate from 1000ms to 2000ms, add new item to the list in the thread,...)

除了制作我的类Thread的每个属性之外还有其他方法吗?通过使用"锁定"安全 - > "锁定"每个属性的对象?

Is there any other way than making each property of my class Thread-Safe by using "lock" -> "locking" object for each property?

谢谢

推荐答案

可能你有一个全局变量可以设置扫描速率(1000ms到2000ms),可以从你的线程函数访问。

Probably you can have a global variable to set Scan rate (1000ms to 2000ms) which can be accessed from your thread function.

你可以使用Monitor.Enter / Monitor.Exit函数或者用于锁定对象的互斥锁

You can use Monitor.Enter/Monitor.Exit function or Mutex to lock the object

http://msdn.microsoft。 com / zh-CN / library / de0542zz.aspx

http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx


这篇关于将参数从UI(WinForms)传递到后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆