|
Maybe Item is a reference type, so you're always setting the same item and your list will always hold the same item in the entire list.
better use
List<Items> itemsArray = new List<Items>(NumberOfRecords)
and then in the loop always create a new item and append it, like that:
int c = 0;
while(c < NumberOfRecords)
{
Items item = new Item();
itemsArray.Add(item);
c++;
}
<pre>
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks for all the replys!
Ill do that, thanks a lot!
|
|
|
|
|
|
Hi,
I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object.
It only returns the IP address and since the DNS lookup table isn't up to date I can't use that.
Is there another way to get a computers name from the IP address
Thanks
WvdW
|
|
|
|
|
WvdW wrote: then executing a bat on the computer
[the CodeProject PoliticallyCorrectBot has removed some comments from this post]
hope that helps
|
|
|
|
|
Dns.Resolve method will get you the name
however I said before in another post the method is obsolete and Microsoft recommends to use Dns.GetHostEntry method instead of it but that method didn't give me the name of computers on our network except mine maybe there is a trick on using that!.
good Luck
|
|
|
|
|
Thanks
I guess I'll have rewrite it for myself or open a cmd and get the reply from there.
Enjoy the rest of your day
WvdW
|
|
|
|
|
I just had it in my archive maybe simplify your work but it in VB
<br />
<br />
Option Explicit<br />
<br />
'// define constants<br />
Private Const IP_SUCCESS As Long = 0<br />
Private Const SOCKET_ERROR As Long = -1<br />
<br />
Private Const MAX_WSADescription As Long = 256<br />
Private Const MAX_WSASYSStatus As Long = 128<br />
Private Const MIN_SOCKETS_REQD As Long = 1<br />
<br />
Private Const WS_VERSION_REQD As Long = &H101<br />
'Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF&<br />
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&<br />
<br />
Private Const WSADescription_Len As Long = 256<br />
Private Const WSASYS_Status_Len As Long = 128<br />
Private Const AF_INET As Long = 2<br />
<br />
'// structures<br />
<br />
Private Type HOSTENT<br />
hName As Long<br />
hAliases As Long<br />
hAddrType As Integer<br />
hLength As Integer<br />
hAddrList As Long<br />
End Type<br />
<br />
<br />
Private Type WSADATA<br />
wVersion As Integer<br />
wHighVersion As Integer<br />
szDescription(0 To MAX_WSADescription) As Byte<br />
szSystemStatus(0 To MAX_WSASYSStatus) As Byte<br />
wMaxSockets As Long<br />
wMaxUDPDG As Long<br />
dwVendorInfo As Long<br />
End Type<br />
<br />
'
'kernel32<br />
Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nBytes As Long)<br />
Private Declare Function apiStrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long<br />
'wsock32<br />
Private Declare Function apiGetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal hostname As String) As Long<br />
Private Declare Function apiWSAStartup Lib "wsock32.dll" Alias "WSAStartup" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long<br />
Private Declare Function apiWSACleanup Lib "wsock32.dll" Alias "WSACleanup" () As Long<br />
Private Declare Function apiInetAddr Lib "wsock32.dll" Alias "inet_addr" (ByVal s As String) As Long<br />
Private Declare Function apiGetHostByAddr Lib "wsock32.dll" Alias "gethostbyaddr" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long<br />
<br />
'// private functions<br />
Private Function InitializeSocket() As Boolean<br />
Dim WSAD As WSADATA<br />
<br />
'attempt to initialize the socket<br />
InitializeSocket = apiWSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS<br />
End Function<br />
<br />
Private Sub CloseSocket()<br />
'try to close the socket<br />
If apiWSACleanup() <> 0 Then<br />
MsgBox "Error calling apiWSACleanup.", vbCritical<br />
End If<br />
<br />
End Sub<br />
<br />
Public Function GetIPFromHostName(ByVal sHostName As String) As String<br />
'converts a host name to an IP address.<br />
<br />
Dim nBytes As Long<br />
Dim ptrHosent As Long<br />
Dim hstHost As HOSTENT<br />
Dim ptrName As Long<br />
Dim ptrAddress As Long<br />
Dim ptrIPAddress As Long<br />
Dim sAddress As String 'declare this as Dim sAddress(1) As String if you want 2 ip addresses returned<br />
<br />
'try to initalize the socket<br />
If InitializeSocket() = True Then<br />
<br />
'try to get the IP<br />
ptrHosent = apiGetHostByName(sHostName & vbNullChar)<br />
<br />
If ptrHosent <> 0 Then<br />
<br />
'get the IP address<br />
apiCopyMemory hstHost, ByVal ptrHosent, LenB(hstHost)<br />
apiCopyMemory ptrIPAddress, ByVal hstHost.hAddrList, 4<br />
<br />
'fill buffer<br />
sAddress = Space$(4)<br />
'if you want multiple domains returned,<br />
'fill all items in sAddress array with 4 spaces<br />
<br />
apiCopyMemory ByVal sAddress, ByVal ptrIPAddress, hstHost.hLength<br />
<br />
'change this to<br />
'CopyMemory ByVal sAddress(0), ByVal ptrIPAddress, hstHost.hLength<br />
'if you want an array of ip addresses returned<br />
'(some domains have more than one ip address associated with it)<br />
<br />
'get the IP address<br />
GetIPFromHostName = IPToText(sAddress)<br />
'if you are using multiple addresses, you need IPToText(sAddress(0)) & "," & IPToText(sAddress(1))<br />
'etc<br />
End If<br />
Else<br />
MsgBox "Failed to open Socket."<br />
End If<br />
End Function<br />
<br />
Private Function IPToText(ByVal IPAddress As String) As String<br />
'converts characters to numbers<br />
IPToText = CStr(Asc(IPAddress)) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 2, 1))) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 3, 1))) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 4, 1)))<br />
End Function<br />
<br />
Public Function GetHostNameFromIP(ByVal sIPAddress As String) As String<br />
<br />
Dim ptrHosent As Long<br />
Dim hAddress As Long<br />
Dim sHost As String<br />
Dim nBytes As Long<br />
<br />
'try to open the socket<br />
If InitializeSocket() = True Then<br />
<br />
'convert string address to long datatype<br />
hAddress = apiInetAddr(sIPAddress)<br />
<br />
'check if an error ocucred<br />
If hAddress <> SOCKET_ERROR Then<br />
<br />
'obtain a pointer to the HOSTENT structure<br />
'that contains the name and address<br />
'corresponding to the given network address.<br />
ptrHosent = apiGetHostByAddr(hAddress, 4, AF_INET)<br />
<br />
If ptrHosent <> 0 Then<br />
<br />
'convert address and<br />
'get resolved hostname<br />
<br />
apiCopyMemory ptrHosent, ByVal ptrHosent, 4<br />
<br />
nBytes = apiStrLen(ByVal ptrHosent)<br />
<br />
If nBytes > 0 Then<br />
'fill the IP address buffer<br />
sHost = Space$(nBytes)<br />
<br />
apiCopyMemory ByVal sHost, ByVal ptrHosent, nBytes<br />
GetHostNameFromIP = sHost<br />
End If<br />
Else<br />
MsgBox "Call to gethostbyaddr failed."<br />
End If<br />
'close the socket<br />
CloseSocket<br />
Else<br />
MsgBox "Invalid IP address"<br />
End If<br />
Else<br />
MsgBox "Failed to open Socket"<br />
End If<br />
End Function<br />
<br />
<br />
|
|
|
|
|
Thanks
Just scanned it but the 'GetHostNameFromIP' looks promising.
Luckily vb is not that difficult to convert to C#.
Hope you have a great day
WvdW
|
|
|
|
|
Hello,
I posted this on msdn thread too but haven't gotten any answer yet so was wondering if you guys can help.
===================
Ahh question about accessing Windows form from a thread created through thread.
When I tried to make sure it's thread-safe, it keeps looping back into InvokeRequired because for some reason the calling thread and creating thread are never the same... Here's a watered down example...
private void blahblahblah()<br />
{<br />
<br />
Thread t = new Thread(method);<br />
t.Start(); <br />
<br />
}<br />
<br />
private void method()<br />
{<br />
<br />
if(InvokeRequired)<br />
{<br />
<br />
<br />
}<br />
<br />
else<br />
{<br />
<br />
for(int i = 0; i < Number; i++)<br />
{<br />
<br />
<br />
Timer timer = new Timer();<br />
timer.Elapsed += new ElapsedEventHandler(andAnother);<br />
_Event.WaitOne();<br />
<br />
}<br />
<br />
}<br />
<br />
}<br />
<br />
<br />
<br />
private void andAnother()<br />
{<br />
<br />
if(InvokeRequired)<br />
{<br />
<br />
<br />
EventHandler Handler = new EventHandler(andAnother);<br />
this.Invoke(Handler, sender, e);<br />
<br />
}<br />
<br />
else<br />
{<br />
<br />
<br />
_Event.Set();<br />
<br />
}<br />
<br />
}
So on both threads, I want to access Windows Form. The first thread is good but the second thread that was created through the first thread loops through InvokeRequired.
Thanks!
|
|
|
|
|
Two pieces of information are missing:
- in what class is all this code, i.e. whose InvokeRequired are you using ?
- and what thread created the instance of that class having the problem ?
The comments don't match with the code:
- "Do something to Windows Form" and what you do is create a Timer (without sufficient
initialization I would say)
- "Doing something to Windows Form" and what you do is set an event.
Furthermore, the GUI thread should not be kept waiting on something.
ictoane wrote: it keeps looping back
what exactly does that mean ? are you getting a StackOverflowException ?
please provide exact code, with proper comment, and the symptoms that go with it.
The better your question is, the sooner you will get a good answer.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hello,
The comments "Do something to Windows Form" are replacements for codes dealing with Windows Forms to eliminate unncessary blocks of code that are irrelevant to the question. I just left them out because they're basically doing something to Windows Form like setting up the ProgressBar or changing the Label.
Basically I want to have a method that creates two threads. And for each of those two threads, create System.Timers.Timer to call on an event to update the ProgressBar and other Windows Forms every second. The two threads need to start on the same time but performs different tasks so I can't skip the threads and go immediately to Timers.
I want to interact with Windows Form on the first layer thread as well as the second layer thread. The way to call Controls in a thread safe manner is to use if(this.InvokeRequired) (<a href="http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx">)</a>[<a href="http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx" target="_blank" title="New Window">^</a>). However, this doesn't work when I use it on the second layer thread.
Therefore, the problem I'm getting is that whenever I use this.InvokeRequired on the second layer thread created by the first layer thread, "it keeps looping back". I'm not intentionally making it to loop back.
Thanks
|
|
|
|
|
Your biggest problem with this code is that the _Event.WaitOne(); is causing the GUI thread to wait until signalled.
This is a great example of a deadlock situation.
Phil
|
|
|
|
|
Not exactly. _Event.WaitOne() is actually called on another thread besides GUI thread so it's not telling the GUI thread to wait but the other thread to wait. Also, the problem occurs before it gets to _Event.WaitOne(). It happens when the Timer starts. Inside the method called by the Timer it keeps invoking itself.
if (this.InvokeRequired)
{
EventHandler Handler = new EventHandler(startProgressBar);
this.BeginInvoke(Handler, sender, e);
}
else
{
...
progressBar1.PerformStep();
...
}
|
|
|
|
|
You know best, we can not possibly improve what you do.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I'm trying to get help on the forum here not looking for sarcastic comments. You're very rude...
|
|
|
|
|
You are new on this forum. Maybe you dont understand how it works.
If you choose to ignore both valid remarks and questions asked, you will be on your own.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
How exactly am I ignoring valid remarks? I got two reponses and I replied both of them the best I could. For your response, you commented about "Do this on Windows Form" which I explained what it really meant etc. For the other response, the person pointed out a possible Deadlock which I replied that it's not the problem because in the code it doesn't even get to that part before it freezes up. All I'm doing is trying to point out the problem I'm having and clearifying it. Both reponses I received did not solve the problem so am I suppose to say thanks and not pursue it any more further simply because I'm new and "don't understand how it works?" Thanks but no thanks. I'll go somewhere else. Thanks for your help. Have a good day.
|
|
|
|
|
For us on this forum to be able to drill down to the actual problem being experienced with this code you will have to post a more explicit example. Otherwise, we have to make assumptions and our assumptions will be incorrect.
Phil
|
|
|
|
|
Hello All
I m facing one problem in C# Windows Application
That one is RPC Server Unavailable while open excel sheet
I m using excel object com object
i just want to import data into SQL Database
first i open the excel sheet and then read the excel finally store data into sql
but Excel object throw one error RPC Server unavailable
The error occur in the following code
WorkBook = ExpApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\Clone.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "\t", true, false, 0, true);
anybody plz help me
it s very urgent
Thanks in Advance
Sak
|
|
|
|
|
|
Thanks for ur command
but i m using Excel COM Object for reading excel data
so i need solution for rpc server unavailable using Excel dll
Thanks
Sak
|
|
|
|
|
How do I declare a queue of string arrays?
Queue<string[]> qVar = new Queue<???>;
|
|
|
|
|
you almost did that just here is the complete one
Queue<string[]> qString = new Queue<string[]>();
good luck
|
|
|
|
|
Ahh, I was trying Queue<string[]> but forgot the () after the fact.
Thanks!
|
|
|
|