|
Great Scott!
1.
Scott Dorman wrote: There really isn't a direct link for this. It comes from reading the MSDN documentation (more correctly, reading "between the lines") and experience.
That's fine. Could you post the link please so that I can also have a learn?
2.
Could I understand "internal" is used to restrict access only to the current assembly (e.g. other assembly can not reference this "internal" type)?
regards,
George
|
|
|
|
|
George_George wrote: Could you post the link please so that I can also have a learn?
Sure, the link is: http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx[^]
George_George wrote: Could I understand "internal" is used to restrict access only to the current assembly (e.g. other assembly can not reference this "internal" type)?
Yes, internal restricts access to only the current assembly.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines][ Articles][ Blog]
|
|
|
|
|
Great Scott!
What means "When overridden in a derived class, searches for the fields defined for the current Type" in the document? I am especially confused about what means "overridden in a derived class" and "defined for the current Type". Could you show me some pseudo code please?
regards,
George
|
|
|
|
|
You told it you wanted fields that are neither Public nor NonPublic; you should specify at least one.
Same with Instance and Static I presume.
|
|
|
|
|
Thanks PIEBALDconsult!
1.
Sorry my English is not very good.
You mean the behavior of using Instance tag alone is -- "wanted fields that are neither Public nor NonPublic"?
2.
If the answer to 1 is yes, then I think it is useless to use the Instance tag alone, right?
regards,
George
|
|
|
|
|
|
|
Those links don't work right for me.
|
|
|
|
|
Hi PIEBALDconsult,
The link is,
http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx[^]
Question is,
--------------------
What means "When overridden in a derived class, searches for the fields defined for the current Type" in the document? I am especially confused about what means "overridden in a derived class" and "defined for the current Type". Could you show me some pseudo code please?
--------------------
regards,
George
|
|
|
|
|
George_George wrote: what means "overridden in a derived class"
Ignore that.
George_George wrote: "defined for the current Type".
Fields in the type.
|
|
|
|
|
Thanks PIEBALDconsult,
It is great!
regards,
George
|
|
|
|
|
Hello everyone,
Here is my code segment for a thread, which will sleep one second, do some work and check if the stop status is set -- if set, then break the loop and stops the method.
My question is, whether it is possible to use event (e.g. ManualResetEvent) to implement similar logics?
void ThreadMethod1()
{
while (true)
{
Thread.Sleep(1000);
if (stop)
{
}
else
{
}
}
}
thanks in advance,
George
|
|
|
|
|
You could use a semaphore and wait for it to unblock.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
I think in this way, my thread will be forever blocked until the signal is set, right? But my purpose is to do some work at 1 second interval and checks for stop at the same interval -- if the stop signal is not coming, other than blocking forever, I want the thread to continue to do its work.
Any comments?
regards,
George
|
|
|
|
|
Well, then you're on the right track with a timer.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
1.
Do you mean using Timer to check stop status or to do the regular work?
2.
If you mean using Timer to do the regular work, I think the Timer class is used to perform a task in a regular interval no matter whether or not the previous task is completed or not. So, it is possible to execute two tasks at the same time if the 1st task is not completed in 1 second for some reason. Right?
How to avoid such situation?
regards,
George
|
|
|
|
|
1 - if you want to execute code regularly, use a timer
2 - Your timer is not going to fire again if that thread is busy. So, it's not going to run your code twice at the same time, it's single threaded. If your code takes 30 seconds to run and you have a 20 second timer, the timer will just fire late.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
You mean using timer and in the timer execution method, we do two work items -- checks both the stop status and do the regular work?
regards,
George
|
|
|
|
|
Yeah, I guess so.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
The timer solution works. A further question, what are the pros and cons compared with using sleep and using timer from functional and performance perspective?
regards,
George
|
|
|
|
|
Christian Graus wrote: it's not going to run your code twice at the same time
I find that not to be the case (with a System.Timers.Timer anyway, other Timers may differ).
private System.Timers.Timer tt = new System.Timers.Timer ( 1000 ) ;
public Template
(
)
{
tt.Elapsed += F ;
tt.Start() ;
return ;
}
private void
F
(
object sender
,
System.Timers.ElapsedEventArgs e
)
{
System.Console.WriteLine ( "Start" ) ;
System.Threading.Thread.Sleep ( 5000 ) ;
System.Console.WriteLine ( "Stop" ) ;
return ;
}
Sample output:
Start
Start
Start
Start
Start
Stop
Start
Stop
Start
Start
Stop
Start
Stop
Stop
Start
Stop
Start
Stop
Start
Stop
Start
Stop
Start
|
|
|
|
|
George_George wrote: I think the Timer class is used to perform a task in a regular interval no matter whether or not the previous task is completed or not. So, it is possible to execute two tasks at the same time if the 1st task is not completed in 1 second for some reason. Right?
AFAIK, timer's execution will be blocked until it finishes the job. So you won't be able to run two tasks in parallel in normal case. I think you can use a Producer/Consumer Queue[^] like things to do this. Each time timer ticks, enqueue job. So it will be executed on another thread and timer thread won't be blocked. I might be wrong here
|
|
|
|
|
Hi N a v a n e e t h,
It is really a great article. I read the sample you mentioned, I like the code but confused about what means "except that the caller doesn't get blocked if the worker's already busy with a task", any ideas?
I think the caller (I think in this sample should be someone which enqueues the task) is blocked when the worker is working since it uses a lock called "locker" when Enqueue. Any ideas?
regards,
George
|
|
|
|
|
George_George wrote: "except that the caller doesn't get blocked if the worker's already busy with a task", any ideas?
Yes this is true. Caller won't be blocked. The example uses a queue system. When you enqueue new task, it will be added to the queue and immediately control will be given back. The program then processes each task depending on the availability of worker threads.
|
|
|
|
|
Thanks N a v a n e e t h,
1.
N a v a n e e t h wrote: Yes this is true. Caller won't be blocked. The example uses a queue system. When you enqueue new task, it will be added to the queue and immediately control will be given back. The program then processes each task depending on the availability of worker threads.
I agree. But the Enqueue operation will be blocked since it is using a lock object to do synchronization, right? Means if worker thread is at the same time Dequeue, the person who Enqueues must wait.
2.
I have an alternative implementation, which will use the following logics,
2.1 Sleep 1 second;
2.2 Check is a bool variable called stop is set by some other threads?
2.3 If stop is set, then stop current thread;
2.4 If stop is not set, then do some regular work, then go back to 2.1.
Does using event (as showed in the document you referred) has better performance compared with my alternative method?
regards,
George
|
|
|
|