So I am trying to executing javascript in my forms program, which is in visual studio. I am using C#. I have created a virtual web browser, and I am running the code in it which works fine. The only problem is I need to run a wait function in the script, but that freezes the whole program. I am trying to make a new thread and run the javascript in that so I can wait without freezing the uis. I have been trying to do this but the thread exits before the javascript code runs more than a single line because it thinks there is no code left to execute. This is because the javascript code is in the web browser HTML page. The thread stopping makes the javascript execution stop. The javascript code works fine if I run it in the main script since the thread stays open, but I can't run wait without freezing the program. I have tried running a timer to keep it open but it doesn't work. Is there any way I can force a thread to not auto-close?
Thread code to start the web browser:
public void ThreadStart3()
{
LogToConsole("Starting Script!", true);
WebBrowser Browser = new WebBrowser();
Browser.ScriptErrorsSuppressed = false;
var ns = new ScriptManager(_Form, _Num, _ConsoleRun, _MiniConsoleRun, @"G:\MemuProject\MemuScripter\Scripts\" + _Name + @"\", Int32.Parse(_Table[_LowestID][7]), _Device, _Table[_LowestID][1]);
Browser.ObjectForScripting = ns;
Browser.Navigate(@"G:\MemuProject\MemuScripter\Scripts\" + _Name + @"\" + _Name + @".html");
var timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(TimerRun);
timer1.Interval = 20000; // in miliseconds
timer1.Start();
// EVEN WITH A TIMER THE THREAD STILL CLOSES!
}
Code to create the new thread:
var t = new Thread(ThreadStart3);
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();