|
If an exception is not thrown your try block will be executed sequentially until finished, then the finally block is executed. If an exception is thrown, the appropriate catch block is executed, then the finally block is executed.
The finally is always executed except when you terminate the CLR using Environment.Exit .
See the C# language specification in the VS.NET product documentation or online at http://msdn.microsoft.com/library[^] for more information.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you for the reply!
So that means that even if the method WaitForStatus has a time span of say 3 minutes, the code in the try block will not execute the FINALLY block untill the either the servie has started successfully or the time has expired and then the exception is throw, which is handled in the CATCH?
So the program will pause until the the service starts or an exception is thrown?
Thanks for all the help!!
|
|
|
|
|
|
Hi,
I've a FTP server, which is a Unix box, and here are some 100s of log files reside.
Also, I'm trying to develope an application in C# where at one point i need these log files to be copied to the developement machine [which on the later date going to be small App Server, anyways,].
Now, my problem is these log files are going to be updated daily and I need to analyze them on daily basis. It would be awesome if I could just write a small program in C# which will just transfer these files on the developemtn machine on the back-ground.
Although I think .net remoting or .net's System.net might be usefull I have no idea about that.
I would really apreciate if you could tell me 2 things...
1) If it is at all possible [withought user intervention, coz path and file name convesion n everything is all fixed, and its not going to change]
2) If it is possible, I would appreciate if i could get a little push towards the right direction
thanks
|
|
|
|
|
There is no direct support for FTP in the .NET BCL (base class library), but there is for network copying and downloading via HTTP. This means you could use Samba or Apache (or any other HTTP daemon), respectively, with much better results. FTP is insecure. It base64-encodes your passwords, which is not strong encryption (all you need to do is base64-decode it).
If you want to stick with FTP, however, just search this site for "FTP" using the search box under the logo. There are several good articles on FTP using managed code (C#, VB.NET, and perhaps some Managed C++). There are also class libraries available for free and commercially, like that from Xceed[^].
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
thanks, but since server is an inter-network machine I'm not too worried about the security part, and also there r few other reasons, but ayways, I would really like to stick to FTP.
|
|
|
|
|
Overlooking internal security is a big mistake. Attacks can come from within, as well, and it does happen.
If you want to use FTP you will either need to implement it yourself or use a free or commercial library.
My other suggestions are still viable.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Can somebody tell me how can i use the modem port to build a 'Phone Dailer' program.
I cann't understend how to use the modem port in c# to dial a call and recive a call.
Alex
|
|
|
|
|
Click "Search comments" directly above this message board and search for "TAPI". You can also search the Internet for "C# TAPI" for relevant articles (some of which should be on this site as well).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I know there must be a better way to do this. I have a project that references another project. The second project needs to access the first projects app.config file. I could do this just by calling the file by name and accessing it as an XML file but before I go and do all that is there a better way out there somewhere that I just haven't found???
-Richard
|
|
|
|
|
|
Hey Nick, thanks for the reply but at a quick glance it looked like your manager only handled the current projects config file? Even if I missed the way to access another config file it really ends up being more code than I want to add. If I just open the xml document it will only take like 5 lines of code. I was really just interested in seeing if I had missed some .Net method along the way that would be easier or faster
-Richard
|
|
|
|
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml.Xsl;
using System.Xml;
namespace PDM.excel
{
///
/// Summary description for WebForm3.
///
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public SqlConnection conDB = new SqlConnection();
static public string[] LanguageStr =
{
"English","French","Spanish"};
static public string[] DropDownListStr =
{
"Language_DropDownList"};
protected System.Web.UI.WebControls.DropDownList Language_DropDownList;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
static public string[][] AddStrArray =
{
LanguageStr};
private void Fill_DropDownList()
{
for (int i = 0; i < DropDownListStr.Length; i++)
{
string CtrlName= DropDownListStr[i];
DropDownList MyList = (DropDownList)Page.FindControl(CtrlName);
// Populate all the checkboxes
string[] ToPopulate = AddStrArray[i];
for (int j = 0; j < ToPopulate.Length; j++)
{
MyList.Items.Add(new ListItem(ToPopulate[j], j.ToString()));
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
Fill_DropDownList();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Language_DropDownList.SelectedIndexChanged += new System.EventHandler(this.Language_DropDownList_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public DataSet DataToExcel = new DataSet();
public void BindGrid (int index)
{
conDB.ConnectionString = "data source=10.195.17.7;database=devnew;uid=bounaajak;pwd=ehsfirst;packet size=4096";
SqlDataAdapter da = new SqlDataAdapter("Select string_id, string from pdm_translations where language_id = 1 and string_id not in (select string_id from pdm_translations where language_id = " + index.ToString() + ") ", conDB);
da.Fill(DataToExcel, "DataToExcel");
Session["Tabla"] = Language_DropDownList.SelectedItem.Text;
DataToExcel.WriteXml(Server.MapPath(Session["Tabla"] + ".xml"));
DataGrid1.DataSource = DataToExcel;
DataGrid1.DataBind();
// Get row count
int nRowCount = DataToExcel.Tables["DataToExcel"].Rows.Count;
// Store Row count in Session variable
Session["RowCount"] = nRowCount;
}
private void Language_DropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
int getSelectedIndex = Language_DropDownList.SelectedIndex+1;
BindGrid(getSelectedIndex);
}
public void DataGridToExcel(DataGrid Export,HttpResponse Response)
{
// Set the Response mime type for excel
Response.ContentType = "application/vnd.ms-excel";
// Create a String Writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Button1_Click(object sender, System.EventArgs e)
{
DataGridToExcel (DataGrid1, Response);
}
}
}
|
|
|
|
|
This question belongs in the ASP.NET[^] forum.
|
|
|
|
|
Hi, iv got a program which has a networkstream for exchangin messages between a server and client......
The problem i had was that the client needs to keep checking the data stream for new messages sent from the server......as im no expert i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing..........
so i found out about the BeginRead() method which allowed me to create a seperate execution thread using a delegate and callback method......so then i could re call the beginRead() with in the thread so it continously read in formation from the stream............
Now, i dont think i understand how to implement them, as it seems to work one time round and then crashes, or reads data into the buffer that looks like \0\0\0\0\0\0\0\0\0\0\0 (what eva that means)..........i think i need some one to shed some light on these methods so i can get it to work??
cheers
sorry for the long message....
mike
|
|
|
|
|
mikeyhardingboyo wrote:
i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing
That's hardly obvious. I've done a lot of checks in programs and they've never crashed the app or the computer. So have many other developers. It's what applications do.
Understand that when you use BeginRead a read is started on another thread. The read itself is a blocking call. It does not return until there's data to actually read. If you keep calling BeginRead and there's never any data, then you've got many threads piling up, wasting memory, and doing nothing. Now all of a sudden when data arrives you've got a race condition.
Never use asynchronous Begin* methods without ever calling their corresponding End* functions. The mess you got into is the mess that will happen when you don't use asynchronous methods correctly.
If you want to know if data is available for a NetworkStream , check the NetworkStream.DataAvailable property. If you had checked the documentation - especially important if you're new to .NET development - you should've noticed this obviously named method.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I do use the DataAvailable property. My point is that when i initially call BeginRead() from my main thread, there is always data to read....thus is returns and allows the main thread to continue....
Thus in the seperate thread that the initial BeginRead call created I want it to block in that thread if there is no data to read, until data becomes available.....
Thus I have to have a form of iteration where the BeginRead() is called in the seperate thread so it can continue to check for data as long as the program is running.
As the seperate thread is executed i do call the EndRead() on each result of a BeginRead() call.
The method that the seperate thread executes looks like this...so are you telling me this is not possible?
public void myReadCallBack(IAsyncResult result)
{
NetworkStream str = (NetworkStream) result.AsyncState;
byte[] myReadBuffer = new byte[2048];
int numberOfBytesRead = 0;
numberOfBytesRead = str.EndRead(result);
myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
TokenizeCommands(myCompleteMessage);
myCompleteMessage = "";
stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
mike
|
|
|
|
|
I'm not sure I follow you completely. All you need to do is use BeginRead once while your main thread of execution continues (to update an animated "wait" graphics or something). In your AsyncCallback you call EndRead as you're doing and you've call the data.
Take a look in the .NET Framework SDK documentation at the NetworkStream.BeginRead and EndRead methods. The two together form a complete solution (i.e., a callback is created in the docs for BeginRead that is defined in the example for EndRead ). What you have is similar, but the examples - and the documentation itself - may help you discover what the problem is.
Otherwise, please post your complete solution (minus any code that actually acts on the data or updates a UI, etc., for brevity's sake).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
O, you have helped me understand these methods better. But to clear things up in what im trying to achieve..........The bottom line is I want to be able to read data as it is sent down the stream over time.....Thus my program needs to be able to constantly monitor the stream in-order to react as it were when data(messages) are there to be read off.
So in my meain theard after the TCP connection has been set up I call a method which contains the following:
if(stream.CanRead)
{
byte[] myReadBuffer = new byte[2048];
stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
After this is called the callback method is called (were the seperate thread executes)
The code in there looks like this:
public void myReadCallBack(IAsyncResult result)
{
NetworkStream str = (NetworkStream) result.AsyncState;
byte[] myReadBuffer = new byte[2048];
int numberOfBytesRead = 0;
numberOfBytesRead = str.EndRead(result);
myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
while(str.DataAvailable)
{
str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
if(stream.DataAvailable == false)
{
TokenizeCommands(myCompleteMessage);
myCompleteMessage = "";
str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream);
}
}
}
Thats the code. So im trying to achieve structure that will monitor data when it becomes available on the stream but in an iterative way so the main thread can continue to do other things but react when messages become available on the stream.
thanks for ya help, its difficult for me to explain as im a student to this, but im getting there
cheers
mIKE
|
|
|
|
|
just one more thing, when I insert buttons in my form and when I set KeyPreview to true, the left/right/up/down/space keys move the focus from controls to controls. How can I take the control of the left/right/up/down/space keys?
thanks.
There is no spoon.
|
|
|
|
|
In your OnKeyDown override in your form set KeyEventArgs.Handled to true for those keys you want to process yourself.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
Even if I put the KeyEventArgs.Handled to true , the arrow and space bar keys are not processed. The OnKeyDown handler simply do the following:
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
{<br />
Debug.WriteLine("frmMain.OnKeyDown: "+e.KeyCode.ToString());<br />
e.Handled=true;<br />
}<br />
in the OnLoad event of the main form, I just do:
private void OnLoad(object sender, System.EventArgs e)<br />
{<br />
this.MinimumSize=this.Size;<br />
this.KeyPreview=true;<br />
}
Maybe I have done something wrong somewhere???
There is no spoon.
|
|
|
|
|
You dno't need to do that in the Load event handler - just set it in the constructor. The same is true for MinimizeSize . Windows Forms will remember settings if they are not applicable yet, and will apply those when the are applicable (like after the handle is created inside OnHandleCreated ).
If those don't work for you, override (and be sure to call the base implementation) ProcessDialogKey which does handle the arrows and a few others keys in addition to the other keys.
It's all documented in the .NET Framework SDK if you look at the members for Control (from which Form indirectly derives).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
yes, overriding ProcessDialogKey works fine! all keys are processed.
Is the ProcessDialogKey the equivalent of the MFC'sPreTranslateMessage (with trapping WM_KEYDOWN )?
There is no spoon.
|
|
|
|
|
Hello gurus,
There is a strange behaviour with Windows Forms... is it me or is it normal?
Here is what's happening:
When I create an empty form that handles the KeyDown event the event is triggered.
But, when I insert a control like my own control or a buttons etc., the KeyDown event is not triggered: I never enter in the KeyDown event...
Why does this happen?? I have done something wrong?
Thanks
Best regards.
There is no spoon.
|
|
|
|