|
I agree, ManualResetEvent is IMHO the best solution. In your case you might not need a timeout, however, because a long pause does not indicate an exceptional case (if you are making a "play/pause" sort of functionality).
But remember to implement IDisposable in your class, and signal the ManualResetEvent before Disposing to make sure that your background thread ends also, otherwise you will end up waiting forever.
|
|
|
|
|
I am having following code in delphi language.
i want to convert it into C#
The delphi contains CreateOleObject method. but in .net we are not having such method. how to convert it.
code:
//set com port
Comet := CreateOleObject('Comteck.xy');
Comet.ComPort := "COM1";
I wamt to Use that 'Comteck.xy' file compulsary. please help me.
Thanks in advance
gaurav
|
|
|
|
|
Right click your project in VS and select "Add reference". Then select your COM object from the list of installed objects. .Net will create a managed wrapper class (RCW) which you can use to instantiate the object and access it like it was a managed object.
|
|
|
|
|
How to create a generic FTP Uploader windows Service which could upload Multiple Files at Different Folders to Differeent FTP Locations?
|
|
|
|
|
There are multiple "hows" possible. Simplest would be to write a batchfile that uses the FTP command, and execute that
I are troll
|
|
|
|
|
Hi,
I have a media center that is activated by a remote control.
My computer uses dual monitors, and I would like to make it so that the secondary monitor turns off when I start up the media software.
Ive found working examples that turn off both monitors, does anyone know of a good example that will turn just one off?
Cheers,
Mark Brock
"We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
Click here to view my blog
|
|
|
|
|
|
I'm developing a countdown timee that will support Pause function and I'm wondering that how can I get the passed milliseconds from Timer. I'm not sure that it is possible but perhaps it might possible through API.
I do not want timer calls tick event after each millisecond, what I want is once I set Interval(eg. 5000) then if I call Pause just after 3000 passed then when resume it should start with 3000. It would be easily possible if I can get the Timer passed millisecond...
any idea guys or any other way ?
edited
this helped me
Environment.TickCount
thanks all of you guys
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
modified on Thursday, April 9, 2009 8:54 AM
|
|
|
|
|
You could use DateTime.Now and subtract one from another to give you a timespan. Timers I don't believe allow you to access their internals.
DateTime has a resolution of about 15ms I believe.
Regards,
Rob Philpott.
|
|
|
|
|
DateTime itself has a resolution of 1 tick, which is 100ns.
What you probably wanted to say was that Windows.Forms.Timer is not very precise, because it always invokes the handler from a Gui thread, which means it will sometimes have to wait until OS is done doing Gui stuff before it actually calls the handler delegate.
|
|
|
|
|
Quite. But DateTime.Now will give not give you 100ns resolution. More like 15ms.
Regards,
Rob Philpott.
|
|
|
|
|
Good point, resolution of the Now property is in ms, and depends on the system. In Vista it seems to have a 1ms resolution, I just tested it:
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(16384);
for (int i = 0; i < 10000; i++)
sb.AppendLine(String.Format("{0:HH:mm:ss.ffff}", DateTime.Now));
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
System.Diagnostics.Stopwatch on the other hand is much more precise:
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(16384);
Stopwatch sw = new Stopwatch(); sw.Start();
DateTime now = DateTime.Now;
for (int i = 0; i < 10000; i++)
sb.AppendLine(String.Format("{0:HH:mm:ss.fffff}", now + sw.Elapsed));
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
|
|
|
|
|
Hey nice, stopwatch is new to me. We have some performance timers in the system I work on, but they use raw API calls via interop to do their stuff. Just tried your first example - the elapse is usually 15ms but sometimes 16ms, so on XP I guess the resolution is 15.something ms.
Regards,
Rob Philpott.
|
|
|
|
|
It all depends on the available hardware; you may want to look at the article "Timer surprises, and how to avoid them"[^] which predates .NET 2.0 and hence does not mention StopWatch (which is based on PerformanceCounters anyway).
|
|
|
|
|
You can create a new background thread with a manual reset event, waiting for a specified amount of seconds or an external Set.
int msToWait = 5000;
do {
manualStartEvent.WaitOne();
Stopwatch sw = new Stopwatch();
sw.Start();
bool externalSet = manualPauseEvent.WaitOne(msToWait);
sw.Stop();
int actualMsElapsed = sw.Elapsed.TotalMilliseconds;
if (externalSet)
{
msToWait -= actualMsElapsed;
if (msToWait < 0)
msToWait = 0;
}
else
{
msToWait = 0;
}
} while (msToWait > 0);
[EDIT] Code is posted only as an idea, you should take care of starting the thread, handling Stop situations and proper disposing all events.
modified on Thursday, April 9, 2009 8:39 AM
|
|
|
|
|
hmmm nice way...thanks
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Hi All,
I am unable to deserialize the byte array to the object of structure on another machine,when i send it through TCP/IP by socket then there is no problem in serialization.,but problem occurs at the other side during deserialization.
It shows the error something like "unable to find assembly....."
I am using the following code....
private byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream(arrBytes);
BinaryFormatter binForm = new BinaryFormatter();
memStream.Position = 0;
//memStream.Write(arrBytes, 0, arrBytes.Length);
// memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object)binForm.Deserialize(memStream);
return obj;
}
Please help,it is urgent....
Regards
Lalit Narayan
|
|
|
|
|
That's telling you that it needs to know about the type (defined in an assembly you don't have loaded) in order to deserialise it. Make sure its in your project's references on the deserialisation side.
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
thanks for reply,
but i have not attached the assembly at my side because i have to only read the data that is coming through TCP channel from the controller.
Please reply...
Regards,
Lalit Narayan
|
|
|
|
|
What you have there is a method to convert an object to a byte array, and a method to convert a byte array to an object.
What you're transmitting is the member data of the object. All the methods and code for it isn't sent - that's defined in an assembly. You need to make sure that for any type which is sent this way, the assembly which contains it is on both client and server.
Find out which assembly the type you are serialising is in. Then make sure that assembly is referenced in the project dependencies of the deserialising project.
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob,
Thanks for ur reply..
Actually the problem is that we are communicating the DDC through windows service,on DDC side there is no .Net assembly but the embeded type of work over there(like C++ type).
Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side.
But for testing purpose i am communicating between two computers that have .net application and windows service.But basically i have to set up communication between windows service and the DDC.
So i do not want to attach the assembly..because there will no assembly on other side in real time.
Please reply...
Regards,
Lalit Narayan
|
|
|
|
|
lnmca wrote: Now if when we receive the byte array from DDC then i have to convert it into object of my own created structure same as DDC side.
That's not good enough. When you call serialize, it packages up the fully qualified type name - that includes namespace, type name and assembly. When deserialising on the other side it will use all these things to create the type prior to populating it. Just because you have a type with the same name .NET will not use that.
I don't entirely understand what's you trying to do, but unless both sides are .NET you want to steer clear of Serialisation and just send the relevant fields one by one.
Regards,
Rob Philpott.
|
|
|
|
|
|
Hello,
has somebody a tutorial for UDP Hole Punching or STUN? I have a few problems with the UDP Punching and a few points which i don't understand, maybe somebody could help me:
Here an article to UDP Hole Punching: http://www.h-online.com/security/How-Skype-Co-get-round-firewalls--/features/82481[^]">
so, UDP Hole Punching works like this, you have 2 clients and one server which knows the IP-Adresses of the clients and their ports over which they would send their UDP Packages (so here's the first problem, from where does the server know, which ports the client use?)
first step: Client1 gets the IP-Adress and the Port (see above, here's the problem) of Client 2.
second step: CLient1 (IP: 1.1.1.1:1000) sends a package to Client 2 (IP: 2.2.2.2:???? Which port is it?) over UDP, which will be deleted on the NAT-Router of Client 2 (no incoming packages)
third step: Client 1 sends the server the information to initiiate a connection to Client 2 over the port 1000 (Client 1) and the Port ???? (Client 2).
fourth step: CLient 2 gets the information to create a udp-connection to CLient 1 (IP: 1.1.1.1:1000) over his Port ????.
Well, you see the principle is easy, but the Ports of CLient 2 are my big problem, how do i find that out? Has somebody experience in UDP Hole Punching or STUN? Well, what's STUN? I get from a STUN server only my Port, from which i sendet my package to the Server, but another client couldn't do anything with that port and thats the problem how to find out the port of my partner.
Many Thansk
|
|
|
|
|
Hi,
I am also trying to figure out the solution for the same problem.
Does anyone have the answer?
|
|
|
|