I have a C# code for a simple windows form with 3 buttons.
Button 1 calls R and plots a surface while button 2 plots a contour. If I launch the application and click on button 1, I correctly see the surface plot but then I would like to click on button 2 to open a new window with the counter plot. Unfortunately, if I try to do so the app freezes and I cannot go on. So I have added button 3 with the intention to close the R engine if it is running. The idea was to kill the R instance and reopen it when clicking on button 2. But this doesn't work either. Is there a way to fix my problem?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDotNet;
namespace mySurface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string dllPath = #"C:\Program Files\R\R-3.1.0\bin\i386\";
REngine.SetDllDirectory(dllPath);
REngine.CreateInstance("RDotNet");
REngine engine = REngine.GetInstanceFromID("RDotNet");
if (engine.IsRunning == false)
{
engine.Initialize();
}
var x = engine.Evaluate("x <- 1:100").AsNumeric();
var y = engine.Evaluate("y <- 5:105").AsNumeric();
engine.Evaluate("model = function (a, b){23.86+5.525*b-2.5725*a-6.6413*b^2-5.1862*a^2}"); //evaluate function
engine.Evaluate("z = outer(x, y ,model)");
engine.Evaluate("persp(x,y,z)");
//Console.WriteLine(x[0]);
}
public void button2_Click(object sender, EventArgs e)
{
string dllPath = #"C:\Program Files\R\R-3.1.0\bin\i386\";
REngine.SetDllDirectory(dllPath);
REngine.CreateInstance("RDotNet");
REngine engine = REngine.GetInstanceFromID("RDotNet");
if (engine.IsRunning == false)
{
engine.Initialize();
}
var x = engine.Evaluate("x <- 1:100").AsNumeric();
var y = engine.Evaluate("y <- 5:105").AsNumeric();
engine.Evaluate("model = function (a, b){23.86+5.525*b-2.5725*a-6.6413*b^2-5.1862*a^2}"); //evaluate function
engine.Evaluate("z = outer(x, y ,model)");
engine.Evaluate("contour(x,y,z, nlevels = 10)");
//Console.WriteLine(x[0]);
}
private void button3_Click(object sender, EventArgs e)
{
REngine engine = REngine.GetInstanceFromID("RDotNet");
if (engine.IsRunning == false)
{
engine.Close();
}
}
}
}
You seem to be using R.NET 1.5.5 or less. The latest version is 1.5.16 and the initialisation procedure is significantly different. The answer to a recent post on stackoverflow provides more details. Issues with attempts at multiple initialisations of R tipycally used to lead to the symptoms you describe, and the new API tries to prevent this.
Related
I have an ASP.Net page for login/register operations. What I'm trying to do is show the user name on LCD when the user logs in. The hardware I'm using is LCD Keypad Shield not just LCD if that matters. And the lovely Arduino UNO.
C# side
I tried storing the username in a char array and send to arduino one by one but the Serial.Write() gives error if I don't give a string to it. I wanted to send the whole name at once then, but Serial.Read() seems to be reading one at a time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO.Ports;
using System.Text;
using System.ComponentModel;
using System.Windows;
namespace EComm
{
public partial class Login : System.Web.UI.Page
{
SerialPort sp;
protected void Page_Load(object sender, EventArgs e)
{
sp = new SerialPort();
sp.PortName = "COM13";
sp.BaudRate = 9600;
txtPass.TextMode = TextBoxMode.Password;
}
protected void Button1_Click(object sender, EventArgs e)
{
DBDataContext db = new DBDataContext();
db.Connection.Open();
var users = from allusers in db.Users select allusers;
foreach (User _user in users)
{
if (txtUser.Text == _user.UserName.Trim())
if (txtPass.Text == _user.UserPassword.Trim())
{
Session["User"] = _user.UserName;
String outgoing = Session["User"].ToString();
for (int i = 0; i < outgoing.Length; i++)
{
sp.Open();
sp.Write(outgoing[i].ToString());
sp.Close();
}
Response.Redirect("Default.aspx");
}
}
}
Arduino side
#include <LiquidCrystal.h>
char usrName[10];
char incomingChar;
byte index=0;
LiquidCrystal lcd(4,5,6,7,8,9);
int baud = 9600;
int x=0;
int y=0;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(x,y);
Serial.begin(baud);
}
void loop()
{
while(Serial.available()>0){
if(index<10){
y=1;
incomingChar=Serial.read();
usrName[index]=incomingChar;
lcd.setCursor(x,y);
lcd.print(usrName[index]);
index++;
x++;
}
}
}
Both codes do not give any errors or warnings. When I upload the program to Arduino and run the login page, I am succesfully redirected to the page stated but nothing shows up on the LCD.
Actually this is what I see after I login to the site. I have no idea why there are white cells, they just show up when I plug in the board. But when I upload an example program for keypad shield those cells just turn to normal.
I found out that the order of the pin numbers mattered. Changed the LiquidCrystal lcd(4,5,6,7,8,9); line to LiquidCrystal lcd(8,9,4,5,6,7); I can see the desired output now and there are no white cells on startup too.
I'm creating an app in winforms c# using vs 2013.
In the app I have a textfile to which I'm saying the time in int format using a custom format from a time select dropdown list.
I then want to display what is in that text file on a selectable listview from where I can remove it from the textfile etc. I'm almost there however at the moment when I try to add the items into the listbox they do seem to add however they do not display correctly.
For example say in my text file there is
22102210
19101610
17182218
10272227
Then that is how it should be displayed in the listview as selectable ready to be deleted.
At the moment it isn't showing correctly, it's showing up as 1.. 2.. 1..
Could someone help me out and point me in the right direction as to why this might be happening? Any help much appreciated. This is my class.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chronos
{
public partial class Interface : Form
{
private string[] getTimes = System.IO.File.ReadAllLines(#"G:\Dropbox\University\Chronos\Application\Chronos\Chronos\AdminAccount\Times.txt");
public Interface()
{
InitializeComponent();
}
private void Interface_Load(object sender, EventArgs e)
{
PopulateList();
}
private void PopulateList()
{
int size = getTimes.Length;
lstTime.Items.Clear();
GetTimes();
for (int i = 0; i < size; i++)
{
lstTime.Items.Add(getTimes[i]);
}
}
private void GetTimes()
{
string[] getTimes = System.IO.File.ReadAllLines(#"G:\Dropbox\University\Chronos\Application\Chronos\Chronos\AdminAccount\Times.txt");
}
private void btnAdd_Click(object sender, EventArgs e)
{
string time = pickerTimeStart.Value.Hour.ToString() + pickerTimeStart.Value.Minute.ToString() + pickerTimeEnd.Value.Hour.ToString() + pickerTimeEnd.Value.Minute.ToString();
System.IO.File.AppendAllText(#"G:\Dropbox\University\Chronos\Application\Chronos\Chronos\AdminAccount\Times.txt", time + Environment.NewLine);
PopulateList();
MessageBox.Show("Time added", "Ok");
//PopulateList();
}
}
}
As currently written, GetTimes does nothing except read the file:
private void GetTimes()
{
// "string[]" here overrides the outer scope
string[] getTimes = System.IO.File.ReadAllLines(#"G:\Dropbox\University\Chronos\Application\Chronos\Chronos\AdminAccount\Times.txt");
}
If you change it to this, it becomes more useful:
private string[] GetTimes()
{
return File.ReadAllLines(#"G:\Dropbox\University\Chronos\Application\Chronos\Chronos\AdminAccount\Times.txt");
}
... and then PopulateList can simply become:
lstTime.Items.Clear(); //so you aren't getting a bunch of dupes
lstTime.Items.AddRange(GetTimes().Select(t => new ListViewItem(t)).ToArray());
You can also remove this line because you don't need to keep a copy of the data in the class:
private string[] getTimes = ...
Note: If you decide to keep the data source local and not work solely against the file, much of this would change.
I'm trying to capture the camera feed from an EasyCap 4 Channel USB DVR Device that i got recently
and i have bought a super Mimi Monochrome/Color Camera and connected it to the DVR Device and managed to correctly setup the device with the driver "SMI Grabber" and installed the software that comes with the Device "SuperViewer"
and i have wrote a simple windows form application that contains a PictureBox to priview the camera feed
(There is an edit in the bottom)
The Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DirectX.Capture;
namespace DirectShowWithCrossbar
{
public partial class Form1 : Form
{
private CrossbarSource crossbar;
private Filters filters;
private Capture capture;
public Form1()
{
InitializeComponent();
filters = new Filters();
capture = new Capture(filters.VideoInputDevices[0], filters.AudioInputDevices[0]);
foreach (Filter device in filters.VideoInputDevices)
{
comboBox1.Items.Add(device);
}
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
foreach (Filter device in filters.AudioInputDevices)
{
comboBox2.Items.Add(device);
}
if (comboBox2.Items.Count > 0)
comboBox2.SelectedIndex = 0;
foreach (Source source in capture.VideoSources)
{
comboBox3.Items.Add(source);
}
if (comboBox3.Items.Count > 0)
comboBox3.SelectedIndex = 0;
ShowPropertPagesInMenuStrip();
crossbar = (CrossbarSource)capture.VideoSource;
crossbar.Enabled = true;
capture.PreviewWindow = pictureBox1;
}
private void ShowPropertPagesInMenuStrip()
{
foreach (PropertyPage pro in capture.PropertyPages)
{
menusToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem(pro.Name));
}
}
private void button1_Click(object sender, EventArgs e)
{
capture.Cue();
capture.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
capture.Stop();
capture.Dispose();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
capture.VideoSource = (Source)comboBox3.SelectedItem;
}
}
}
and i got a black screen in the picture box ??
and by mistake after closing my application i ran the SuperViewer application that comes with the DVR device and then open my application then my picture box began to show me the feed from the camera, strange!!! and the feed from the original software freezes !!
DirectX.Capture Example and Sources tried with the same result http://www.codeproject.com/Articles/3566/DirectX-Capture-Class-Library
and i have also used OpenCV and Touchless and i got the same result :(
Edit:
I have been searching and found that i need to get the filter (IAMCrossbar) i think that is the problem DirectShow USB webcam changing video source and after appling the changes in this link in the DirectX.Capture Wrapper i still get the same results :(
Thanks for any help in advance Yaser
If your capture device has option, to select one from multiple input interfaces, then yes, you are right about, that you needed to use IAMCrossbar.
If you want to stick to Directshow( as others have suggested OpenCV), then I would suggest,
Try building all capturing related code in a C++/CLI dll,
Build all your UI in C#.
You can take this MP3 Player Sample Project as starting point for your dll.
For capturing, AmCap is a detailed example.
What I mean is that you need to get the capturing code out of AmCap into above MP3 Player Sample dll, and expose it to your C# application.
I am trying to call javascript using OpenWebKitSharp from WinForms with .NET 4
Here is the code I am trying to use.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WebKit;
using WebKit.Interop;
using WebKit.JSCore;
using webkitForm.Properties;
namespace webkitForm
{
public partial class Form1 : Form
{
WebKitBrowser webKitSharpBrowser = new WebKitBrowser();
public Form1()
{
InitializeComponent();
this.Controls.Add(webKitSharpBrowser);
webKitSharpBrowser.Width = 600;
webKitSharpBrowser.Height = 400;
}
private void button1_Click(object sender, EventArgs e)
{
webKitSharpBrowser.Preferences.AllowPlugins = true;
webKitSharpBrowser.UseJavaScript = true;
webKitSharpBrowser.Navigate("http://sandbox.icontact.com");
webKitSharpBrowser.GetScriptManager.EvaluateScript("alert('An alert from C#!');"); //Call javascript?
}
}
}
I can't get javascript to fire for anything... there must be something that I am missing.
Thanks in advance.
Well, it seems like it can't be done the way you want to:
if you are using .NET 4, calling a function is possible by using:
<webkitbrowser>.GetScriptManager.CallFunction("name", new Object[] { arg1, arg2, ...});
If you want to use .NET 2 you can use:
<webkitbrowser>.StringByEvaluatingJavaScriptFromString("name(arguments)")
- Open Webkit Sharp Issues
I tested your code, it IS working, but calling the alert function only triggers an event (WebKitBrowser.ShowJavaScriptAlertPanel), you are responsible for handling that event and showing a message or updating a label, or anything else.
For example:
Browser.ShowJavaScriptAlertPanel += Browser_ShowJavaScriptAlertPanel;
and then handle the event:
private void Browser_ShowJavaScriptAlertPanel(object sender, WebKit.ShowJavaScriptAlertPanelEventArgs e)
{
MessageBox.Show(e.Message);
}
I am working on a project using Visual Studio(c#). I want to create a startup form when i install my application with a progress bar. And after progress bar completed this form should be hide and a new form should be open. can u help me about this problem?
Edit:
I've just made a sample application trying to use exactly the code that you've specified. It worked fine besides just one tweak:
Form1().Show(); should be new Form1().Show();
The only way this code does not execute is if you forgot to set timer1 to enabled state in design view which causes the code to never fire up.
Are you sure the code is firing up? have you done a break-point on this piece of code?
On a sidenote: timer1 is not on a separate thread so you don't need to use Invoke (you can see if you actually need it by looking InvokeRequired property of a control)
Suggested improvement: if you are not going to use Form2 again and judging from your code, it is likely you won't; perhaps you should call Close() on Form2 instead of Hide() and release the resources. I've had times when my application kept running in background because I hid the form but never closed it and application was on "exit when last window closes" which never happened.
So to be sure, here is the final code that does work on my machine:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//enable timer1 here or in designer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
//disable timer1 first thing, otherwise it can end up ticking
//multiple times before you've had a chance to disable it
//if the timespan is really short
timer1.Enabled = false;
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
Hide();
//create a new Form1 and then show it
new Form1().Show();
}
}
}
Create your form and add your progress bar
Set up event handlers on the parts of the form that should effect the progress bar
Update the progree bar to reflect the amount of work that is done
When the form is complete close it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
this.Hide();
Form1().Show();
timer1.Enabled = false;
}
}
}