Tag: multithreading

SystemEvents.SessionEnding在Process(之前打开)关闭之前不会被触发

当用户关闭会话时,我正试图在程序中做一些事情。 这是代码: using System; using System.Diagnostics; using Microsoft.Win32; using System.Windows.Forms; using System.Threading; public class MyProgram { static Process myProcess = null; public MyProgram() { } // Entry point static void Main(string[] args) { SystemEvents.SessionEnding += SessionEndingEvent; // Does not trigger inmediately, only fires after “myProcess” gets closed/killed myProcess = CreateProcess(“notepad.exe”, null); myProcess.Exited += pr_Exited; // […]

在C#console应用程序中更改线程上下文

我有一个C#控制台应用程序,其中我可以通过TCP套接字连接获得输入。 当我通过套接字通过接收function接收输入时,如何切换到主线程? 在WPF中类似于这样的东西: public void TaskDispatcher() { if (DispatcherObjectForTaskDispatcher.Thread != System.Threading.Thread.CurrentThread) DispatcherObjectForTaskDispatcher.Invoke(new TaskDispatcherDelegate(TaskDispatcher)); else { // Do some thing in the UI thread } }

UI线程阻止调用COM对象的后台线程

我正在开发一个通过第三方COM库与外部设备通信的应用程序。 我试图让所有与设备的通信都通过后台线程来防止通信问题搞砸我的应用程序并摆脱通过在UI线程中进行通信而引入的一些其他复杂性。 问题是每当发生导致主UI线程阻塞的事情(即MessageBox.Show被调用或甚至只是在屏幕上移动窗口)时,与后台线程上的设备的通信也会停止。 是否有任何方法(缺少完全独立的过程)将两个线程分开足够远以至于它们不会相互干扰? (注意,完全相同的代码与一些数学计算,以减慢一点工作得很好,只有当我使用COM库我有问题)

该线程已退出Windows窗体中的代码0

我在Windows窗体中退出线程时遇到问题。 我有经典的Windows窗体,它正在运行。 我需要在每个时间段做一些事情,所以我补充说: TimerCallback timerDelegate = new TimerCallback(this.TryDoSomething); int period = 10 * 1000; // to miliseconds System.Threading.Timer stateTimer = new System.Threading.Timer(timerDelegate, null, period, period); 方法DoSomething由几个线程(主线程和这个计时器)调用,所以我这样覆盖它: private void TryDoSomething(object o) { lock (tryDoSomethingMutex) { if (this.dataGridView1.InvokeRequired) { RefreshCallback d = new RefreshCallback(DoSomething); this.Invoke(d, new object[] { o }); } else { this.DoSomething(o); } } } […]

暂停新的BackGroundWorker直到上一次完成

我正在努力进行线程化。 问题是当我通过foreach循环迭代时。 设置this.Document ,应用程序执行登录,该事件由事件触发并需要几秒钟才能完成。 在worker_RunWorkerCompleted方法中,我需要执行一些依赖于当前登录信息的操作。 问题是,在我可以对第一个文件执行此操作之前, this.Document已经更改,使应用程序执行另一次登录。 这样我就无法实际执行我的操作。 我的问题是:如何在上一个线程完成之前暂停下一个线程。 我的问题还有其他解决办法吗? 我尝试使用AutoResetEvent但我没有运气。 我在RunWorkerAsync调用之后设置了waitOne() ,并在RunWorkerCompleted中设置了.Set() 。 代码永远不会到达RunWorkerCompleted …… 这是代码: public void Start(object obj) { try { foreach (KeyValuePair pair in this.CollectionOfFiles) { Worker = new BackgroundWorker(); Worker.DoWork += new DoWorkEventHandler(worker_DoWork); Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); using (Stream stream = pair.Value) { primaryDocument = new Document(stream); DataHolderClass dataHolder = […]

使用Invoke进行父窗体或目标控件之间有什么区别吗?

如果我想从另一个线程处理控件,检查InvokeRequired和调用控件的Invoke方法和父窗体的区别是什么? if (theForm.InvokeRequired) Invoke(…) 要么 if (myControl.InvokeRequired) myControl.Invoke(…) 来自MSDN : Invoke方法搜索控件的父链,直到找到具有窗口句柄的控件或窗体(如果当前控件的基础窗口句柄尚不存在)。

在C#中,如果程序不断输出,您如何从DOS程序中读取?

我找到了很多关于如何执行cmd.exe并执行命令的编码示例,甚至执行nslookup和交互,但我遇到的问题是使用特定的dos程序,当它启动时,它不会停止“输出” 。 这里有一些代码,我会发表评论和我从C#得到的错误 这是我如何以更高级的方式设置它,以便我可以从事件的程序接收输出 public void StartApplication(string appNameAndPath) { StreamReader outputStream; Process p = new Process(); p.StartInfo.FileName = appNameAndPath; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = false;//for now just so I can see it p.Start(); //here is my advanced example if(advanced == true) { outputStream = p.StandardOutput; DoReadOutPut(); } else {//here is a simple […]

实现跨线程调用检查

我需要检查一个方法是从实例化类的同一个线程调用的,类似于WinForms控件实现的function。 怎么能实现这个? 以下示例是否有效? public class Foo { int ManagedThreadId; public Foo() { ManagedThreadId=Thread.CurrentThread.ManagedThreadId; } public void FooMethod() { if (ManagedThreadId!=Thread.CurrentThread.ManagedThreadId) throw new InvalidOperationException(“Foo accessed from a thread other than the thread it was created on.”); //Method code here. } } 我不太确定存储ManagedThreadId就足以实现这一点。 如果我存储对线程的引用,这是否会给GC或其他东西带来问题?

在调用`Invoke`时避免`ObjectDisposedException`

我有2个表单,一个是MainForm ,第二个是DebugForm 。 MainForm有一个按钮,可以像这样设置和显示DebugForm,并传递对已经打开的SerialPort的引用: private DebugForm DebugForm; //Field private void menuToolsDebugger_Click(object sender, EventArgs e) { if (DebugForm != null) { DebugForm.BringToFront(); return; } DebugForm = new DebugForm(Connection); DebugForm.Closed += delegate { WindowState = FormWindowState.Normal; DebugForm = null; }; DebugForm.Show(); } 在DebugForm中,我附加了一个方法来处理serialport连接的DataReceived事件(在DebugForm的构造函数中): public DebugForm(SerialPort connection) { InitializeComponent(); Connection = connection; Connection.DataReceived += Connection_DataReceived; } 然后在Connection_DataReceived方法中,我更新DebugForm中的TextBox,即使用Invoke进行更新: private […]

C# – System.Windows.Forms.Clipboard.GetDataObject()没有响应

有没有人知道为什么System.Windows.Forms.Clipboard.GetDataObject()在从主线程中调用System.Windows.Forms.Clipboard.Clear()之后从另一个线程调用它并且在主线程停止之前没有返回? 我写了一个示例程序来解释我的问题: public class ClipboardDemo { [STAThread] public static void Main(string[] args) { Thread.CurrentThread.Name = “MAIN_THREAD”; Thread clipboardViewerThread = new Thread(RunClipboardViewer); clipboardViewerThread.Name = “CLIPBOARD_VIEWER_THREAD”; clipboardViewerThread.SetApartmentState(ApartmentState.STA); Thread clipboardClearerThread = new Thread(RunClipboardClearer); clipboardClearerThread.Name = “CLIPBOARD_CLEARER_THREAD”; clipboardClearerThread.SetApartmentState(ApartmentState.STA); Console.WriteLine(“Starting ” + clipboardViewerThread.Name + “, expecting initial WM_DRAWCLIPBOARD message…”); clipboardViewerThread.Start(); Thread.Sleep(1000); Console.WriteLine(“Clearing clipboard from ” + clipboardClearerThread.Name + “, expecting […]