0

here is my thread in asp.net web page (code behind) :

Thread my_thread = new Thread(delegate()
{
    my_thread_method(params);
});

Timer1.Enabled = true;

my_thread.Start();

Now I want to access Timer1 in my_thread() and work with that timer.
How can i do that?
I also need to update some labels text inside thread, but I don't have access to them.
What is the solution?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Duplicate of: http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – Sjips Nov 10 '14 at 09:32
  • Because Your UI thread and Threads those you have created run on different Contexts – Dgan Nov 10 '14 at 09:34
  • @Sjips thanks for the link. but i am talking about web pages, not winforms. your link is about winforms. – SilverLight Nov 10 '14 at 10:12

2 Answers2

-1

my_thread_method needs to be defined in the same class as the controls in order for you to access them.

You will then need to read this sort of thing to call UI classes correctly from the non-UI thread:

How to update the GUI from another thread in C#?

EDIT
this answer is not related to asp.net web forms.
so we can not accept this.

Community
  • 1
  • 1
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • thanks for the answer. not winforms. please check my tags and edit your answer... – SilverLight Nov 10 '14 at 10:13
  • Oh - so in that case I would ask why on earth are you using a timer in an ASP.NET app? It is not like you can give back control to the UI then update it later in the same way you can a windows App. – Justin Harvey Nov 10 '14 at 10:21
  • i know that man. this is a test and i did it very nice. but i need access to my outside controls inside thread. i really want to learn that and i still could n't find an answer for it. please edit your answer... – SilverLight Nov 10 '14 at 10:26
  • please delete your answer and let people work on that. – SilverLight Nov 10 '14 at 19:17
  • It is very hard to answer your question as you give no context. You need to show where my_thread_method is defined in relation to the code you show, then maybe we would have a chance. – Justin Harvey Nov 12 '14 at 10:53
  • Your answer is not accepted, but i should end this thread. thanks for the attentions... – SilverLight Dec 04 '14 at 14:13
-1

For update controls in Thread, you have to use invoke method.

Go to : C# Threading using invoke, freezing the form

Community
  • 1
  • 1
Crumbleszatan
  • 41
  • 1
  • 4