c# - How to create Image in Timer method? -


for example. have timer , i'm initializing in window constructor.

public mainwindow() {     initializecomponent();     checktimer = new timer(3000);     checktimer.elapsed += checktimer_elapsed;     checktimer.start(); } 

and checktimer_elapsed method:

void checktimer_elapsed(object sender, elapsedeventargs e) {     messagebox.show("firsttext");     image img = new image();     messagebox.show("secondtext"); } 

why loading first messagebox window? but, if function called not timer ok.

public mainwindow() {     initializecomponent();     somefunc(); } void somefunc() {     messagebox.show("firsttext");     image img = new image();     messagebox.show("secondtext"); } 

i believe threading problem.

if recall event getting called timer on new thread. trying ui work not on ui thread (which not allowed). couple of solutions this, simplest of use dispatchertimer instead of timer. can find in system.windows.threading;

the event gets called dispatchertimer on ui thread

hope helps


Comments