Click here to Skip to main content
       

C#

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralRe: nested try catch blockmember000mann00023 Jan '13 - 7:11 
if you would show in code that would be more helpful.
thanks
GeneralRe: nested try catch blockmemberPIEBALDconsult23 Jan '13 - 7:12 
Which is why I won't -- please read some documentation.
AnswerRe: nested try catch blockmemberPaulo Zemek23 Jan '13 - 7:23 
try
{
  try
  {
    throw new Exception();
  }
  catch(Exception exception)
  {
    throw; // this rethrows the caugh exception.
  }
}
catch(Exception exception2)
{
  // here you will get the same exception as you got in the
  // other catch.
  // Note that I used throw; to rethrow the exception. If on the
  // other catch I did a throw exception; I would end-up replacing
  // the stored callstack (that's usually a bad thing).
}
 
Note: You can also avoid catching the exception by putting a more specific type in the first catch (like IOException instead of Exception).
GeneralRe: nested try catch blockmember000mann00023 Jan '13 - 7:25 
thanks bro..
QuestionClient is not connecting to the servermemberSkytten23 Jan '13 - 5:33 
Hey folks!! Hope you can help me with my client code.
Först I debugg the Server wich is an consoleapplication and the server starts, secondly I debugg the Client, I put a nickname value to the nickNameTextBox and click on Connect (button2) the compiler stops at this code line
serverStream.Write(outStream, 0, outStream.Length);
It says that the objectreference has not given an instans of a object!!
 
Note!!!! The code is working fine with VS 2008 but Im currently trying to work with it on VS 2010 but it wont run like it should
 
