Directx window will not load when debugged - c#

I'm trying to create a window and initializing a device to the window but every time I run the program the window does not load. I am doing this in visual studio 2015 for a windows forms application.
here the form1.cs:
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
namespace DirectXTutorial2
{
public partial class Form1 : Form
{
private Device device;
public Form1()
{
InitializeComponent();
InitializeDevice();
}
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}
private void Render()
{
device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 0, 1);
device.Present();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Render();
}
}
}
Does anyone know of a solution to this?
I don't know if it helps but I am running windows 10, 64 bit, Directx 2010, and I have been added my references before.

okay, i have found a solution to my problem. DirectX for managed code is no longer supported by the devs. the latest framework that works for DirectX is .net Framework 3.5. managed code also does not support 64 bit. to fix this problem go to project properties. in the application tab find target framework and change your .net framework to 3.5 or less. next go to the build tab and find platform target, change this to x86.

First think use try catch in your application, because there is alot of stuff happening in DirectX, so you need to confirm the step, where you are getting error.
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
}
catch (DirectXException)
{
return false;
}
Try to use different device type like DeviceType.Software.
After clearing your device add begin and end scene
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
device.BeginScene();
device.EndScene();
device.Present();
And try calling your 'Render; function in 'Main' function

Related

Iconics Simulator OPC Serve and C#

I'm developing a C# Application for OPC Access via "OPC DA Automation Wrapper 2.02"
For testing on my WIN8 machine I'm using "Iconics Simulator OPC Server 3.12"
I have 4 boolean OPCItems with different names configured on the server.
Connecting, reading the OPCItems works fine.
But:
If I write to ONE of them, the monitor (OPC DataSpy 9.01) and other tools are showing that ALL OPCItems have changed. The Change Event appears for all OPCItems
To check this behaviour, I've written the same application with VisualBasic 6 - same result!
What is wrong, my code or the Simulator Server?
using System;
using System.Windows.Forms;
using OPCAutomation;
namespace opc {
public partial class Form1 : Form {
OPCGroups opcGroups;
OPCGroup opcGroup;
bool aliveToggle;
Timer aliveTimer;
public Form1() {
InitializeComponent();
OPCServer opcServer = new OPCServer();
opcServer.Connect("Iconics.SimulatorOPCDA.2");
opcGroup = opcServer.OPCGroups.Add("Gruppe1");
opcGroup.OPCItems.AddItem("BOOL_1", 1);
opcGroup.OPCItems.AddItem("BOOL_2", 2);
opcGroup.OPCItems.AddItem("BOOL_3", 3);
opcGroup.OPCItems.AddItem("BOOL_4", 4);
opcGroup.UpdateRate = 10;
opcGroup.IsActive = true;
opcGroup.IsSubscribed = true;
aliveTimer = new Timer();
aliveTimer.Tick += new EventHandler(sendAlive);
aliveTimer.Interval=1000;
aliveTimer.Start();
}
private void sendAlive(object sender, EventArgs e) {
aliveToggle=!aliveToggle;
opcGroup.OPCItems.Item(2).Write(aliveToggle);
}
}
}
Easy fix is whenever adding a tag that needs to be event notified independently it should have it's own group, because unfortunately change events are fired on a group basis instead of a tag basis.

Application crashes after 15min, WCE 6.0 CF 3.5 Motorola MC3190

I created application for WCE (RF terminal Motorola MC3190). My problem is that app after 15 minutes of running crashed without some error message. No exception catch in code is possible. No matter if the I working with application or not. After crashes if I try close directory with exe file (or anything else) then Windows freezes and after couple of minutes RF is a hard-reseted.
Application communicate via TCP sockets with other application. Based on XML data from other app are created Windows controls and events. All is OK for first 15min (+/- 30sec). For this time I not using motorola EDMK library.
Some ideas for a solution?
Thanks!
NEWS: When I removed this code app stopped crashes. Concretely remove Thread.Sleep(1000); has effect.
//using Symbol.Keyboard;
delegate void setBoolAlpha(bool alpha);
public void checkAlpha()
{
KeyPad KP = new KeyPad();
bool alpha = KP.AlphaMode;
showAlpha(alpha);
Thread.Sleep(1000);
checkAlpha();
}
public void showAlpha(bool alpha)
{
if (this.abc.InvokeRequired)
{
setBoolAlpha d = new setBoolAlpha(showAlpha);
this.Invoke(d, new object[] { alpha });
}
else
{
this.abc.Visible = alpha; //abc = Control panel
}
}
A little change was necessary. CheckAlpha() is running as "IsBackground" thread.
delegate void setBoolAlpha(bool alpha);
public void checkAlpha()
{
while(true)
{
KeyPad KP = new KeyPad();
bool alpha = KP.AlphaMode;
showAlpha(alpha);
Thread.Sleep(500);
//checkAlpha();
}
}
}

