unfortunately i'm new in C# and this should be a stupid question but i need it, hope someone should help..
Based on this article: http://csharptest.net/532/using-processstart-to-capture-console-output/
i need to use the first code example but applying the second one like described by the Author. He talk about substitute the second code in the first at one point, but i really don't understand how many code to delete and/or substitute..
If it's possible i like to have a working example of that...
Thanks in advance for the help :-)
Given the description:
you would remove the event subscription and the call to process.BeginOutputReadLine()
he is talking about those two lines:
process.OutputDataReceived += (o, e) => { if (e.Data == null) mreOut.Set(); else output(e.Data); };
process.BeginOutputReadLine();
Note that the code you substitute to this should be just:
new ReadOutput(process.StandardInput, mreOut);
ReadOutput class would be outside the Run method (since in C#, methods cannot contain classes).
Related
I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA. How it does, can someonoe please explain it to me?
Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here...
I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".
Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
MessageBox.Show(s);
}
Thanks in advance!
I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.
But what is the line
Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});
telling you?
You have an Event, and you add the handler (obviously).
You have added an anonymous method as Eventhandler and created a Delegate from it.
Class.Delegate(<anoymous method, matching the siganture of the handler>);
Your anonymous method looks like this
(sometext) => {eventhandlemethod(sometext);}
This anonymous method (short lamba) consists of two parts
siganture => body
In Your case the signature gets eh "sometext". Derived from the rest of your code, this seems to be a string. And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:
private void (string sometext)
{
}
which is basically, what your eventhandlemethod looks like. You could write this lambda like this as well.
(string sometext) => {eventhandlemethod(sometext);}
This shows you the expected intput type. But you can ommit this usually.
So just as goodie: you could have written
with a "normal" method, since you already have it
Class.Event += Class.Delegate(eventhandlemethod);
with a lambda
Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));
if you have multiple input variables, put them in brackets
(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");
and of course, you can ommit the types as well leaving you:
(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");
I hope i could clarify it a bit for you.
I am trying to create a custom dialog using the MvxAppCompatDialogFragment and AlertDialog.Builder classes. I've looked through every bit of example that I could find on the internet but I'm stuck on this one part. Every single one of those working samples that I've found used the base.EnsureBindingContextSet method inside the overridden OnCreateDialog method. But every time I use that method, the compiler keeps giving me this error:
'MvxAppCompatDialogFragment<MyViewModel>' does not contain a definition for 'EnsureBindingContextSet'
So I tried to look for others who have this problem. But no matter how hard I searched, no matter the keywords that I use, I really can't find anyone who has this same problem. I hope someone can help me with this problem.
The version of MvvmCross that I use is 6.0.1. Here is the part of my code which gives me problems, in case it might help. It's still quite empty since it won't work on my first test.
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.EnsureBindingContextSet(savedInstanceState);
var view = this.BindingInflate(Resource.Layout.DurationDialogFragment, null);
var builder = new AlertDialog.Builder(Activity);
builder
.SetView(view)
.SetPositiveButton("Confirm", (s, e) => { })
.SetNegativeButton("Cancel", (s, e) => { ViewModel.CloseCommand.Execute(); });
var dialog = builder.Create();
dialog.SetCanceledOnTouchOutside(false);
return dialog;
}
Ok, I've made it work. I've found the source code on how the EnsureBindingContextSet was implemented and used it instead.
I've actually found the latest source code from Martin's github page and it looks like the EnsureBindingContextSet method is gone. Here's the link if you want to check:
https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross.Android.Support/V7.AppCompat/MvxAppCompatDialogFragment.cs
So what I did was to type:
this.EnsureBindingContextIsSet();
and used intellisense to find the proper using statement to use, which is:
using MvvmCross.Droid.Support.V4;
Also using intellisense, I think that the problem is that the new EnsureBindingContextIsSet extension method does not accept a Bundle object as a parameter anymore which resulted in the method's removal.
Seeing as I can't find any trace of this problem in the internet, I hope that this will help somebody who comes into this exact problem in the future.
I have come across the code on another question. It is an answer to the question with C# tag, but I couldn't understand some parts:
using System.Port.IO; ( ? System.IO.Ports)
System.Windows.Timers.Timer serialTimer; (There is no system.windows.timers ?)
serialPort1.DataReceived+=Tab Enter (What's the function of tab and enter here?)
serialPort1.Interval =100; ?
Would you please help me in understanding them?
The link I gave is an answer, not a topic. I am trying to understand what kind of code it is on the link, not to learn how to communicate with a port.
This code was typed by someone without compiling it, and it's full of syntactical and conceptual errors. I'll try to address the lines in your question:
using System.Port.IO; ( ? System.IO.Ports)
Yes, he probably meant to type System.IO.Ports.
System.Windows.Timers.Timer serialTimer; (There is no system.windows.timers ?)
No, there isn't. He either meant a System.Timers.Timer or a System.Windows.Forms.Timer.
serialPort1.DataReceived+=Tab Enter
(What's the function of tab and enter here?)
Those commands (though I usually TabTab) write an empty event handler for you, like:
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
}
But that is makes little sense here, as his code already shows an event handler for that, so that line should actually read:
serialPort1.DataReceived += serialPort1_DataReceived;
serialPort1.Interval =100; ?
Typo again, he probably meant to set the timer's interval through serialTimer.Interval.
So I think the lesson here is: always assume the worst when you're copypasting someone's code off the web.
i found this syntax from another article(C# How to stop animated gif from continually looping) but it seems i cant understand it. what is the meaning or purpose of s and e from
System.Drawing.ImageAnimator.Animate(txImage.Image, (s,e) => OnFrameChanged(s,e));
// start
System.Drawing.ImageAnimator.Animate(txImage.Image, (s,e) => OnFrameChanged(s,e));
// stop
System.Drawing.ImageAnimator.StopAnimate(txImage.Image, (s, e) => OnFrameChanged(s, e));
private void OnFrameChanged(object sender, EventArgs e)
{
// frame change
}
or simply can anyone explain this briefly. sorry for being stupid but im really new to programming but i really want to learn thank you
There are three basic ways in which you can write an event handler. Unfortunately the author of that code got it pretty wrong by inappropriately mixing them up. What he should have used is the original C# version 1 way:
ImageAnimator.Animate(txImage.Image, OnFrameChanged);
Which is quite straight-forward and easy to understand. Certainly the syntax you should strongly prefer in this case, it makes it very easy to call the StopAnimate() method. To answer your question, I need to show the other two ways, the ones you should not use. In C# version 2, an anonymous delegate can be used to write the code for the event handler in-place:
ImageAnimator.Animate(txImage.Image, delegate {
// Put the OnFrameChanged code here...
});
In C# version 3, lambda expressions became available to write an event handler in-place:
ImageAnimator.Animate(txImage.Image, (s, e) => {
// Put the OnFrameChanged code here...
});
Which is what you asked about. The (s, e) part of the lambda expression represent the two arguments that are passed to the event handler, s is the sender, e is the EventArgs object. Do note that you don't actually use those two arguments in your OnFrameChange code so the lambda syntax is superfluous, the anonymous delegate works just as well. Albeit that many C# programmers have stopped using them and prefer to use the lambda expression syntax everywhere. Which is fair. Even though you don't use the arguments, you must still write them to convince the compiler that your lambda is a proper substitute for the delegate. Much like you still had to write OnFrameChanged with two arguments to keep the compiler happy.
Understanding lambda expression syntax can be a bit of a speed-bump, any decent introductory book about the C# language will do a better job than I can do to explain it.
Last but not least, you'll find some hackorama code in this answer to show you how to pause an animation in a PictureBox without having to use the ImageAnimator class at all. Albeit with some odds that this just adds more questions :)
Animate and StopAnimate expect "an eventHandler object that specifies the method that is called when the animation frame changes."
You can read this as
System.Drawing.ImageAnimator.Animate(txImage.Image, new EventHandler(this.OnFrameChanged))
I'd like to add "IDLE-like functionality" to C# WinForms application, but I don't quite have an idea how to do that and couldn't find anything useful with Google.
So basically I want interactive command line interface, where user could enter some Python code and execute it (not just expressions, should be possible to define new functions).
So, where to start? Are there any good tutorials or samples available?
If my memory serves me correctly there's a chapter on embedding Python in the book Python in a Nutshell. Perhaps you can find some useful information there, but since the book is not Windows specific, you may have to adapt it yourself.
I would setyp my WinForm like this: add 2 textboxes.
1: for output. Set the multiline property of the first to true, and make it read only.
2: for input. Use KeyUp Or KeyPress Event for e.g. the return key and use the text to do what you want: add command to output textbox, launch code against the engine and capture output of interpreter
This link (http://groups.google.com/group/ironpy/browse_thread/thread/5e61a944c7c94d4b/0cbf29ec0f5fbb64?pli=1) might give some answers about launching commands agains a python engine.
IronRuby comes with a command line interpreter. Doesn't IronPython also have one? If so, the source code would be a good start :)
Oh, and if it doesn't, be sure to look at the IronRuby interpreter, because both languages are based on the DLR and are therefore similar enough to learn from both.
Thru IronPython mailing list I found IronTextBox2, which is good example how things are done. It needs a little tweaking, to get it running, but otherwise is good solution.
Here go my most generic solution:
Point cursorPoint;
int minutesIdle=0;
private bool isIdle(int minutes)
{
return minutesIdle >= minutes;
}
private void idleTimer_Tick(object sender, EventArgs e)
{
if (Cursor.Position != cursorPoint)
{
// The mouse moved since last check
minutesIdle = 0;
}
else
{
// Mouse still stoped
minutesIdle++;
}
// Save current position
cursorPoint = Cursor.Position;
}
You can setup a timer running on 60000 interval. By this way you will just know how many minutes the user don't move the mice. You can also call "isIdle" on the Tick event itself to check on each interval.