This is the Client
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        string readData = null;
 
         public Form1()
        {
            InitializeComponent();
            
        }
      
        //Connect to the Server
         private void button2_Click(object sender, EventArgs e)
         {
         //    clientSocket.Connect("127.0.0.1", 8888);
         //    serverStream = clientSocket.GetStream();

 
             byte[] outStream = System.Text.Encoding.ASCII.GetBytes(nickNameTextBox.Text + "$");
             serverStream.Write(outStream, 0, outStream.Length);
             serverStream.Flush();
 
         }
 
         private void button1_Click(object sender, EventArgs e)
         {
            
             readData = "Conected to Chat Server ...";
             msg();
             clientSocket.Connect("127.0.0.1", 8888);
             serverStream = clientSocket.GetStream();
 
             byte[] outStream = System.Text.Encoding.ASCII.GetBytes(sendTextBox.Text + "$");
             serverStream.Write(outStream, 0, outStream.Length);
             serverStream.Flush();
 
             Thread ctThread = new Thread(getMessage);
             ctThread.Start();
 
         }
         private void getMessage()
        {
            while (true)
            {
                serverStream = clientSocket.GetStream();
                int buffSize = 0;
                byte[] inStream = new byte[10025];
                buffSize = clientSocket.ReceiveBufferSize;
                serverStream.Read(inStream, 0, buffSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        }
 
        private void msg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
                loggTextBox.Text = loggTextBox.Text + Environment.NewLine + " >> " + readData;
        }

AnswerRe: Client is not connecting to the servermvpOriginalGriff23 Jan '13 - 5:41 
Why have you commented out the lines
         //    clientSocket.Connect("127.0.0.1", 8888);
         //    serverStream = clientSocket.GetStream();
from the button 2 handler - unless you have pressed Button1 first, the connection has not been established and it will likely throw an exception - probably the one you are complaining about.
 
Personally, I would have a "Connect" method, which checked if you are connected and does so if not - and call it from both click events. (I would probably also make it return the stream instead of using a class level variable directly in the two event handlers).
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

GeneralRe: Client is not connecting to the servermemberSkytten23 Jan '13 - 5:55 
I put back the
 clientSocket.Connect("127.0.0.1", 8888); 
serverStream = clientSocket.GetStream();
and it worked it connects now but the problem is with the button1 which send the text to the server, it stops at this line
             clientSocket.Connect("127.0.0.1", 8888);

GeneralRe: Client is not connecting to the servermvpOriginalGriff23 Jan '13 - 6:07 
That's why I suggested that you had a routine which checked if you were connected - you don't need (or want) to connect if you are connected already!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

GeneralRe: Client is not connecting to the servermemberSkytten23 Jan '13 - 6:09 
Hope Im not requestion to much but would you like to write that if-statemant?
GeneralRe: Client is not connecting to the servermvpOriginalGriff23 Jan '13 - 6:20 
I dunno - it's soooo difficult....
if (!clientSocket.Connected)
   {
   ...
   }
Laugh | :laugh:
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

GeneralRe: Client is not connecting to the servermemberSkytten23 Jan '13 - 6:26 
I changed it to
 private void button1_Click(object sender, EventArgs e)
         {
 
             if (serverStream == null)
             {
                 MessageBox.Show("Please connect to a server.");
                 return;
             }
            
             readData = "Conected to Chat Server ...";
             msg();
             clientSocket.Connect("127.0.0.1", 8888);
             serverStream = clientSocket.GetStream();
 
             byte[] outStream = System.Text.Encoding.ASCII.GetBytes(sendTextBox.Text + "$");
             serverStream.Write(outStream, 0, outStream.Length);
             serverStream.Flush();
 
             Thread ctThread = new Thread(getMessage);
             ctThread.Start();
 
         }
 
But still the same
GeneralRe: Client is not connecting to the servermvpOriginalGriff23 Jan '13 - 8:04 
Well yes - what did you expect.
The logic I suggested is not what you did:
if (not connected)
    connect
TalkToServer
What you did was
if (not connected)
    Complain
else
    {
    connect
    TalkToServer
    }
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

GeneralRe: Client is not connecting to the servermemberSkytten23 Jan '13 - 10:03 
Thank you mate I appreciate your help, I changed it to this code,
the reason I commented out
//clientSocket.Connect("127.0.0.1", 8888);
because the compiler stops there when I debugg telling me that a socket is already connected, now the compiler runs withour errors but the server consoleAppplication doesn't receive any msg at all when I send it from sendTextBox..
 
private void button1_Click(object sender, EventArgs e)
         {
 
             if (serverStream == null)
             {
                 MessageBox.Show("Please connect to a server.");
                 return;
             }
             else
             {
 
                 readData = "Conected to Chat Server ...";
                 msg();
                 //clientSocket.Connect("127.0.0.1", 8888);
                 serverStream = clientSocket.GetStream();
 
                 byte[] outStream = System.Text.Encoding.ASCII.GetBytes(sendTextBox.Text + "$");
                 serverStream.Write(outStream, 0, outStream.Length);
                 serverStream.Flush();
 
                 Thread ctThread = new Thread(getMessage);
                 ctThread.Start();
             }
 
         }
 
The compiler stops on this code under the getMessage method
serverStream.Read(inStream, 0, buffSize);

GeneralRe: Client is not connecting to the servermvpOriginalGriff24 Jan '13 - 0:04 
Skytten wrote:
The compiler stops on this code under the getMessage method

The compiler? What error message does it give?
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

GeneralRe: Client is not connecting to the servermemberSkytten24 Jan '13 - 12:57 
"Failed to read data from the transport connection: An existing connection was forced to close by the remote host." It havens because I closed the Server... Anyway this project is driving me crazy man I can't see why it's not working and the Server doesn't get and show the string on the server prompt from the Client
QuestionC# parametersmemberclassy_dog23 Jan '13 - 4:37 
I am going to write a C# 2010 application as a console application to start with. Eventually I will change the application to compile as a windows application so I do not get the dos pop windows. When this application goes into production, it will be executed by a windows scheduler. My question is the best way to pass parameters to this application.
 
Thus can you show me the following:
1. Code on how to accept values from parameters?
2. When the application runs as an executable, can you show me in code how to pass the values to this executable?
AnswerRe: C# parametersmvpRichard MacCutchan23 Jan '13 - 5:04 
How to access command line parameters[^]. To execute a program with parameters you use a command line of the form:
program_name parameter1 parameter2 "a string parameter with spaces" parametr4 ...

GeneralRe: C# parametersmemberPIEBALDconsult23 Jan '13 - 5:29 
There are several articles on here regarding parsing command line parameters. Here's one of the more recent ones:
Implementing command execution in a console application[^]
 

However, I caution you against expecting a Scheduled Task to be able to run a Windows application or anything that needs user interaction.
AnswerRe: C# parametersmemberjschell23 Jan '13 - 8:59 
I would suggest that you are also likely to need a way to report on failures. So you might want to investigate that as well.
AnswerRe: C# parametersmemberBC @ CV23 Jan '13 - 10:17 
I see the others have you given you plenty to read regarding the parsing of parameters, so I will just add that it would be best to do ONLY the parsing of parameters and possibly writing of the help text in the Console application and put all actual program logic in a dll. That way you can plug any interface you want into your app: console window, windows UI, WPF, Web Service or whatever.
QuestionC# filememberclassy_dog23 Jan '13 - 4:31 
In a C# 2008 desktop application, I want to be able to check for a directory folder existing in a specified location. The directory path will look like C:\\main_folder\mm_yyyy. The mm_yyyy stands for the month and year.
Thus can you should be code on how to check if a folder actually exists?
AnswerRe: C# filememberdeflinek23 Jan '13 - 4:37 
Take a look at http://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx[^]
--
"My software never has bugs. It just develops random features."

GeneralRe: C# filememberPIEBALDconsult23 Jan '13 - 5:10 
I strongly recommend YYYY_MM instead.
GeneralRe: C# filemvpOriginalGriff23 Jan '13 - 5:36 
I strongly agree!
 
To the OP: If you store file files as MM_yyyy, then the sort order is useless to you, no matter what you chose to sort by - because all the Januaries come before all the Februaries, and the years are then sorted within the month.
If you store by year first, you can retrieve the file names in "oldest first" or "newest first" without any additional processing.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

QuestionUse axFrameControl to open the ppt encountered the error [modified]memberJason Fang_22 Jan '13 - 21:55 
Code:
this.axFraCtrlShowPPT.Open("e:\\1.ppt",true,"PowerPoint.Show",null,null);//open file
 this.axFraCtrlShowPPT.PPTPlay();//Try to read or write protected memory this usually indicating other memory has been damaged
I want to open a ppt in the axFrameControl,and play the ppt after opening it,but it encoutered an error when it was playing ppt.
Can you give me the correct answer?

modified 23 Jan '13 - 4:06.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 25 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid