|
process.WaitForExit(10 * 60 * 1000); --> This starts a wait that remains waiting 10 minuts the exit of the process
Thread.Sleep(200); --> When the wait is finised because the timer expires or the process ends we wait 200 milliseconds
try
{
process.Kill(); --> Try to kill the process manually
process.WaitForExit(5000); --> This wait 5 seconds to check if the process has been killed
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); --> If some problem happen in the try loop it shows the message error using a MessageBox
}
I hope this explanation line by line helps you.
|
|
|
|
|
Hey, Im a C# nooblord. This problem probably has a very easy solution, but im unable tot find out what i did wrong. Could you help me fix it please ? Also could you write the code i need to fix this? Thank you .
http://imgur.com/vqHjMjE[^]
|
|
|
|
|
I guess your intent is to write
if (cislo > nahodne)
instead.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Oh. Thanks
|
|
|
|
|
Screenshot of code are bad form. Not everyone can see the sites you post images to so don't do it.
Copy and paste code into the post and surround the code inside HTML PRE tags.
|
|
|
|
|
That is because the operator > (greater than) cannot be applied to these types. double is a built-in type where as System.Random (class) is a custom type. .NET has no idea how to resolve it.
You wanted to write the condition as,
if(cislo > nahodne) { }
int and double can be somehow checked against each other to resolve the expression.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
In C# assuming I have 2 forms: form1 and form2. I open Form1 drag Statusbar Control into Form1 with the following declaration:
public static void LogStatusBar(string status)
{
try
{
statusBar1.Text = "Thông báo: " + status;
}
catch (Exception ex)
{
}
}
private void form1_Load(object sender, EventArgs e)
{
....
LogStatusBar(Message);
....
}
Form1 run before and open form2, form2 will be used together with function LogStatusBar Form1 (...) I have declared above, and in this form2. I declare the following code:
try
{
....
}
catch (Exception ex)
{
form1.LogStatusBar(ex.Message);
}
My question posed here as Rebuild Solution in statusBar1.Text error will not be public static because of it, according to my understanding of this, verbatim error: Error 1 An object reference is required for the nonstatic field, method, or property 'Vidu.form1.statusBar1'. I want to declare the control statusBar1 was dragging this into the public static Form1 themselves to declare how the band through household property.
|
|
|
|
|
static methods are unrelated to any particular instance of the containing class - so they don't have a this reference to address any non-static class elements (such as field, properties, and methods) with.
Since statusBar will be a class control, it is specific to a particular instance of the containing form, because you will normally have a different status bar for each instance. As a result, you can't access the status bar within any static method.
There are ways round this, but the best is not to try. Instead, create a event in Form2 which Form1 handles, and pass the information either via a propety in teh Form2 instance, or via a custom event args.
See here: Transferring information between two forms, Part 2: Child to Parent[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I am trying to create some CircularStream and / or CircularTextWriter.
When It reach its limit it should continue writing from the start again.
I got 2 problems:
- if I use UTF16 encoding I am not sure whether a given byte is in the middle of a character or not
- even if I use UTF32 I can't cut the stream every 4 bytes as damn unicode characters might need to come in pair sometimes.
basically.. how I like to know... where to split a char[] at a legal place...
[EDIT] kind of fixed...
I create a Stream (view) of the beginning of my CircularStream and read it with Stream Writer and do some educated guess with the StringInfo class
[EDIT2]
Because splitting on char is too hard (2 char might work together depending on the whole line) I decided to split only at line returns!
For the record here is how I read char from the stream:
const int bit00 = 0x00;
const int bit10 = 0x80;
const int bit20 = 0xC0;
const int bit30 = 0xE0;
const int bit40 = 0xF0;
const int bit50 = 0xF8;
const int bit07 = 0x7F;
const int bit06 = 0x3F;
const int bit05 = 0x1F;
const int bit04 = 0x0F;
const int bit03 = 0x07;
public static int ReadUTF8Char(Stream s)
{
int c = s.ReadByte();
if (c == -1)
{
return -1;
}
else if ((c & bit10) == bit00)
{
return c;
}
else if ((c & bit30) == bit20)
{
var c2 = s.ReadByte();
return c & bit05 << 6 | c2 & bit06;
}
else if ((c & bit40) == bit30)
{
var c2 = s.ReadByte();
var c3 = s.ReadByte();
return c & bit04 << 12 | c2 & bit06 << 6 | c3 & bit06;
}
else if ((c & bit50) == bit40)
{
var c2 = s.ReadByte();
var c3 = s.ReadByte();
var c4 = s.ReadByte();
return c & bit03 << 18 | c2 & bit06 << 12 | c3 & bit06 << 6 | c2 & bit06;
}
else
{
return c;
}
}
modified 5-Jun-15 22:22pm.
|
|
|
|
|
The problem is not the writer, but the reader. When reading back you need some way to resynchronise from the middle of a line, to recognise a valid line end. The CR/LF sequence may not be good enough because other UTF sequences could be mistaken for them. Perhaps some other sequence of bytes could be used that cannot be mistaken for anything else.
|
|
|
|
|
In fact you gave me an idea, when the buffer is full and I start overwriting, I can free space until the next line return (which is reasonable it's for program logs, lots of line return).
that's a good work around!
I might do that, since the next alternative I just found:
StringInfo.GetNextTextElement(String, Int32)
needs a String ... whereas I am working with Stream ....
|
|
|
|
|
How about keeping tabs on the byte-offsets of the inserted strings?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
mmm.... I will use a normal TextWriter wrapping an (almost complete now) CircularStream.
there is no really good place for that...
Never mind the CircularStream has a Reclaim delegate property, where I can use a StreamReader on the beginning of the stream, should be acceptable!....
Thanks!
|
|
|
|
|
I have a post from a few days ago mentioning async TCP clients. It has been causing me nothing but trouble.
So i come to you asking what the best method of achieving what i am trying to do is.
I have an unidentified number of clients, sometimes 3 sometimes 5 sometimes 50 that are not all always connected.
The server needs to send information to all of them occasionally. no data needs to be received by the server. It is a very small amount of data that needs to reach all the clients as quickly as possible.
Using Async i keep running into the error "A request to send or receive data was disallowed because the socket is not connected" on the client side after a forced close of the software, or logging off without closing the application properly.
Is there any way to open this connection on demand? Without knowing which clients are online?
|
|
|
|
|
It sounds like you need to use a try/catch block to catch the exception and handle it properly. You can try opening the connection when the error is received.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Good idea, thank you I will let you know how it goes
|
|
|
|
|
I have a WPF app and I'm attempting to switch from my MSSQL dev database to a DB2 database the app will actually run against.
I have the DB2 drivers installed on my computer and I can connect to the server via WinSQL.
I can't figure out how to set up the provider in my app.config to work though.
I'd appreciate some info on the steps required or exactly what I need to configure to get this thing moving.
|
|
|
|
|
|
So, I recently started working with c# without prior coding backround. I am now trying to write a program wich compiles a pdf of different textboxes that the user provides.
I have managed to code the program to check if the boxes are empty, but I am having a hard time trying to make a "ErrorState" if the box is empty.
So here is how it is supposed to go:
User enters text to fields -> Presses btnSave -> Program calls for a method that checks if textboxes are empty, If empty then put program to a "errorstate" and don't call for the next method that saves the pdf. Reset "errorstate" next time btnSave is pressed and check the textboxes again, if all is ok then continue with program.
My problem is getting the "errorstate" out of the method, I get a error: Use of unassigned local variable errorState with my current code. What am I missing, and how wrong Am I using methods?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Text.RegularExpressions;
namespace CV_maker
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private bool ErrorCheck(bool errorState)
{
bool isError;
if(txtboxName.Text.Trim().Length == 0)
{ MessageBox.Show("Anna nimesi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
if (txtboxBday.Text.Trim().Length == 0)
{ MessageBox.Show("Anna Syntymäaikasi", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
if (txtboxPhone.Text.Trim().Length == 0)
{ MessageBox.Show("Anna Puhelinnumerosi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
if (txtboxAddress.Text.Trim().Length == 0)
{ MessageBox.Show("Anna Osoitteesi", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
if (txtboxEmail.Text.Trim().Length == 0)
{ MessageBox.Show("Anna S-postisi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else
{
isError = false;
}
return isError;
}
private void btnAddimage_Click(object sender, RoutedEventArgs e)
{
addImage();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
bool ISOK;
bool errorState;
ISOK = ErrorCheck(errorState);
if (ISOK == true)
{
MessageBox.Show("ERROR I WILL SHOW YOU THIS MESSAGE BEFORE REAL CODE");
}
}
void addImage()
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
PictureOmakuva.Source = new BitmapImage(new Uri(op.FileName));
}
return;
}
void savepdf()
{
string OwnName;
OwnName = txtboxName.Text;
FileStream fs = new FileStream(OwnName + " Curriculum Vitae.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new iTextSharp.text.Paragraph("Tämä on testi"));
doc.Close();
}
}
}
|
|
|
|
|
That's like starting to translate a text, without knowing the language you are translating to; it is possible, but rather frustrating to do.
I'd recommend to learn at least the basics before attempting a real task.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You are calling the ErrorCheck method with a variable which has not been initialised, but more importantly is never used. Try changing your code to:
public partial class MainWindow : Window
{
bool errorState = false;
private bool ErrorCheck()
{
return isError;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
errorState = ErrorCheck();
if (errorState)
{
}
else
{
}
Also your tests look to be the wrong way round. The ErrorCheck method returns true if the information in the text boxes is not valid. But the btnSave_Click method calls the return value ISOK . But if ISOK is true then that indicates an error, which means you have to go back and try again. You could also use the String.IsNullOrEmpty Method[^] on your textboxes to check if they contain any valid data.
|
|
|
|
|
Thank you Richard, Such a stupid mistake to make.
Now the compiled file executes properly, But I am pretty sure that it do not retrieve anything from the Errorcheck method.
I coded this in the savePDF button (and making your changes from above). It do not matter if i leave a textbox empty, the program will always display the "All COOL" message.
Is there somthing wrong with the way errorstate = ErrorCheck(); tries to retrieve the bool false or true from the method?
private void btnSave_Click(object sender, RoutedEventArgs e)
{
errorstate = ErrorCheck();
if (errorstate)
{
MessageBox.Show("ERROR I WILL SHOW YOU THIS MESSAGE BEFORE REAL CODE");
}
else
{
MessageBox.Show("ALL COOL");
}
|
|
|
|
|
No, nothing wrong with that, the error is in the ErrorCheck method, which I missed before. Your tests will set isError to true if any of the text boxes are not valid. However, the last test is a problem, if txtboxEmail contains some text then it goes to the else clause and sets isError to false , thus negating any previous test which set it to true . You should either change it to use else if clauses like this:
bool isError;
if(txtboxName.Text.Trim().Length == 0)
{
MessageBox.Show("Anna nimesi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else if (txtboxBday.Text.Trim().Length == 0)
{
MessageBox.Show("Anna Syntymäaikasi", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else if (txtboxPhone.Text.Trim().Length == 0)
{
MessageBox.Show("Anna Puhelinnumerosi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else if (txtboxAddress.Text.Trim().Length == 0)
{
MessageBox.Show("Anna Osoitteesi", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else if (txtboxEmail.Text.Trim().Length == 0)
{
MessageBox.Show("Anna S-postisi!", "Seis!", MessageBoxButton.OK, MessageBoxImage.Error);
isError = true;
}
else
{
isError = false;
}
return isError;
, or set isError to false at the beginning of the method, and remove the final else clause.
|
|
|
|
|
Thank you.
I Got it working!
Thank you a million times, I will look more into methods and if statments on my own now.
Clearly some crucial pieces of knowleadge lacking in those departments
I am just very bad at following tutorials, I like trying achieve somthing usefull and tackle problems as they come.
Have a great day!
|
|
|
|
|