|
Hmm. That sure is the easiest way, but it would severely degrade app responsiveness when Ping.Send() is awaiting a reply and possibly times out. Yes one can use an overload that offers a timeout parameter, however IMO there isn't a timeout value that is sufficiently long for Ping not to give false negatives, and at the same time sufficiently small for the app to remain fully responsive.
So for a periodic Ping to a constant address I would recommend using a separate Thread, with a forever loop holding Ping.Send() and a Thread.Sleep(); and then a simple bool variable could offer the latest state. Simple variables can safely be accessed across thread boundaries, no locking or other synchronization would be required.
|
|
|
|
|
Why not use an async method combined with Ping.SendPingAsync[^]?
private void OnPingSuccess()
{
...
}
private void OnPingFailure()
{
...
}
private async Task MonitorServer(IPAddress address, TimeSpan frequency, CancellationToken cancellationToken)
{
Ping ping = new Ping();
while (!cancellationToken.IsCancellationRequested)
{
PingResult result = await ping.SendPingAsync(address);
if (pingresult.Status == IPStatus.Success)
{
OnPingSuccess();
}
else
{
OnPingFailure();
}
await Task.Delay(frequency, cancellationToken);
}
}
private CancellationTokenSource _cancellationTokenSource;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_cancellationTokenSource = new CancellationTokenSource();
_ = MonitorServer(IPAddress.Parse("10.10.9.15"), TimeSpan.FromMinutes(1), _cancellationTokenSource.Token);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed();
_cancellationTokenSource?.Cancel();
} The OnPingSuccess and OnPingFailure methods should be able to directly update the UI, because they'll be running on the UI thread.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard. That looks interesting. I had been considering async /await but am unfamiliar with it; also I'm not afraid of going all in with new Thread() . Your async approach seems like a lot of code...
|
|
|
|
|
Most of it's about handling cancellation when the form closes. Which you'd need to do with a background thread as well.
Plus you have the added advantage that you can update the UI from the async method, without having to monitor changes to a variable or mess around with Invoke or synchronization contexts.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
All true. I set Thread.IsBackground true, and typically have a global public static bool AppIsExiting which I test in my forever loops. That takes care of most of the potential termination problems. Your approach is cleaner of course, mine dates back 20 years...
|
|
|
|
|
Check out System.Reactive
.NET Foundation Reactive Extentions[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
hi, does in c# exist a type of variable that is like pointers in c++?
I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing?
Thank you.
|
|
|
|
|
Yes. They are called "pointers": Pointer types - C# Programming Guide | Microsoft Docs[^]
Do note that you need to specify unsafe for yoru code in order to use them, and you need to take precautions that the memory won't be moved by the GC while you are using the pointers. If you don't need 'em, don't use 'em: and that's "real need" not "I need this to be like my C++ code".
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
There's no need to do things like that in c#, so if you find yourself doing things like that then you probably need to rethink your solution and find one more suitable to the language.
|
|
|
|
|
Just declare a byte[] type and you can access the content in a similar way, although by index value rather than using pointer arithmetic.
|
|
|
|
|
|
Yes. Pointers do exist in C#
Refer this link for pointers in C#: Pointers in C#
modified 25-Oct-19 4:28am.
|
|
|
|
|
Mucking around with DataGridView, DataTable, DataView: this works, but, I suspect there's a better way to do this:
private void btnSave_Click(object sender, EventArgs e)
{
if (dt != null)
{
DataTable dtbl = dt.Clone();
if (dView != null)
{
foreach (DataRowView vrow in dView)
{
dtbl.ImportRow(vrow.Row);
}
}
dtbl.WriteXml(... filepath goes here .xml ... );
}
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
How about using the ToTable method on the view?
DataView.ToTable Method (System.Data) | Microsoft Docs[^]
if (dt != null)
{
DataTable dtbl = dView != null ? dView.ToTable() : dt.Clone();
dtbl.WriteXml(...);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Hallo, I'm trying to extract images from pdf.
The problem is with some images that, in pdf, are with the attribute Predictor. I think that, with this attribute, the resultant bytes are to be decoded with png algorith, but I don't find it.
Another problem is with images that are with 8 bit indexed, in the pdf file I don't file the palette, as in other pdf files. Is there someone who is expert with pdf images? I hope my question is understandable. Thank you.
|
|
|
|
|
Hi,
you seem to be peeking into the PDF file to extract what you need, and that is bound to give you surprises when several different PDF sources are involved.
There is another way to approach things, that may or may not meet your needs: using something like Ghostscript[^] you can get an image of a particular page of a PDF document (in any resolution you want), then operate on that.
The advantage is you don't need to deal with all the possible ways PDF stores/creates images; the disadvantages are (1) you only get the resolution you asked for and (2) if all you need is a picture you still have to locate it and extract it from the page image.
Hope this helps.
|
|
|
|
|
How to use ghostscript?i have installed it but i don't understand how to run it.
|
|
|
|
|
Ghostscript can be ran in a separate process; it takes parameters from its command line. It is one of the many programs that explain their parameters by running it with a /h, -h or -help argument in a command window.
Here is a method I once used, you would have to adapt it to your needs of course.
using System.Diagnostics;
using System.IO;
...
string toolsFolder=@"...";
...
private Bitmap getPngImageFromPDF(int resolution, int pageNumber, string pageName, string inName) {
string args=" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dTextAlphaBits=4 "+
"-r" + resolution + " -sPageList=" + pageNumber + " -sOutputFile=" + pageName + " " + inName;
string cmd=Path.Combine(toolsFolder, "gswin32c.exe");
ProcessStartInfo psi=new ProcessStartInfo(cmd, args);
psi.CreateNoWindow=true;
if (!withDebug) psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
Process proc=Process.Start(psi);
proc.WaitForExit();
Bitmap bm=(Bitmap)Image.FromFile(pageName);
return bm;
}
Notes:
1. pageName is the name of the file that will be generated by ghostscript.
2. the generated file will be locked as long as the generated bitmap is alive.
3. you should Dispose() of the bitmap when you no longer need it.
|
|
|
|
|
I am not able to generate the png file, the file should have generated in the same folder of the pdf files?
|
|
|
|
|
If you have trouble with ghostscript, run it manually first, i.e. from inside a Command Prompt (you can copy/paste command lines into it) and look at what it tells you. Only when you're satisfied should you start using C# code and the Process class.
The easiest way to get the folder issues sorted is by putting the ghostscript exe and your C# exe in the same folder (say "Debug"); I expect ghostscript will then use paths relative to that ("Debug") folder.
NB: there is nothing to install about ghostscript, you can put the exe anywhere, as long as it can be found (e.g. because its folder is added to the PATH environment variable, or because it is in your "current directory"). Alternatively you can tell the Process class where it is, that is what my toolsFolder did.
Warning: if you specify a full or partial path for the output, you probably must make sure the folder exists before running ghostscript. And the output location must be writeable, so special folders such as C:\Program Files\ are a no-no.
I can't help you any further, it is all standard Windows behavior as far as I know.
|
|
|
|
|
In C#.Net. Drag and drop control groupBox1 while run time how to write code ?
|
|
|
|
|
|
I took a look at the link you sent for drag-and-drop controls, but I haven't found an example yet
|
|
|
|
|
The first link gives a complete walkthrough. Maybe you need to study it in closer detail.
|
|
|
|