|
using System;
using Microsoft.Win32;
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Windows;
using System.Configuration;
using System.Threading;
namespace LogApplication
{
public partial class Event_frm : Form
{
ArrayList entries = new ArrayList();
DataGridBoolColumn col = new DataGridBoolColumn();
string selectedPath;
Progress progress = new Progress();// check here it is a form
public Event_frm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnDownload.Enabled = false;
chbSelect.Enabled = false;
string strHostName = Dns.GetHostName();
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList machine_names = new ArrayList();
machine_names.Clear();
for (int i = 0; i < gvMachine.RowCount; i++)
{
if (gvMachine.Rows[i].Cells[0].Value != null)
{
machine_names.Add(gvMachine.Rows[i].Cells[1].Value);
}
}
if (machine_names.Count <= 0)
{
MessageBox.Show("Please select atleast one machine name");
}
else
{
Credentials credentials = new Credentials();
credentials.username = "Administrator";
credentials.password = "1A2B3c456789$";
if (dgFolderBrowse.ShowDialog() == DialogResult.OK)
{
selectedPath = dgFolderBrowse.SelectedPath;
}
else
{
return;
}
if (rbtSoft_Inven_Info.Checked)
{
credentials.managementPath = @"\root\cimv2";
LogApplication.RegistryScan registryScan = new LogApplication.RegistryScan();
Hashtable registryEntries = registryScan.getRegistryEntries(machine_names, credentials);
ArrayList pass=(ArrayList)registryEntries["pass"];
ArrayList fail=(ArrayList)registryEntries["fail"];
FileWriter writer = new FileWriter();
int created = writer.write(pass, fail, selectedPath);
if (created > 0)
{
MessageBox.Show("Data Written");
}
}
else
{
credentials.managementPath = @"\root\cimv2";
if (ddEventType.SelectedIndex == -1)
{
errorProvider.DataSource = ddEventType;
errorProvider.SetError(ddEventType, "Please select valid event type");
ddEventType.Focus();
}
else
{
DateTime dateFrom;
DateTime dateEnd;
try
{
dateFrom = DateTime.Parse(string.Format("{0 }", dtFrm.Value));
}
catch (Exception ex)
{
MessageBox.Show("Invalid Start Date ");
dtFrm.Focus();
return;
}
try
{
dateEnd = DateTime.Parse(string.Format("{0 }", dtEnd.Value.AddDays(1)));
}
catch (Exception ex)
{
MessageBox.Show("Invalid End Date");
dtEnd.Focus();
return;
}
if (dateFrom <= dateEnd)
{
Hashtable logEntries = new EventLog().getLogEntries(machine_names,ddEventType.Text,credentials,dateFrom,dateEnd);
ArrayList pass = (ArrayList)logEntries["pass"];
ArrayList fail = (ArrayList)logEntries["fail"];
FileWriter writer = new FileWriter();
int created = writer.write(pass, fail, selectedPath);
if (created > 0)
{
MessageBox.Show("Data Written");
}
}
else
{
errorProvider.SetError(dtEnd, "End date should be less than or equal to Start date");
}
}
}
}
}
ArrayList generateMachineNames()
{
ProcessStartInfo pinfo = new ProcessStartInfo("net", "view");
pinfo.RedirectStandardOutput = true;
pinfo.UseShellExecute = false;
pinfo.CreateNoWindow = true;
using (Process p = new Process())
{
try
{
p.StartInfo = pinfo;
p.Start();
ArrayList machine_names = new ArrayList();
while (!p.StandardOutput.EndOfStream)
{
string read = p.StandardOutput.ReadLine().Trim();
if (read.StartsWith("\\\\"))
{
string[] machines = read.Split(' ');
machine_names.Add(machines[0].Substring(2).Trim());
}
}
return machine_names;
}
catch (IOException IOException)
{
MessageBox.Show("Error occurred in generating IP list, "+IOException.Message);
return null;
}
catch (Exception ex)
{
MessageBox.Show("Error occurred : " + ex.Message);
return null;
}
}
}
void loadMachines(ArrayList machineList)
{
int count = 0;
foreach (string machine in machineList)
{
gvMachine.Rows.Add();
gvMachine.Rows[count].Cells[1].Value = machine;
count++;
}
}
void loadMachines(string machine)
{
gvMachine.Rows.Add();
gvMachine.Rows[0].Cells[1].Value = machine;
}
private void rbtSoft_Inven_Info_CheckedChanged(object sender, EventArgs e)
{
ddEventType.Enabled = false;
dtFrm.Enabled = false;
dtEnd.Enabled = false;
}
private void rbtEvent_log_history_CheckedChanged(object sender, EventArgs e)
{
ddEventType.Enabled = true;
dtFrm.Enabled = true;
dtEnd.Enabled = true;
}
private void btnSearch_Click(object sender, EventArgs e)
{
if (rbtRemote.Checked == false && rbtLocal.Checked == false)
{
errorProvider.SetError(btnSearch, "Please, select one option");
}
else
{
if (rbtRemote.Checked)
{
// CHECK HERE ALSO
MethodInvoker invoker = new MethodInvoker(showProgress); /check here
invoker.BeginInvoke(null,null); //here tooooo
ArrayList machineList = generateMachineNames();
if (machineList == null || machineList.Count == 0)
{
MessageBox.Show("No machines are available, please check your network or try again");
btnDownload.Enabled = false;
if (gvMachine.Rows.Count > 0)
{
gvMachine.Rows.Clear();
}
progress.Close();
progress.Dispose();
}
else
{
loadMachines(machineList);
btnDownload.Enabled = true;
chbSelect.Enabled = true;
progress.Close();// here i got error
progress.Dispose();
}
}
else
{
try
{
string machine = Dns.GetHostName();
loadMachines(machine);
btnDownload.Enabled = true;
}
catch (SocketException SocketException)
{
MessageBox.Show("Error occurred in getting local machine name, " + SocketException.Message);
}
catch (Exception ex)
{
MessageBox.Show("Error occurred , " + ex.Message);
}
}
}
}
private void chbSelect_CheckedChanged(object sender, EventArgs e)
{
if (chbSelect.Checked)
{
for (int i = 0; i < gvMachine.RowCount; i++)
{
gvMachine.Rows[i].Cells[0].Value = true;
}
}
else
{
for (int i = 0; i < gvMachine.RowCount; i++)
{
gvMachine.Rows[i].Cells[0].Value = false;
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
try
{
Environment.Exit(0);
}
catch (System.Security.SecurityException SecurityException)
{
MessageBox.Show(SecurityException.Message);
}
}
void showProgress()//this is the method
{
progress.ShowDialog();
}
}
}
|
|
|
|
|
You are doing just opposite to what I said. I reiterate, do long running process on the method which you are invoking using MethodInvoker . Show progress dialog in the main thread. When the long running process finishes, end the progress window. You will get cross thread access error when you try to close the progress, so use Invoke/BeginInvoke to close the progress window safely.
|
|
|
|
|
If i put in the main thread then the execution hangs on showdialog() call
|
|
|
|
|
Hi,
I'd like to invoke a couple of methods retrieved from multiple,
dynamically loaded Assemblies (I have the MethodInfo objects)
asynchronously and wait for all methods to finish processing before I
continue. The method signature is:
public void ProcessJob(JobInfo job);
Can this be done? If so, how? Thanks in advance.
Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by. [Douglas Adams]
|
|
|
|
|
matthias s. wrote: asynchronously and wait for all methods to finish processing before I
continue
If you want to wait till all methods finishes, what is the point in using asynchronous calls ?
|
|
|
|
|
they might take quite some time for processing and they should run parallel. once all of them are done, i need to move on.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by. [Douglas Adams]
|
|
|
|
|
Not tested this, but I believe this almost near to what you need to do. Assume you have MethodInfo array named methods .
WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
new Thread(delegate(object userState)
{
info.Invoke();
(userState as AutoResetEvent).Set();
}).Start(waitHandles[i]);
}
WaitHandle.WaitAll(waitHandles);
|
|
|
|
|
Thank you very much for your reply. That looks very good and I'll test it as soon as I've understood it. What I don't get is where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?
Kind regards
/matthias
I love deadlines. I like the whooshing sound they make as they fly by. [Douglas Adams]
|
|
|
|
|
matthias s. wrote: where do I put my parameters (the info object takes a JobInfo parameter) in there and what is the userState object used for?
Well, you need to pass parameter to the Invoke() , right ? You could probably right an inner class and pass this classes instance to the user state, I mean as parameter to the thread's Start() method.
private class DataCarrier
{
JobInfo JobInfoInstance { get; set; }
WaitHandle Handle { get; set; }
DataCarrier(JobInfo info,WaitHandle handle){
this.JobInfoInstance = info;
this.Handle = handle;
}
} You can modify the above said method like
WaitHandle[] waitHandles = new WaitHandle[methods.Length];
for(int i = 0;i < methods.Length; i++ )
{
waitHandles[i] = new AutoResetEvent(false);
MethodInfo info = methods[i];
DataCarrier carrier = new DataCarrier(JobInfoInstance,waitHandles[i]);
new Thread(delegate(object userState)
{
DataCarrier c = userState as DataCarrier;
info.Invoke();
c.Handle.Set();
}).Start(carrier);
}
WaitHandle.WaitAll(waitHandles);
|
|
|
|
|
Don't get mad at me, but I don't fully understand the lines where the new thread is started.
N a v a n e e t h wrote: new Thread(delegate(object userState)
{
DataCarrier c = userState as DataCarrier;
// use c.JobInfoInstance here
info.Invoke();
c.Handle.Set();
}).Start(carrier);
If I try to compile this in my code, I get the problem that I need to pass a parameter to the info.Invoke() method. Besides that, the WaitHandle doesn't contain a Set() method.
Again, thanks for your help and patience.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by. [Douglas Adams]
|
|
|
|
|
matthias s. wrote: WaitHandle doesn't contain a Set() method.
Yes. It doesn't contain a set method. It's my mistake, it should be "EventWaitHandle ".
private class DataCarrier
{
JobInfo JobInfoInstance { get; set; }
EventWaitHandle Handle { get; set; }
DataCarrier(JobInfo info,EventWaitHandle handle){
this.JobInfoInstance = info;
this.Handle = handle;
}
}
matthias s. wrote: but I don't fully understand the lines where the new thread is started.
matthias s. wrote: {
DataCarrier c = userState as DataCarrier;
// use c.JobInfoInstance here
info.Invoke();
c.Handle.Set();
}
Quoted one is the method which gets executed when thread starts. You can create a new method and put this there if you are confused with anonymous methods. I used it for simplicity.
|
|
|
|
|
I want to use MIRR function to Returns a Double specifying the modified internal rate of return for a series of periodic cash flows (payments and receipts).
It is Working Fine in vb.net application
but in c# it is not showing MIRR function
Any Idea about this or any namespace included to show this option in c#
thanks in advance
|
|
|
|
|
You should
(1) add a reference to Microsoft.VisualBasic.dll in your project.
(2) use Microsoft.VisualBasic.Financial.MIRR (you may specify using Microsoft.VisualBasic; ad then use Financial.MIRR for code brevity).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello All
I am writing an application to monitor the serial port (RS232) and extract appropriate data from a continual ASCII stream that is written to it by a connected device.
The problem I have is with the datareceived event.
I want to work on the data received a byte at a time.
Thus I have set the datreceived threshold to a byte.
I would thus expect a new event to be generated each time a new byte is recieved.
I currently have a test app mimicking the connected device and I can send test strings to the monitoring application.
If I send strings of 2 bytes or less, 2 events are raised and I can process each individually.
However if I send longer strings, only 2 events are raised. The remaining unprocessed bytes remain in the buffer and are processed when a new event is raised.
I can use serialPort1.BytesToRead to determine number of bytes in the buffer and process them in a while loop. However this is not how I want this to work. I need to handle each byte as it is recieved.
Am i doing something silly?
Thanks
public Form1()
{
serialPort1.DataReceived +=new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.ReceivedBytesThreshold = 1;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffer = new byte[1];
try
{
serialPort1.Read(buffer, 0, 1);
}
}
|
|
|
|
|
Ok, it would appear that I have misundestood how serialport works. It is not posible to receive and event notification for every byte. I will therefore have to use the readbytes method and then itereate through all recieved.
|
|
|
|
|
Yep, SerialPort.ReceivedBytesThreshold "gets or sets the number of bytes in the internal input buffer before a DataReceived event occurs", i.e. there will be an event as soon as that number or more data
bytes are available, and the driver may offer more than one byte at a time, so no you will not get
an event for every byte (which would be very inefficient performance wise anyhow).
|
|
|
|
|
i use unmanaged code [odbc32.dll] to get the list of Sql Server on Network can any body tell me that this code will run on 64bit Computer Server smoothly
wasim khan
|
|
|
|
|
Shouldn't see any reason why not. Why don't you try testing it on the server 1st?
"Sex is not the answer, it's the question and the answer is yes"
|
|
|
|
|
i have 32bit server and i have not any 64bit server
wasim khan
|
|
|
|
|
Hell EveryBody,
I have a webservice in a server.And in another server I have a webApplication. When i use the webmethod of the webservice in my webApplication I got the following error.
I had already used this code, but no result.
webservLeave.PreAuthenticate = false;
webservLeave.Credentials = System.Net.CredentialCache.DefaultCredentials;
or
webservLeave.Credentials = System.Net.CredentialCache.DefaultCredentials;
The request failed with HTTP status 401: Unauthorized
plz help me out.
A key to every Solution
|
|
|
|
|
AS@13 wrote: The request failed with HTTP status 401: Unauthorized
Looks like anonymous access is not enabled. Allow anonymous access in the server or supply valid credentials instead of default credentials.
|
|
|
|
|
I had already done this,but no result.
plz tell me some other option.
my code is as below....
MyWS.Service webservLeave = null;
if (webservLeave == null)
{
webservLeave = new MyWS.Service();
}
webservLeave.PreAuthenticate = false;
webservLeave.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
webservLeave.Credentials = CredentialCache.DefaultCredentials;
webservLeave.MyWebMethod();
Thanks in Advance
A key to every Solution
|
|
|
|
|
Did you try to call .asmx file from explorer?
You can see your webService methohds. Are there any methods?
If there aren't any methods you must edit WebService application's security in IIS.
|
|
|
|