Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I recently joined a team responsible for the enhancement of an existing 5-year old program.
It was developed with multithreading in mind, but not correctly implemented.
The solution is composed of multiple Windows Forms programs (let's call them Screen 1 and Screen 2).
Screen 1 can launch Screen 2 multiple times with differents parameters, but launches them as Process, not as Threads, Tasks, or by using BackgroundWorker.
Here is how it's done :
public void RunProcess(Arguments Arguments,string ExcutableName)
{
Process Prc = new Process();
Prc.StartInfo.FileName = ExcutableName;
Prc.StartInfo.Arguments = Arguments.GetProcessArguments();
Prc.Start();
_ListProcess.Add(Prc);
}
The processes are kept in memory and killed when Screen 1 closes.
I would like to refactor this in a cleaner way, going for a "single process" approach. What would you advise?
It looks like you need to port Screen 2's logic into the Screen 1 project, then replace the "Process" logic with threaded code.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
When creating a console based app, is it functionally more efficient/ practically more efficient to run off of command line start arguments than it is to use Console.ReadLine();? I have two options, open the program and have it listen to my commands via ReadLine in a console window, or I can use CMD like: C:\MYPROGRAM argument and have it take my commands this way. (think ping.exe for example)
I have used string userCommand = Console.ReadLine(); along with if statements in order to control my program. I have also used userCommand = args[x] as a way to command my program from CMD.
Examples:
string userCommand = Console.ReadLine();
if(userCommand == "help")
{
Console.WriteLine(help);
}
Or
if(args[0] == "help")
{
Console.WriteLine(help);
}
Both of these methods are working for me, but I do not understand which of these is going to be more efficient in terms of speed/ performance. I also do not understand which one of these is practically a better solution. Is one of these superior to the other for any specific functional/ practical/ potential reason? Note: Google wasn't particularly helpful here.
The cost of creating a new process and initializing the CLR is several orders of magnitude larger than either of these. So there is no practical difference in efficiency between them.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
On my computer I have two monitors(regular one and usb elo display) and I wont my application only opens on usb display...any idea?
You have to use Screen class. It provides information about number of screens, which one is a primary screen etc. It also has the following method Screen.FromControl. It returns information about the screen that contains the largest part of the application. You can run it on the startup. If the method returns usb display then you should move the application to the second screen. See also this question.
Here is an example that verifies if the current screen is the primary one. If not it finds it and changes the location of the application based on the bounds of the found primary screen. You can also use DeviceName to determine if the current screen is the correct one.
var current = Screen.FromControl(this);
if (!current.Primary) //you can also use device name e.g." s.DeviceName.Contains("...")
{
var primary = Screen.AllScreens.Single(s => s.Primary);
SetBounds(primary.Bounds.Left, primary.Bounds.Top, Width, Height);
}
You should get the rectangle of required monitor first:
Rectangle rect = Screen.AllScreens[n].WorkingArea; //n stands for the index of monitor you'd like to use
Then you should invoke the Windows API SetWindowPos to move it via Process.MainWindowHandle.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the code to call a console application within a WPF? I have an application in WPF you need:
call a console application;
the console application closes the WPF
The console application again calls WPF and close console application.
This is necessary because I am doing a system where updates to the application must close the files to copy.
For close application WPF I am using the following:
Process wpfProc = Process.GetProcessesByName("MainWindow.exe").First();
wpfProc.Kill();
return in console application:
Unhandled Exception: System.InvalidOperationException: Sequence sontains no elements
at System.Link.Enumerable.First[TSource]<IEnumerable'1 source>
at Updater.Program.Main<String[] args> in d:\endereçodoUpdater\Program.cs:line 17
in line 17 have the following:
Process wpfProc = Process.GetProcessesByName("MainWindow").First();
How do I resolve this?
You do this with Process.Start:
Process myProc = Process.Start("MyConsoleApp.exe");
//Close gracefully
Application.Exit();
In MyConsoleApp.exe, you would need to use GetProcessByName to kill your WPF app, and then Process.Start again to restart it:
Process wpfProc = Process.GetProcessesByName("MyWpfApp.exe").First();
//If you want to directly kill it
wpfProc.Kill();
//Or be nice and let it kill itself
wpfProc.WaitForExit();
//Do stuff
Process.Start("MyWpfApp.exe");
System.Diagnostics.Process on MSDN
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 8 years ago.
Improve this question
I am working on one project where I stuck on one point where I have to run two methods in parallel.
In Function 1
In my application what I am doing is I am grabbing images from the IP cam and storing that image into the one folder.
This function is used for continues streaming of camera.
For this you can refer this question which I have asked IP Camera stops streaming.
In Function 2
I will pick images from the path where my Function2 is dumping images.
Here I am doing some other operations like:
Save Image captured from the IP Camera
Detect faces in Image
Draw Face markers on Image
Some database based on result of Face Detection
Delete image File
Function 2 takes more execution time than Function 1.
So for this purpose after searching on google I get to know I can do this by multithreading.
So, I am little bit confused about this and as I am new in c# I am not that much aware of multithreading.
So, can anyone help me out on this?
You do indeed need to use multithreading, and in your case it should not be too difficult.
You'll need to add a "using System.Threading;" to the start of any files that involve threading.
public void Function1()
{
//Do camera stuff
Image image = MagicalCameraStuff();
//Create a thread that the processing will occur on
Thread process = new Thread(() => Function2(image));
//Start the thread
process.Start();
}
public void Function2(Image i)
{
//Do some processing without blocking the main thread
}
More information on threading:
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.