When debugging my program, as soon as it hits this line, it crashes:
if (fbd.ShowDialog() == DialogResult.OK)
The errors I get are
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
And
Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away
I have no clue why this is happening. The line in question is at the very beginning of the program, there is no multi-threading that I am aware of (at least none that I intentionally developed), and the error only appears during debug, not in the build.
I have looked in to these errors, and have tried these solutions for the Multi-Threading, and these solutions for the "optimized away" issue, but neither made any difference.
I have the belief that this program was initially built as a Console application, then changed later to a Windows application, as I needed to add a reference to System.Windows.Forms
(which is what the program is complaining about), so that I could use the MessageBox
.
Here is my Code:
Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FlirAdept_Plugin
{
class Program
{
static void Main(string[] args)
{
//Get list of files & extract Meta Data
FlirFileList imageList = new FlirFileList();
imageList.FlirJpg();
}
}
}
FlirFileList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Windows.Forms;
namespace FlirAdept_Plugin
{
//Gets the image files from a specified directory and extracts the metadata if there, if not, just returns the file directory and name
class FlirFileList
{
int textLength = 0;
string[] flirFiles;
string line = "";
string findText = ".0 index";
string fileName = "";
string passString = "";
string inPathString = @"C:\Users\Frank\Desktop\FLIR\Thermo images"; // where to read from
string outPathFileString = null; // where to write to
FolderBrowserDialog fbd;
SaveFileDialog sfd;
public void FlirJpg()
{
Console.WriteLine("All new FLIR images should be in C:\\Users\\Frank\\Desktop\\FLIR\\Thermo images /n");
Console.WriteLine("CSV file will be written to C:\\Users\\Frank\\Desktop\\FLIRb /n");
Stream sw = null;
sfd = new SaveFileDialog();
sfd.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
fbd = new FolderBrowserDialog();
MessageBox.Show("Please select the folder to scan...");
if (fbd.ShowDialog() == DialogResult.OK) // Crashes here
{
inPathString = fbd.SelectedPath.ToString();
}
MessageBox.Show("Please select where the file will be saved...");
if (sfd.ShowDialog() == DialogResult.OK)
{
if ((sw = sfd.OpenFile()) != null)
{
//[etc...]
Please let me know if there is additional information required.