Capture Camera Feed from EasyCap 4ch USB DVR Device using DirectShow and DirectX.Capture C#

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.

reference for webkit sharp not exist in monodevelop

ok i create new project with monodevelp and add this code :
using System;
using Gtk;
using GtkSharp;
using WebKit;
namespace browserapp
{
public class browser
{
public static void Main () {
Application.Init ();
Window window = new Window ("a browser in 12 lines...");
window.Destroyed += delegate (object sender, EventArgs e) {
Application.Quit ();
};
ScrolledWindow scrollWindow = new ScrolledWindow ();
WebView webView = new WebView ();
webView.Open ("http://mono-project.com");
scrollWindow.Add (webView);
window.Add (scrollWindow);
window.ShowAll ();
Application.Run ();
}
}
}
and i add all the reference that code need except for :
using WebKit;
and i search in monodevelop library reference and couldn't find webkit_sharp
so i searched in Google to find the library and i found this :
https://github.com/mono/webkit-sharp
but i couldn't manage how to build it in windows and extract the right dll file for reference so pls if someone can tell me how to build it in windows and i'm using mono develop the windows version thanks
You could download the suse rpm here: http://download.opensuse.org/repositories/openSUSE:/Factory/standard/noarch/webkit-sharp-0.3-7.2.noarch.rpm
It's just an archive. You could extract it and pull the dlls out. I'm not sure what you're trying to do would work though.

CantStartSingleInstanceException when trying to start second instance

I'm trying to build a single instance application using the approach outlined here.
The reason I tried going with that solution is that I need to pass on the commandlines from the second attempt to start the app to the first instance, and this seemed the easiest way to accomplish that.
OS flavours I need to support:
Windows XP SP3
Windows 7 32 Bit
Windows 7 64 Bit
I've got it working on all three OS versions, however, I have one machine with Windows 7 32Bit where this crashes with a CantStartSingleInstanceException.
Here's the code:
SingleInstanceController.cs:
using System;
using Microsoft.VisualBasic.ApplicationServices;
namespace SingleInstanceTest
{
public class SingleInstanceController : WindowsFormsApplicationBase
{
public SingleInstanceController()
{
IsSingleInstance = true;
}
protected override void OnCreateMainForm()
{
base.OnCreateMainForm();
Form1 f = new Form1();
MainForm = f;
// process first command line
f.SetCommandLine(Environment.GetCommandLineArgs());
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
base.OnStartupNextInstance(eventArgs);
Form1 f = MainForm as Form1;
// process subsequent command lines
f.SetCommandLine(eventArgs.CommandLine);
}
}
}
Program.cs:
using System;
using System.Windows.Forms;
namespace SingleInstanceTest
{
static class Program
{
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceController si = new SingleInstanceController();
// This triggers the crash on one machine when starting the
// app for the second time
si.Run(Environment.GetCommandLineArgs());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
// this is triggered with CantStartSingleInstanceException
MessageBox.Show(e.ToString(),"ThreadException");
MessageBox.Show(e.Exception.ToString(), "ThreadException");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ToString(), "UnhandledException");
MessageBox.Show(e.ExceptionObject.ToString(), "UnhandledException");
}
}
}
For testing purposes, the form is just a plain form containing a listbox that displays the command line arguments.
Any ideas why this doesn't work on that one machine? I've been fiddling with this for two days now and can't figure it out ...
I ran into the same Problem, but I don't think it has something to do with Windows 7 or 32bit. In my case it turned out, it was a performance issue. Unfortunately, I can't find the source code of WindowsFormsApplicationBase
but it uses network to communicate with the main application, so there might be timeouts involved. It is especially bad, when the main application has to do a lot of network I/O anyways. When the main application does not answer the call to Run fast enough, this exception is thrown.
I solved it by fine tuning the processes, tasks ans threads, so the call gets answered first.
And getting rid of WindowsFormsApplicationBase by using mutexes and proper IPC, where I can actually not only choose the time-out, but also catch any exceptions! Actually, for some sorts of IPC, there isn't even a need for a mutex.
See this fine article for more on that topic:
https://www.codeproject.com/Articles/1089841/SingleInstance-NET
The two dirtymost workarounds I choose:
Catching the exception and trying again a couple of milliseconds later.
After some testing, spawning a new thread with a low priority in the base application seems to be a good idea (at least it was in my scenario).
public void SetCommandLineInThread(string[] args) {
new Thread(() => {
SetCommandLine(args);
}) { IsBackground = true, Priority = ThreadPriority.Lowest }.Start();
}
Note, that I make a copy of the command line args as soon as possible.
var args = e.CommandLine.ToArray();

Categories