|
Compared of using Timer, maybe this solution is of better performance (means less performance impact to the whole system)? Any comments?
while (false == StopEvent.WaitOne(1000))
{
}
regards,
George
|
|
|
|
|
That's how I write Services. I use a Timer. If the process needs the ability to stop part way through then I also use a semaphore. In some cases, the Service runs a method in another class (with another semaphore) and that class has an event that the Service can raise to stop that method.
|
|
|
|
|
Thanks PIEBALDconsult,
"If the process needs the ability to stop part way through then I also use a semaphore. In some cases, the Service runs a method in another class (with another semaphore) and that class has an event that the Service can raise to stop that method." -- I am confused whether you are using event or semaphore or both? Could you show me your pseudo code please? Code clarifies much more.
regards,
George
|
|
|
|
|
Both; the second class' event handler sets its semaphore.
George_George wrote: Could you show me your pseudo code
I could, and I'll try to, but it's late now.
|
|
|
|
|
Thanks PIEBALDconsult,
Here is my code, please let me know if you think my code is the same as your ideas or not. I am using Event and Timer, but not semaphore. I want to learn from you about the new method.
class Program
{
static AutoResetEvent CheckEvent = new AutoResetEvent(false);
static bool stop = false;
static void ThreadMethod2()
{
while (true)
{
CheckEvent.WaitOne();
Console.WriteLine(DateTime.Now);
if (stop)
{
break;
}
else
{
}
}
}
static void OnTimedEvent(object source, ElapsedEventArgs e)
{
CheckEvent.Set();
}
static void Main(string[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Thread worker = new Thread(new ThreadStart(ThreadMethod2));
worker.Start();
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
stop = true;
worker.Join();
return;
}
}
regards,
George
|
|
|
|
|
I'll use that as a basis for writing something for you tomorrow.
But you shouldn't need a Timer and a Thread and the while loop.
|
|
|
|
|
That will be great, PIEBALDconsult!
I am looking forward to learning from you.
regards,
George
|
|
|
|
|
Here's a simple console program that will display the time every five seconds until you press RETURN.
namespace Template
{
public partial class Template
{
private System.Timers.Timer tt = null ;
public Template
(
int Interval
)
{
tt = new System.Timers.Timer ( Interval ) ;
tt.Elapsed += F ;
return ;
}
private void
F
(
object sender
,
System.Timers.ElapsedEventArgs e
)
{
System.Console.WriteLine ( System.DateTime.Now.ToString() ) ;
return ;
}
public void
Start
(
)
{
tt.Start() ;
return ;
}
public void
Stop
(
)
{
tt.Stop() ;
return ;
}
[System.STAThreadAttribute()]
public static int
Main
(
string[] args
)
{
int result = 0 ;
try
{
Template x = new Template ( 5000 ) ;
x.Start() ;
System.Console.ReadLine() ;
x.Stop() ;
}
catch ( System.Exception err )
{
while ( err != null )
{
System.Console.WriteLine ( err.Message ) ;
err = err.InnerException ;
}
}
return ( result ) ;
}
}
}
|
|
|
|
|
Thanks PIEBALDconsult,
I have got your idea. Compared with my original code using sleep in the very first post, does your code have better performance (e.g. less impact to the whole application performance)?
regards,
George
|
|
|
|
|
George_George wrote: better performance
Doubt it's any better or worse, but I find it easier to read and maintain. Which is why I use Timers for my Services.
|
|
|
|
|
Thanks PIEBALDconsult,
1.
We have discussed solutions using only Timer, using only Sleep. Is it possible to have solutions using only Event? (I showed a solution using Timer together with Event, which I think is not good.)
2.
The most concern why I can not find a solution using Event only is, there is no way to check whether a signal is set or not after Wait (with timeout), right?
regards,
George
|
|
|
|
|
1) Not to my knowledge.
2) Those events (which I don't use and hadn't heard of until you mentioned them) require a thread of some sort.
Unless this is homework and you have to use those events, just do what you know works.
|
|
|
|
|
Thanks PIEBALDconsult,
This is not homework. Just my interest to find alternative solutions.
Here is my question showed by the code, it seems there is no way to check whether the signal is set or not after WaitOne(1000) returns, right? So, we can not just use a singal to check the stop condition for a thread, and we need some other approaches to check?
(my previous ideas is to check whether the signal is set or not, if it is set after WaitOne(1000), then I can stop the thread, but after MSDN document study, seems no way to check whether a signal is set or not after WaitOne(1000)?)
static void ThreadMethod3()
{
while (true)
{
CheckEvent.WaitOne (1000);
Console.WriteLine(DateTime.Now);
}
}
regards,
George
|
|
|
|
|
I don't know, I don't use those, I don't foresee a need for them.
|
|
|
|
|
I want to create my own browser,it is like Oicq or icq ,this is his clinet can reration his server.How can I do?help me,thank you!
|
|
|
|
|
lockepeak wrote: it is like Oicq or icq
These are not browsers, they are chat programs. You do it by writing client code, and server code. There is an example here on CP. Odds are you need to buy a book, and choose some more basic projects first.
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 All,
I've been reading up on this and all the examples I can find show the exact method I'm using to read a value yet my value is never retrived. I'm checking the version of WMP installed on the system using:
Microsoft.Win32.RegistryKey wmpRegKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Active Setup\Installed Components\{22d6f312-b0f6-11d0-94ab-0080c74c7e95}");
string wmpVersion = (string)wmpRegKey.GetValue("Version");
as soon as the last line is executed the wmpVersion won't even pop up a debugger widget and wmpRegKey won't either.
Any Ideas?
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
using Microsoft.Win32;
//Statements (In your case)
//------------------------------
RegistryKey Reg;
//open the registry key
Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Active Setup\Installed Components\{22d6f312-b0f6-11d0-94ab-0080c74c7e95}");
//get the requested values
if(Reg!=null)
{
string str1 = Reg.GetValue("Version").ToString();
MessageBox.Show(str1);
}
//close the key
Reg.Close();
//Regards
|
|
|
|
|
Hello
Is there a way to find out if any dataset is attached to a project
i.e. MyPoject.Dataset.count
Thanks
|
|
|
|
|
You'd need to keep that count for yourself.
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 for your reply,
i need to write a foreach loop for datasets, how can i write it?
foreach dataset in what?
Thanks again
|
|
|
|
|
Hi there.
I want declare method with optional parameters.
For example I want declare this method :
private void tree (string optional_parameter)
{
...
}
that I could call it like this :
tree();
Id est , I can omit optional_parameter.
Can we do it in C# ?
Sorry for my English. I'm a freshman .
|
|
|
|
|
|
C# doesn't support optional parameters. You achieve this concept by using method overloading.
Scott.
—In just two days, tomorrow will be yesterday.
—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]
|
|
|
|
|
Just do as:
private void tree(params string[] optional_parameter)
{
if (optional_parameter.Length > 1)
throw new Exception("tree(): Invalid parameter count");
else if (optional_parameter.Length == 1)
{
}
}
Best regards,
Jaime.
|
|
|
|