|
I think I've sorted it Luc. Part of the problem was TimeSpan.FromSeconds and FromMilliseconds are only accurate to 1 millisecond so it was bound to do wierd stuff!
The answer to that was to use TimeSpan.FromTicks, in combination with Math.Round(), and now I appear get 1 picosecond 10 nanosecond precision. [Edit] The resolution of Ticks per second [/Edit]
Obviously the first pass results in a different timestamp still - but it's acceptably close. I just run it through a TimestampRound method first so future comparisons are made against that.
public static class NTPConversion
{
public static readonly DateTime Epoch = new DateTime(1900, 1, 1);
public static UInt64 TimestampRound(UInt64 timestamp)
{
long ticks = (long)(Math.Round(
(double)timestamp / 0x100000000, 7, MidpointRounding.AwayFromZero)
* TimeSpan.TicksPerSecond);
UInt64 newTimestamp = (UInt64)(((double)ticks / TimeSpan.TicksPerSecond) * 0x100000000);
return newTimestamp;
}
public static DateTime TimestampToDateTime(UInt64 timestamp)
{
long ticks = (long)(Math.Round(
(double)timestamp / 0x100000000, 7, MidpointRounding.AwayFromZero)
* TimeSpan.TicksPerSecond);
TimeSpan timeSpan = TimeSpan.FromTicks(ticks);
DateTime dateTime = Epoch + timeSpan;
return dateTime;
}
public static UInt64 DateTimeToTimestamp(DateTime dateTime)
{
TimeSpan timeSpan = dateTime - Epoch;
long ticks = timeSpan.Ticks;
UInt64 timestamp = (UInt64)(((double)ticks / TimeSpan.TicksPerSecond) * 0x100000000);
return timestamp;
}
} Thanks for your help! If you see any obvious flaws in this - let me know!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
modified on Friday, July 10, 2009 2:48 PM
|
|
|
|
|
Hi Dave,
I am confused, I used Reflector and got convinced ticks was the best you could ever get from DateTime/TimeSpan. I will need more time and a bigger screen to investigate further, I am on a tiny netbook right now, making some things harder. I'll come back on this next week.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
You're right - I was getting confused between nano and pico, this stuff has really fried my brain this week!
10ns is the smallest achievable resolution - good enough I think.
[Edit] After all that, once moved over to the real application, there were some serious errors in that code. I ended up dividing into the whole part and fractional part as you suggested initially. The code below works well (In the actual app the ulong's are converted to a big endian byte array)[/Edit]
private void DateTimeToTimestamp(DateTime dateTime, int startIndex)
{
UInt64 ticks = (UInt64)(dateTime - Epoch).Ticks;
UInt64 seconds = ticks / TicksPerSecond;
UInt64 fractions = ((ticks % TicksPerSecond) * 0x100000000L) / TicksPerSecond;
for (int i = 3; i >= 0; i--)
{
data[startIndex + i] = (byte)seconds;
seconds = seconds >> 8;
}
for (int i = 7; i >= 4; i--)
{
data[startIndex + i] = (byte)fractions;
fractions = fractions >> 8;
}
}
private DateTime TimestampToDateTime(int startIndex)
{
UInt64 seconds = 0;
for (int i = 0; i <= 3; i++)
seconds = (seconds << 8) | data[startIndex + i];
UInt64 fractions = 0;
for (int i = 4; i <= 7; i++)
fractions = (fractions << 8) | data[startIndex + i];
UInt64 ticks = (seconds * TicksPerSecond) + ((fractions * TicksPerSecond) / 0x100000000L);
return Epoch + TimeSpan.FromTicks((Int64)ticks);
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
modified on Saturday, July 11, 2009 8:11 PM
|
|
|
|
|
Hi,
I use the Stream class in a framework.dll to read and write a setting like this,
get
{
Stream stream = GetStreamForRead(ClientHelpFile);
if (stream == null)
return HelpOption.Automatic;
int HelpOptionRead = stream.ReadByte();
stream.Close();
}
set
{
Stream stream = GetStreamForWrite(ClientHelpFile);
stream.WriteByte((byte)value);
stream.Close();
}
private const string ClientHelpFile = "TAC.ClientHelpProfile.bin";
private const IsolatedStorageScope scope =
IsolatedStorageScope.Roaming |
IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain;
private static IsolatedStorageFile GetIsolatedStore()
{
return IsolatedStorageFile.GetStore(scope, null, null);
}
private static Stream GetStreamForWrite(string filename)
{
IsolatedStorageFile storage = GetIsolatedStore();
return new IsolatedStorageFileStream(filename, FileMode.Create, storage);
}
private static Stream GetStreamForRead(string filename)
{
IsolatedStorageFile storage = GetIsolatedStore();
if (storage.GetFileNames(filename).Length == 0)
return null;
return new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, storage);
}
the problem I am facing is I am using another application and referencing the same dll(FrameWork.dll).
the storage.GetFileNames returns null and I wish it return me whatever the file has saved in it. Am I missing something?
|
|
|
|
|
AnilUnni wrote: the problem I am facing is I am using another application and referencing the same dll(FrameWork.dll).
the storage.GetFileNames returns null and I wish it return me whatever the file has saved in it. Am I missing something?
I think, note that is bold, that the problem might be IsolatedStorageScope.Domain . Each application runs in its own domain, so the path to the isolated storage will be different from different applications.
Once again, this is not a certainty, just my interpretation of how it works.
If it were my app, I would start with just IsolatedStorageScope.User, see if it works for both apps add another scope see if it works etc until it fails. That way you will identify which scope(s) will break it and can avoid them.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi all,
Thanks a lot for your help in advance.
I am doing a Mail Merging Application. I can get all data from the source (database, file or anything) and using mail Merge fields in Word Document from C# code, I open up the word which is merged with data from database.
MS Word Document Opens –
--- should this be open in IE
--- Or should this be open as ordinary MS Word on Desktop
The problem is if I open it anyway, I loose all control over the Word Document which is opened. I don’t want to do that. I want to keep control over it and allow the Users to update the document with some standard messages which we have in the project. What that means is, the messages will be selected and those messages selected will be put at some point where cursor is kept in the document.
So, when mail merging happens
MS Word Document is opened.
A pop up with standard messages is opened.
User selects the messages he/ she wants to insert into the MS Word Document which is opened.
For these things to happen, I should have control over the MS Word Document which is opened on the client side. Do selection of the messages and populate in Word document (this is my understanding, is there any way to do it easily).
The Issues are:
I need a way to open the MS Word Document in a controlled manner where I can still control the User editing or help user in editing when he / she clicks on my standard messages and wants them to populate on the MS Word Document.
How can I save the MS Word document and make sure that user is not saving it locally and has to upload it again to save it on server.
I Hope I am clear in explaining the situation, please find the attachments which show the MS Word Doc and the popup on side .
ANY RUNNING CODE WILL BE OF GREAT HELP.
|
|
|
|
|
Sandumone wrote: ANY RUNNING CODE WILL BE OF GREAT HELP.
So this is a new way to say snd me the codz. This will get you flamed severely for being rude and shouting it is just plain stupid.
You have a design problem, why do you open the word document before asking the user for the message. Get the message text in your app before you start the word merge that way you do not need to relinquish control.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I need to get the full name and email of a user using nothing more than the 2.0 Framework.
I figured LDAP would be the best way to go, but I cannot get my code to work.
I am not using the syntax correctly. I need to get the information using the logged in user name.
if my username is "mypicturefaded" and my email for the company is mypicturefaded@bbt.gov, how would I get the full name or email address. Is there an easier way of doing this? It needs to be from Active Directory though. I can do it using 3.5 framework, but it needs to be rolled back. Here is the code I have:
<br />
<br />
string strValue;<br />
DirectoryEntry ent = new DirectoryEntry("LDAP://CN=mypicturefaded");<br />
strValue = ent.Properties["displayName"].Value.ToString();<br />
ent.Close();<br />
<br />
Messagebox.Show(strValue);<br />
<br />
|
|
|
|
|
|
Here [^]you go.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
So how come something like this doesn't work?
<br />
<br />
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=bbt,DC=com");<br />
DirectorySearcher mySearcher = new DirectorySearcher(entry);<br />
mySearcher.Filter = "(&(objectClass=user)(anr=" + "mypicturefaded" + "))";<br />
SearchResult searchResult = mySearcher.FindOne();<br />
string email = searchResult.Properties["mail"].ToString();<br />
MessageBox.Show(email);<br />
I just need the one field. I really don't need all of them.
I get a NullObjectException error.
|
|
|
|
|
Should be like this:
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=Domain,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(anr="+ userID +"))";
mySearcher.PropertiesToLoad.Add("mail");
SearchResult searchResult = mySearcher.FindOne();
string email = searchResult.Properties["mail"];;
MessageBox.Show(email);
You did not add mail as a property to be loaded.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
"A referral was returned from the server." is the error I am getting now.
Hmmm, this is more tricky than I thought.
|
|
|
|
|
username should always be specified as domain\username. Maybe that's the problem?
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
I did it that way. It still isn't working though...Thank you for your help though.
It was a step in the right direction at least. So thank you. What does the anr stand for?
Here is the 3.5 code I am using.
<br />
email = System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress;<br />
Too bad I can't just use it.
|
|
|
|
|
|
Hi all,
I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you!
Code:
Form 1
public void UpdateGridView()
{
// Method code
}
form 2
public void MapFormUpdate_Click(object sender, EventArgs e)
{
Form1 MainForm = new Form1();
MainForm.UpdateGridView();
this.Close();
}
|
|
|
|
|
You made a new form to call a method on and then throw it away. I would advice you not to ask why that does not do what you want, I'm sure several people will laugh at you and/or 1-vote you.
|
|
|
|
|
I tried it without the Form1 MainForm = new Form1(); code but it gives an error NullReferenceException. How can I get around this?
|
|
|
|
|
You want to call it on the original form, not a new one. Usually you only have 1 instance of the main form anyway, so you could use the singleton pattern (makes managers and Java programmers happy) or just pass the instance of form1 to form2 when you make form2. (or use an event, see Davey's post)
There is a difference between a class and an instance of it. I'm sure you know, but it seems like you need to be reminded of that. But no offense..
|
|
|
|
|
No offense taken. I'm new to C# programming. Usually work with SQL. Thanks for the input!
|
|
|
|
|
Form1 MainForm = new Form1(); is not going to work as it's a new instance of Form1, not the one that you started with.
The recommended way of doing this is to raise a custom event in form2, that Form1 subscribes to, so Form1 calls its own method.
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonShowForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.DoUpdate += new EventHandler(form2_DoUpdate);
form2.Show();
}
void form2_DoUpdate(object sender, EventArgs e)
{
Console.Write("Update");
}
}
using System;
using System.Windows.Forms;
public partial class Form2 : Form
{
public event EventHandler DoUpdate;
public Form2()
{
InitializeComponent();
}
void buttonUpdate_Click(object sender, EventArgs e)
{
OnDoUpdate(EventArgs.Empty);
}
protected virtual void OnDoUpdate(EventArgs e)
{
EventHandler eh = DoUpdate;
if (eh != null)
eh(this, e);
Close();
}
} If you need to pass data along with the event, create your own class derived from EventArgs and pass an instance of that instead of EventArgs.Empty. You will need to change the EventHandler to EventHandler<YourEventArgs>
See my Events Made Simple[^] article for more details.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thank you for your reply. That works perfectly.
|
|
|
|
|
Hej bwood2020
You Migth whant to use a static resource that can laungh that form1 passes an event to
static class EventHandler
{
static Action Event;
public static void SetGlobalEvent(Action a)
{
Event = a;
}
public static void Execute_Event()
{
Event();
}
}
class Form1
{
public Form1()
{
EventHandler.SetGlobalEvent(Event);
}
public void Event()
{
var x = 2 + 2;
}
}
class Form2
{
public void Onclick()
{
EventHandler.Execute_Event();
this.Destroy();
}
}
There migth be a bether way of doing this if form1 creates form2 you can pass
in "this" from form1 as a parameter that way form2 whill be able to call the method on that object
Hopes this helps Patrik
|
|
|
|
|
Patrik.karlin wrote: if form1 creates form2 you can pass
in "this" from form1 as a parameter
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|