|
I had thought about that approach. I'm trying to eliminate the need for the user of my library to initialize the library (hiding the details). It's almost like the global.asax file - with the Application.Start event. If there was an Assembly.Load event or something similar that i could hook into, so the initialization could be done automatically.
Phil
|
|
|
|
|
There aren't any equivalent events like Assembly.Load. It still sounds like the best approach would be to implement a singleton MQManager class. Since it is a singleton you can hide all of the initialization details behind an explicit Register method. Each "normal" method can check the state and initialize as needed.
Something like this (rough pseudo-code):
public class MQManager {
private bool registered;
public void Register() {
if (!registered) {
registered = true;
}
}
public void AddMessage() {
Register();
MQ.Add();
}
}
Since the Register function protects itself from registering more than once through the registered flag, it is safe to call multiple times. Also, since the AddMessage function makes a call to Register before doing anything else, it is safe to be called by the user without having first explicitly called Register .
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
I appreciate the help.
If that's what I got do - then that's the way I'll go.
Tanks
Phil
|
|
|
|
|
well, i have a timer initilized and started in the Form constructor :
tmpTimer = new System.Timers.Timer(1000);
tmpTimer.Elapsed += new ElapsedEventHandler(tmpTimer_Elapsed);
tmpTimer.Start();
the timer start in the form constructor and keep checking a bool variable(doThing),
if this variable is set to true in the program, the timer excute the event handler,
private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (doThing == true )
{
MessageBox.Show(doThing.ToString());
}
}
but i want the event handler to excute only once(at the time that doThing is set to ture
, so i add one line of code to change the value of doThing to false,to prevent excuting
of the event handler.
private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (doThing == true )
{
MessageBox.Show(doThing.ToString());
doThing = false;
}
}
but the poblem it's that the value of doTing dose not changed, and messageboxes keep
showing in screen every one second (with True printed on them) till i close one of
them, then they stoped showing.
what i want is to prevent timer from entring if block by changing doThing to false.
i don't know what is realy happened there, any gusse or suggestion would be helpfull,
thanks in advance.
|
|
|
|
|
MSDN states: "The Elapsed event is raised on a ThreadPool thread. If the processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread."
Since the MessageBox.Show method is a blocking method, doThing is true until you close the first message box, and the execution of the event handler on additional Threadpool threads causes the appearance of a new message box every seconds.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
If you move doThing = false above the MessageBox.Show , there will be a small improvement, but there would still be a possibility of multiple message boxes.
You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer. Something like
void StartTimer()
{
int dueTime = 5000;
object state;
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerMethod), state, dueTime, Timeout.Infinite);
}
Passing Timeout.Infinite as the last parameter makes the timer run only once.
|
|
|
|
|
S. Senthil Kumar wrote: You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer.
The same can be achieved by setting the System.Timers.Timer.Autoreset property to false .
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
The documentation[^] on the property doesn't say that the timer will fire only once, it says that the Elapsed event will be raised only once, the first time the timer elapses. You could argue it's the same, but the OP has to write code to stop the timer.
|
|
|
|
|
|
In VB there is this out of focus even for a text box. Can some one tell me the equivalent for C#?
I would like to validate a value in the text box when we leave this text box.
Suggestions?
Thanks,Arun
|
|
|
|
|
mrarunks wrote: In VB there is this out of focus even for a text box. Can some one tell me the equivalent for C#?
It is the same framework, so it will have the same events (Unless, by VB you are talking about VB6 or ealier)
|
|
|
|
|
Enable the CausesValidation property and register to the Validating event.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
i need to write method and return somevalue from that method
for example return number
and use that number in the rest of application
ma_refay
|
|
|
|
|
From your question I can tell you need more help than can be provided here.
only two letters away from being an asset
|
|
|
|
|
Buy a book. C# Step By Step would be an excellent start.
Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture)
http:\\www.frankkerrigan.com
|
|
|
|
|
ma_refay wrote: i need to write method and return somevalue from that method
for example return number
and use that number in the rest of application
You've been asking questions on this site for over a year now and you've only just got around to asking this?
public int SomeMethod()
{
return 1;
}
|
|
|
|
|
Please don't email me direct - I won't respond off line.
ma_refay wrote: how i call the return 1 in my form
or how to assign that return ti variable
int theValue = TheMethodThatReturnsOne();
|
|
|
|
|
I'm trying to send a mail through gmail using System.Net.Mail..but it's giving me errors. here's the code:
<br />
MailMessage msg = new MailMessage();<br />
msg.From = new MailAddress("jiminyjones@gmail.com");<br />
msg.To.Add("sharpie@usmarines.com");<br />
msg.Subject = "C# Mail Message!";<br />
msg.Body = "Hey Buddy!";<br />
msg.IsBodyHtml = false;<br />
SmtpClient b = new SmtpClient();<br />
b.Host = "smtp.google.com";<br />
b.Send(msg);<br />
I know the problem is with the authentication because gmail requires ssl, but i couldn't find a way to fix it (i tried "b.EnableSsl = true" but it didn't work).
|
|
|
|
|
I don't think gmail has an open SMTP relay.
You are not supplying a username or password for the smtp server to authenicate you.
Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture)
http:\\www.frankkerrigan.com
|
|
|
|
|
well, so how do i supply a username and password? :\ that's what i'm asking
|
|
|
|
|
|
Hello everyone -
a short (noob) question:
how do i copy an object containing a Dictionary making sure that the contents of the Dictionary are copied too ?
ex.
class A
{
public Dictonary<string,string> test = new Dictionary<test,test>;
}
...
A a = new A();
A b = new A();
A.test.Add("string1","string2");
b = a; // !! should perform a deep copy of the dictionary, but only copies a reference
b["string1"]= "anotherstring"; // !does also change a's ["string1"] entry but should not!
I do not get it to work...
Anyone can help me out of my misery ?
Thanks,
Amorphis
-- modified at 10:05 Friday 2nd March, 2007
|
|
|
|
|
You need to implement the Icloneable in your dictionary and use clone, that is what it is for, deep copies.
Hope that helps.
Ben
|
|
|
|
|
You can like the other poster create your own class and add the ICloneable interface to it.
But if you're wanting a way to perform a copy of a dictionary with out deriving a new class, you can do this as well...
For dictionaries that contain ICloneable objects:
Dictionary<KeyType, ValueType> dctSource, dctClone;<br />
<br />
dctSource = new Dictionary<KeyType, ValueType>();<br />
dctClone = new Dictionary<KeyType, ValueType>();<br />
<br />
foreach (KeyType key in dctSource.Keys)<br />
dctClone[key] = dctSource[key].Clone();
For dictionaries that contain strings like in your example:
dctSource = new Dictionary<string, string>();<br />
dctClone = new Dictionary<string, string>();<br />
<br />
foreach (string key in dctSource.Keys)<br />
dctClone[key] = dctSource[key];
(You don't have to clone the strings as they are immutable.)
|
|
|
|
|
Thanks, that was the info I needed - now it works as expected
Regards,
Amorphis
|
|
|
|