dispatcher

Using the C# Dispatcher

别等时光非礼了梦想. 提交于 2019-11-27 00:28:36
I'm building a chat client and am not 100% sure on how to use the dispatcher . So the question is I have a method as such: public void LostConnection() { myGUI.chatBox.AppendText("Lost connection to room: "+ myGUI.UsernameText.ToString() + "\r\n"); } Do i need to surrond the statement within (myGUI.chatBox... ) with a Dispatcher.Invoke ? I appreciate any help. Ricibob Your app has a main UI thread (usually ManagedThreadId==1 ). Typically in a chat app your events will come in on other threads (either dedicated socket listen threads or thread pool threads from listening code). If you want to

c# - Volatile keyword usage vs lock

血红的双手。 提交于 2019-11-27 00:09:49
I've used volatile where I'm not sure it is necessary. I was pretty sure a lock would be overkill in my situation. Reading this thread (Eric Lippert comment) make me anxious on my usage of volatile: When should the volatile keyword be used in c# ? I used volatile because my variable is use in a Multithreaded context where this variable could be accessed/modified concurrently, but where I can loose an addition without any hurts (see code). I added "volatile" to make sure that there is no miss alignment occurring: reading only 32 bits of the variable and the other 32 bits on another fetch which

How to pass the UI Dispatcher to the ViewModel

情到浓时终转凉″ 提交于 2019-11-26 21:26:47
I'm supposed to be able to access the Dispatcher that belongs to the View I need to pass it to the ViewModel. But the View should not know anything about the ViewModel, so how do you pass it? Introduce an interface or instead of passing it to the instances create a global dispatcher singleton that will be written by the View? How do you solve this in your MVVM applications and frameworks? EDIT: Note that since my ViewModels might be created in background threads I can't just do Dispatcher.Current in the constructor of the ViewModel. Matthias I have abstracted the Dispatcher using an interface

Using the WPF Dispatcher in unit tests

拜拜、爱过 提交于 2019-11-26 18:35:51
I'm having trouble getting the Dispatcher to run a delegate I'm passing to it when unit testing. Everything works fine when I'm running the program, but, during a unit test the following code will not run: this.Dispatcher.BeginInvoke(new ThreadStart(delegate { this.Users.Clear(); foreach (User user in e.Results) { this.Users.Add(user); } }), DispatcherPriority.Normal, null); I have this code in my viewmodel base class to get a Dispatcher: if (Application.Current != null) { this.Dispatcher = Application.Current.Dispatcher; } else { this.Dispatcher = Dispatcher.CurrentDispatcher; } Is there

“The calling thread must be STA, because many UI components require this” error when creating a WPF pop-up Window in thread

最后都变了- 提交于 2019-11-26 18:28:56
问题 I have a WPF application in which a thread checks some value. In certain cases, I show a pop-up Window in order to display a message. When I create this pop-up window in the thread, an exception is thrown by the pop-up window's constructor: "The calling thread must be STA, because many UI components require this." How do I resolve this error? This is my code for creating the pop-up window: // using System.Threading; // using System.Windows.Threading; Thread Messagethread = new Thread(new

How to put delay before doing an operation in WPF

别等时光非礼了梦想. 提交于 2019-11-26 16:34:36
I tried to use the below code to make a 2 second delay before navigating to the next window. But the thread is invoking first and the textblock gets displayed for a microsecond and landed into the next page. I heard a dispatcher would do that. Here is my snippet: tbkLabel.Text = "two mins delay"; Thread.Sleep(2000); Page2 _page2 = new Page2(); _page2.Show(); Phil The call to Thread.Sleep is blocking the UI thread. You need to wait asynchronously. Method 1: use a DispatcherTimer tbkLabel.Text = "two seconds delay"; var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; timer

Ensuring that things run on the UI thread in WPF

喜夏-厌秋 提交于 2019-11-26 15:32:20
问题 I'm building a WPF application. I'm doing some async communication with the server side, and I use event aggregation with Prism on the client. Both these things results in new threads to be spawned which are not the UI thread. If I attempt to do "WPF operations" on these callback and event handler threads the world will fall apart, which it now has started doing. First I met problems trying to create some WPF objects in the callback from server. I was told that the thread needed to run in STA

Cannot convert lambda expression to type 'System.Delegate'

ぃ、小莉子 提交于 2019-11-26 13:55:24
问题 Neither of these work: _uiDispatcher.Invoke(() => { }); _uiDispatcher.Invoke(delegate() { }); All I want to do is Invoke an inline method on my main UI thread. So I called this on the main thread: _uiDispatcher = Dispatcher.CurrentDispatcher; And now I want to execute some code on that thread from another thread. How do I do it? Am I using the wrong syntax? Note that this is not a WPF application; I've referenced WindowsBase so I could get access to the Dispatcher class. 回答1: The problem is

Dispatcher Invoke(…) vs BeginInvoke(…) confusion

假装没事ソ 提交于 2019-11-26 12:58:07
问题 I\'m confused why I can\'t make this test counter application work with 2 (or more) simultaneous running countertextboxes with the use of \"BeginInvoke\" on my Dispatcher in the Count() method. You can solve the issue by replacing the BeginInvoke by an Invoke. But this doesn\'t solve my confusion. Here\'s the sample code I\'m talking about: public class CounterTextBox : TextBox { private int _number; public void Start() { (new Action(Count)).BeginInvoke(null, null); } private void Count() {

Event system in Python

大城市里の小女人 提交于 2019-11-26 12:35:16
What event system for Python do you use? I'm already aware of pydispatcher , but I was wondering what else can be found, or is commonly used? I'm not interested in event managers that are part of large frameworks, I'd rather use a small bare-bones solution that I can easily extend. Wrapping up the various event systems that are mentioned in the answers here: The most basic style of event system is the 'bag of handler methods', which is a simple implementation of the Observer pattern . Basically, the handler methods (callables) are stored in an array and are each called when the event 'fires'.