Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am attempting to create a simple install-able form application using Visual Studio Community 2015.
I have followed this walkthrough: https://msdn.microsoft.com/library/k3bb4tfd%28v=vs.100%29.aspx
As the setup project templates were not originally present, I used https://visualstudiogallery.msdn.microsoft.com/f1cc3f3e-c300-40a7-8797-c509fb8933b9 to add them.
I am able to successfully make the installer, as well as successfully install the app. However, when I try to run the application (by going to its installed folder), windows explorer just sits there, like it is trying to do something, but never does. I end up needing to restart the computer in order to cancel this process that never starts.
The same behavior is seen when I try to run the built application on its own, without the installer, running the executable generated from building the actual application. (diving down into the bin/ of the project).
I have seen similar behavior from new, indev programs before (https://github.com/Storj/driveshare-gui/releases), but would not know if the issues are necessarily related.
The app itself runs fine when being debugged by Visual Studio. I used the Visual C# "Windows Forms Application" template to create the original app.
Please let me know if there is anything you would need to see, I would not know.
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Parking_Variable_Editor {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Constructor & onload from MainForm.cs (the only form in the project)
public MainForm() {
InitializeComponent();
}
private void onShown(object sender, EventArgs e) {
refreshForm();
}
Update:
I have gotten the project to run without debugging. I did this by instead refreshing the form when it is shown, as onLoad seemed to try to do it before things were initialized. Now, however, the same issue is only happening for the installed application.
I've seen this problem before -- and others have too:
C# app runs with debugging, but not without
Here are some ideas.
Most likely when the app starts it is failing when attempting instantiate your main form. If you run in Visual Studio, it may self-heal the problems you've created and make you think it'll run by double-clicking the EXE.
Look in the constructor of your main Windows Form.
Look in the OnLoad of your windows form.
Set a breakpoint in the Program.cs file where the your main form is instantiated and then step into the form code -- run using F5 in visual studio so it will run the debug copy.
Post the code from your Program.cs and your main form constructor and OnLoad methods so we can see it.
Edit:
The RefreshForm() code is 99% most likely the problem. Need to see that code, but I'm betting if you comment out that line the app will run.
The first thing I would do is take that RefreshForm() method and put it in a OnLoad event, wrapped in a try..catch block
try
{
RefreshForm();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
That way if RefreshForm is the issue then you will get the reason, and the app will at least start.
If you don't see a message box, then it is something more fundamental.
So it is clear for future reference, what solved the problem:
removed function that played with form elements from constructor and onload, moved it to an onShow event-tied function
Cleaned and rebuilt everything.
I also needed to add "Runtime Implementation from 'project name here'" and "Localized resources from 'Project name here'" in order for it to run. (this was not specified in the guide I followed)
Related
I have a visual studio windows forms application that has multiple projects, each having multiple forms. The project that loads on startup calls another project's form and closes its own. Referring to the startup program as "setup" and the other as "main". I have a scenario where I want to skip setup and be able to return to it later. Since setup is a dependency of main, and not the other way around, I cannot seem to create an instance of the setup form. Is this the case, or am I doing something wrong?
The call from setup to open main is as so
this.Hide();
frmDemo demo = new frmDemo();
demo.ShowDialog();
this.Close();
I want to do the same thing from main to setup form, but I am having trouble.
Essentially it should just be
frmSetup setup = new frmSetup();
setup.show();
But this isn't working because it cannot find the form.
Well if you want to reference Setup from Main then you need to add a reference in Main.
The problem then is that you will create a circular dependency, which is bad design (and I think you will also get compiler errors).
There are some ways to get around this, but the best approach it to make your Main program the first one that starts-up (not setup). Then during startup it launches the set-up form and waits until it is finished and then continues. And then when it needs to launch setup again, then it is no problem.
So you are effectively reversing the dependency.
I've been developing using C# from scratch for less than 3 months and what I got at present is a console application made with Visual Studio 2015. This application consumes a web service, the XML is deserialized, formatted and saved in an Access database, and lastly a success or error XML is sent back to the web server. 3 classes make up the application: The main stream class, a SOAP client class and a DB class.
Now the reason why I'm posting this it's because I need to get that console app integrated with a GUI in which some data gotten from the deserialized XML should be shown (this is an extension of the project that the stakeholders didn't think about before stepping on the developing stage), but I have no idea how to do it.
So far I am reading about "Building WPF Applications" and "Windows Forms" but the documentation is overwhelming and I don't know if I'm on the right path, so can you guys give some guidelines before I start wasting time coding stuff that maybe are not the best option to integrate my console application with a GUI? Is there a way to make the console application become a GUI based application?
I would appreciate if you provide me with practical links to tutorials in which they quickly implement GUIs, my time is running out and I need to start coding right now. Thank you very much for reading this and helping me.
If you're just looking for a barebones GUI and aren't too worried about it looking polished, I'd suggest you right click on your project -> add->Windows Form.
Then, turning your your Console App into a GUI based application is as simple as instantiating your Form-derived class and calling .ShowDialog().
Example:
using System.Windows.Forms;
//Note: if you create a new class by clicking PROJECT->Add Windows Form,
//then this class definition and the constructor are automatically created for you.
//Open MyFancyForm.cs, right click on the form, and click View Code to see it.
public partial class MyFancyForm : Form
{
public MyFancyForm()
{
InitializeComponent();
}
}
static void Main(string[] args)
{
var guiForm = new MyFancyForm();
guiForm.ShowDialog();//This "opens" the GUI on your screen
}
It's that simple. I suggest you add add a new class to your project as a WindowsForm instead of just a class so that you have access to the form designer. Of course, it's up to you to add fields to your form class and have it populate the GUI appropriately.
Edit: if you wish replace the Console entirely, right click on your project->properties and then change "output type" to Windows Application.
What you're trying to achieve is actually relatively simple and straight forward. You need to launch your existing console application process from a new Windows Forms or WPF application and subscribe to its input and output streams.
If you don't want to show your current console application as a visible window, remember to set CreateNoWindow to true.
var processStartInfo = new ProcessStartInfo(fileName, arguments);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.CreateNoWindow = true;
// etc.
Here's an article that goes through all the steps: Embedding a Console in a C# Application
The above article's code can be found on GitHub if you want to take a closer look. Here's an example of what sort of embedding can be done:
With the project it's easy for you to either hide the whole console or have it side to side with some custom UI controls that do further processing with your data.
You can put a TextBox inside your form and redirect anything that would go in the console to the TextBox.
see this for more info
Redirecting Console.WriteLine() to Textbox
Without entering in "why" territories, if you have a console application project you can right click the Project -> Properties -> Application and change the Output type to Class Library (or to Widows Application).
If you choose class library then you can consume your library from wherever.
I am developing a C# application in Visual Studio 2008 that uses folder browser dialog and printdialog. Lately the program is encountering an error whenever ShowDialog() Method for these two dialog boxes is called.
The error says:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The strange thing is that this error is specific to this project only. When I use folder browser dialog in a new C# project it works perfectly fine. Moreover this error started occurring recently in the mentioned project. Earlier it used to work perfectly fine. Also, this error does not occur when I run the program from the IDE in debugging mode.
There is a suggestion in some other question on Stack Overflow to uncheck "Suppress JIT optimization on module load" in debugging options. I have tried that too but that did not solve my problem.
Can someone please help me solve this problem?
It's difficult to say what without seeing any code. I've seen running into this issue with accessing UI on threads other than it was created. To avoid it, if you're messing with UI controls from some thread, you can create a function like the following
public static void ExecuteAction(Control myControl, Action myAction)
{
if (myControl.InvokeRequired) { myControl.Invoke(myAction); }
else { myAction(); }
}
Then you just call that from whatever thread you want with the control and the action you want to do (say, change its color or what).
I haven't seen the issue with Dialogs specifically since I haven't done anything with them in a long time, but since it's a UI issue, and it's the same error message I've seen before, I'd give it a shot.
You might also check the value of Control.CheckForIllegalCrossThreadCalls. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx
I have taken over support of a VB.Net WinForms application. I am actually a c# developer and am more familiar with the setup of visual studio projects in c# projects. Now I am trying to determine why my application is crashing on a specific XP installation, and I read the suggestion here
http://social.msdn.microsoft.com/forums/en-US/winformssetup/thread/53c2de93-ab33-41d0-b5dd-7ca5fbfa5c24/
to add a try catch block in the main function. This is suggested in about the 5th post from the bottom. (I will quote it below) However, if I look in the VB.Net visual studio project, I do not find a Main() procedure. What I do find is a grey folder called "My project" with a "Application.myapp" file inside it. This file has an associated designer file, but if I click on it I see the following xml:
<?xml version="1.0" encoding="utf-8"?>
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MySubMain>true</MySubMain>
<MainForm>MDIMain</MainForm>
<SingleInstance>false</SingleInstance>
<ShutdownMode>0</ShutdownMode>
<EnableVisualStyles>true</EnableVisualStyles>
<AuthenticationMode>0</AuthenticationMode>
<SaveMySettingsOnExit>true</SaveMySettingsOnExit>
</MyApplicationData>
So can anyone enlighten me to where the actual main procedure call is for this VB.Net project so that I can try catch the exception that is occurring. If, as I suspect, there isn't actually a Main procedure in my VB.Net project, can someone maybe let me know how I can go about doing the following in my project:
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (System.IO.FileNotFoundException ex)
{
MessageBox.Show(ex.Message + " \n\n\n" + ex.StackTrace);
}
}
VB has a special mode called “Application Framework” (which can be found under the main options).
If this mode is enabled, the compiler auto-generates a Main method and some fluff around it. You can disable this option; however, this may cause problems in the project since the application framework functionality might actually be used by the project.
Alternatively, you can register an event handler for uncaught exceptions (UnhandledExceptions) using this same application framework.
The more VB way to do this is to open the Application properties and click on the ViewApplicationEvents button. This will open the Application.xaml.vb file where you can add custom event handlers for the application. Select Application Events from the left drop-down and you can easily access a bunch of events including DispatcherUnhandledException, Activated, Navigating, Startup, Exit, etc. You can also add the Main method here by selecting Applciation from the left drop-down and selecting Main from the right drop down.
In the case of WindowsForms applications, the process is similar. However when you select the Applciation Events button, the file that is shown is the ApplicationEvents.vb file. In here, to add a global error handler, select the left drop-down and select MyApplication Events. Then in the right drop-down, add the UnhandledException handler. You can also create your Main method here as well.
It is generated automatically by the compiler when it can't find one, but you can create one yourself.
http://msdn.microsoft.com/en-us/library/ms235406%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/y4bwckbb.aspx
I came to this page today in search of answers, and I found some good ones, both here and in The Code Project.
By the time I satisfied myself that I knew what to do, I also had in hand a simplified approach that leaves the project properties virtually untouched. (You must turn off the Application Framework, or the VB runtime won't run your Main routine!) In a nutshell, if you define your Main routine in the class module that defines your startup form, the Visual Basic runtime engine will find and execute it.
As is stated above, your routine must be defined as Shared. You can see my example, along with a few other notes, at How to Run a Particular Form in VB.NET
Caveat
When you disable the Application Framework, you lose the Single Instance check box. I just finished updating the cited example to include the code that I developed and tested to enforce single instance.
You can create that method anywhere, as long as it's Shared. To wire it up, you have to go into the project settings and set the entry point to be your Main method.
I have a windows service in VS.net 2008. Just to test the application before I install the service, I have a form (Form1.cs).
Now when I want to make this project as startup project and form1 as startup object,
I don't see the form one in the start up object list.
I just see the namespace of the Program.cs and not set
Please help
Presumably you don't have a
static void Main()
method in Form1.cs. (The method could return int and/or take a string[] parameter, and it doesn't have to be private.) That's what makes VS consider a class to be a potential entry point.
To be honest, it's be pretty odd IMO to have a form in the same project as a service. Why not have a separate project for it which would work in the normal way? Having multiple entry points is fine, but if one of them starts a service and the other opens a form instead, that's a little strange.