I'm getting this Exception
System.InvalidOperationException was
unhandled by user code Message="The
calling thread cannot access this
object because a different thread owns
it."
whenever I run the following code
public partial class MainScreen : Window
{
Timer trm;
public MainScreen()
{
InitializeComponent();
trm = new Timer(1000);
trm.AutoReset = true;
trm.Start();
trm.Elapsed += new ElapsedEventHandler(trm_Elapsed);
}
void trm_Elapsed(object sender, ElapsedEventArgs e)
{
lblTime.Content = System.DateTime.Now;
}
}
guys any solution... I badly wann come out of it :(
Use DispatcherTimer instead:
public partial class MainScreen : Window{
DispatcherTimer tmr;
public MainScreen() {
InitializeComponent();
tmr = new DispatcherTimer();
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
}
void tmr_Tick(object sender, EventArgs e) {
lblTime.Content = System.DateTime.Now;
}
}
Any time you modify Windows controls you must do so on the UI thread (the one that created the controls).
See this question for lots of details.
To be short, you should use Dispatcher.Invoke method to update UI elements.
Related
I need my WPF exe to start monitoring a queue as soon as it starts up, and then respond to messages as they come in.
The way I have it now is:
public partial class App
{
readonly BackgroundWorker _worker = new BackgroundWorker();
protected override void OnStartup(StartupEventArgs e)
{
_worker.DoWork += worker_DoWork;
_worker.RunWorkerCompleted += worker_RunWorkerCompleted;
_worker.RunWorkerAsync();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_worker.DoWork += worker_DoWork;
_worker.RunWorkerAsync();
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
//do work
}
Obviously, this is pretty pathetic IMO.
Also, I don't like the approach of using while(true), inside the thread to keep it running indefinitely..
It's enough to read the messages from queue not permanently but periodically.
The common approach for this is using timer. For example, System.Threading.Timer.
public partial class App : Application
{
private System.Threading.Timer _msmqReadTimer;
public App()
{
_msmqReadTimer = new System.Threading.Timer(MsmqReadTimerTick);
_msmqReadTimer.Change(0, 1000); // call MsmqReadTimerTick immediatelly and each 1000 ms
}
private void MsmqReadTimerTick(object state)
{
// do work
// if you want to update some UI components after work, you should post this to UI thread dispatcher:
this.Dispatcher.Invoke(()=>
{
// logic for updating UI should be here
},
System.Windows.Threading.DispatcherPriority.Background);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Thread HeartRateThread = new Thread(startThread);
HeartRateThread.Name = "Class1";
HeartRateThread.Start();
}
private void startThread(object obj)
{
new Class1();
}
}
public class Class1
{
public Class1()
{
DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0,0,0,1);
timer1.Tick += timer1_tick;
timer1.Start();
}
private void timer1_tick(object sender, EventArgs e)
{
Debug.WriteLine("timer called");
}
}
I am trying to enable this timer_tick function fromanother thread as it is obvious in the code section of maInWindow. However, the Class1 constructor is called but timertick functin is not enabled. However if i do this on the main thread, everything works fine. Any reason for this.And how can I get it working?
DispatcherTimer can only work run on a UI thread. However, in your case you are creating a DispatcherTimer on a background thread. DispatcherTimer, internally tries to get Dispatcher.CurrentDispatcher, in your case it gets dispatcher for the background thread, not for the main UI thread.
Do you really need DispatcherTimer? If you are not going to manipulate any UI elements in the timer1_tick method, then you are better off to go with a different timer, like System.Timers.Timer.
Refer to this to read more about available Timers in .net.
Maybe you can try something like this:
private void timer1_tick(object sender, EventArgs e)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => Debug.WriteLine("timer called")));
}
Without having tested it, I guess you have to pass the MainWindow's Dispatcher to the DispatcherTimer on construction. Otherwise it will create its own:
private void startThread(object obj)
{
new Class1(Dispatcher);
}
...
public Class1(Dispatcher dispatcher)
{
DispatcherTimer timer1 =
new DispatcherTimer(DispatcherPriority.Background, dispatcher);
timer1.Interval = new TimeSpan(0,0,0,1);
timer1.Tick += timer1_tick;
timer1.Start();
}
you can use Dispatcher for call startThread method.
object objParameter = "parametervalue";
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(
() => startThread(objParameter)));
In my desktop application i want to open view in BackgroundWorker to show reminder.
but when i try to open view it gives me the following error.
"The calling thread must be STA, because many UI components require this."
and stackTrace is
at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Window..ctor()
at MahApps.Metro.Controls.MetroWindow..ctor()
can anyone have solution?
you can do this in this manner:
private BackgroundWorker _BgWorker;
public Window1()
{
InitializeComponent();
_BgWorker = new BackgroundWorker();
_BgWorker.DoWork += new DoWorkEventHandler(bgw_DoWork);
_BgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
///your code to get the data from database...
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//you code to open a view
}
Use this in the Runworker completed
Thread Messagethread = new Thread(
new ThreadStart(delegate()
{
DispatcherOperation DispacherOP = frmMassenger.Dispatcher
.BeginInvoke(DispatcherPriority.Normal, new Action(
delegate()
{
frmMassenger.Show();
}
));
} ));
Messagethread.Start();
later you join the Thread with main thread using:
Thread.join(userthread);
Hope This will help you.
I am creating window in another thread. After closing the thread some of the resources window is not released from the memory. Because of this growing counter GDI Objects and User Objects in windows task manager. Graphics that not released are font and region. I haven't idea what is going on...
public class WaitingWindowManager
{
private Thread thread;
private bool canAbortThread = false;
private Window waitingWindow;
public void BeginWaiting()
{
this.thread = new Thread(this.RunThread);
this.thread.IsBackground = true;
this.thread.SetApartmentState(ApartmentState.STA);
this.thread.Start();
}
public void EndWaiting()
{
if (this.waitingWindow != null)
{
this.waitingWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { this.waitingWindow.Close(); }));
while (!this.canAbortThread) { };
}
this.thread.Abort();
}
public void RunThread()
{
this.waitingWindow = new Window();
this.waitingWindow.Closed += new EventHandler(waitingWindow_Closed);
this.waitingWindow.ShowDialog();
}
void waitingWindow_Closed(object sender, EventArgs e)
{
this.canAbortThread = true;
}
}
And call :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WaitingWindowManager waitingWindowManager = new WaitingWindowManager();
waitingWindowManager.BeginWaiting();
Application.Current.Dispatcher.Thread.Join(5000);
waitingWindowManager.EndWaiting();
}
}
Remove your Closed eventhandler in your waitingWindow_Closed Event. It is causing your window to not be disposed. If you manually add events you need to make sure you remove them when finished.
I also noticed another Stackoverflow question that was pertaining to memory leaks in wpf. It referenced this article maybe this will help you.
Add Dispatcher.CurrentDispatcher.InvokeShutdown(); in your closing code. That should take care of any leaking memory.
Any ideas on how i get MVP working with Silverlight? How Do I get around the fact there is no load event raised?
This the view I have:
public partial class Person: IPersonView
{
public event RoutedEventHandler Loaded;
public Person()
{
new PersonPresenter(this);
InitializeComponent();
}
public Person Person
{
set { Person.ItemsSource = value; }
}
}
And my presenter:
public class PersonPresenter
{
private readonly IPersonView _view;
private readonly ServiceContractClient _client;
public PersonPresenter(IPersonView view)
{
_client = new ServiceContractClient();
_view = view;
WireUpEvents();
}
private void WireUpEvents()
{
_view.Loaded += Load;
}
private void Load(object sender, EventArgs e)
{
_client.GetPersonCompleted += Client_GetPerson;
_client.GetPersonAsync();
}
private void Client_GetPerson(object sender, GetPersonCompletedEventArgs e)
{
_view.Person= e.Result;
}
}
Nothing happened for me as the Loaded event dont seem to get called, how do i get around this?
Tim Ross has a good introduction to Silverlight MVP implementation, with source code.
I believe the loaded event gets called when the control has been initialized, loaded, rendered and ready for use. This means that as long as you don't place it inside a visible container (so that it is rendered), the loaded event won't be risen.
You may consider using MVC# - a Model View Presenter framework with Silverlight 2.0 support.
Oleg Zhukov