When I set my code going, the whole GUI freezes and I can't select anything or even move the window until the code has finished.
Is there a way to interact with the GUI whilst a piece of code is running?
When I set my code going, the whole GUI freezes and I can't select anything or even move the window until the code has finished.
Is there a way to interact with the GUI whilst a piece of code is running?
Without sending us some of your code or at least telling us what your code does, it is very difficult to answer your question.
private void button1_Click(object sender, EventArgs e)
{
ThreadStart job = new ThreadStart(modem1_start);
Thread thread = new Thread(job);
thread.IsBackground = true; //thread will run in foreground
thread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
ThreadStart job2 = new ThreadStart(modem2_start);
Thread thread2 = new Thread(job2);
thread2.IsBackground = true; //thread will run in foreground
thread2.Start();
}
I want to be able to push button1 and get that thread going and whilst the code is executing, I want to be able to push button2 and get that code going.
Unfortunately, when I push button1, I got locked out from the GUI
There is nothing wrong with the way you are starting the threads.
What are you doing in modem1_start and modem2_start?
Try breaking the execution in Debug and look at what code is executing.
This can sometimes give a clue as to why it is hanging.
Yes, you are right. The problem comes when I call an external exe file.
I have the code;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @"FILENAME.exe";
proc.Start();
proc.WaitForExit();
Between 'Start' and 'WaitForExit', I am locked out of the GUI. Is there anyway to allow control of the GUI whilst I am waiting for my exe program to exit?
Specifically, it would be nice to be able to use a progress bar that begins on 'Start' and finishes on 'WaitForExit'.
E.g;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @"FILENAME.exe";
proc.Start();
progressbar1.Style = ProgressBarStyle.Marquee;
progressbar1.MarqueeAnimationSpeed = 100;
proc.WaitForExit();
or;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @"FILENAME.exe";
proc.Start();
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
progressBar1.Step = 1;
while(proc != finished)
{
progressBar1.Value = 0;
for (int i = 0; i < 5000; i++)
{
Thread.Sleep(1);
progressBar1.PerformStep();
}
}
proc.WaitForExit();
Is the whole OS locked up while the external program runs or is it just your app?
If it is the whole OS then you need to look at what that program is doing.
If it is just your app then try allowing your app to continue by handling the Process.Exited event in your code.
private void modem1_start()
{
//...
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = true; // <-- allow events to execute
proc.StartInfo.FileName = @"FILENAME.exe";
// capture process finished
proc.Exited += new EventHandler(proc_Exited);
proc.Start();
//proc.WaitForExit(); // <-- do not wait
}
void proc_Exited(object sender, EventArgs e)
{
//Do post process things here
}
[Edit]
You will probably need to declare the process variable proc outside the thread method.
(Otherwise Garbage Collection might get rid of it before the process finishes)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.