This question already has an answer here:
How do I lock a windows workstation programmatically? [duplicate]
(1 answer)
Closed 5 years ago.
I want to implement an app that locks my open session on Windows 10, when an event occurs. I saw more or less the same question here , and the answer seems to say, there is no way to lock the Windows screen programatically. However, I already saw some applications do lock Windows screen (for example, Nymi Companion Device Application).
Do you know how to implement the locker? Or which module allow to achieve the task?
This is a complete sample code in c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool LockWorkStation();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LockWorkStation();
}
}
}
This works:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern void LockWorkStation();
Related
This question already has an answer here:
How to be dynamically either console application or Windows Application
(1 answer)
Closed 4 years ago.
I have this task given to me and have no idea how to approach it.. Everywhere online says that this isn't possible without .dll or mocking.
The server, when launched with an argument of –w should open a windowed interface that
permits an operator to control the functions of the server. If launched with no arguments it
should operate as previously specified in part 1
(part 1 is a console application)
I don't know if I'm missing something obvious.
Thanks for any help you can give
I don't know where you're seeing on-line that this isn't possible. Every Windows Forms application has a Main method, you just need to modify it a bit.
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
File.WriteAllText("hello.txt", "foo");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
I added the string[] args argument and then check it. I'm not checking for -w, I'm just checking for any old argument, but you should be able to take it from there.
It's absolutely not impossible. Just add reference to System.Windows.Forms, add it in the using clause, and go from here.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq;
namespace ConsoleWindow
{
class Program
{
static void Main(string[] args)
{
if (args.Contains("-w"))
{
Form1 f1 = new Form1();
f1.ShowDialog();
}
Console.ReadKey();
}
}
}
Hello I tried creating a rectangle in a wpf application and I'm recieving this error I tried googling it but it didn't work.
Error:
The calling thread must be STA, because many UI components require this.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace Game
{
class Engine
{
#region Members
private Thread _render;
#endregion
public Engine()
{
_render = new Thread(new ThreadStart(Render));
_render.Start();
}
private void Render()
{
while (true)
{
Rectangle rect = new Rectangle();
}
}
}
}
I think this would fix it:
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => this.progressBar.Value = 50));
But doesn't that take away the purpose of using a different thread because then all the rendering would still be done on the main thread.
And another question:
I'm trying to draw rectangles of 1 by 1 and detect collision if they "fall" on each other(a loop sets their x--).
Could I also do this with pixels?
Rendering should always be performed on the main thread. That is a design thing in Windows / Win32.
Thanks to the original design of Win32 (which involves handles to windows, aka hWnd's), the thread calling the painting operation must be the same as the one doing the actual painting.
There is a nice article on MSDN about the Threading model and WPF.
I am trying to embed managed c# code to c++ (with the help of tutorial in http://www.mono-project.com/Embedding_Mono and examples included in samples).
However I cannot get it to work. I think it is very possible that the problem is with a System.Net.Sockets.TcpClient object (for instance I can access values and methods of some other classes but if I add a TcpClient object to a class I encounter problems.
Here is some simple C# code I wrote to test just adding a TCPClient object
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
static void TCPTest()
{
TcpClient TCPClient;
//Console.WriteLine("in tcp test");
}
}
}
And the c++ code is here: http://codepad.org/f9D5bg8u (it is a stripped down version of the mono embedding sample).
When I build the C# code like this,
mono_runtime_invoke (method, obj, NULL, NULL);
in the c++ side exits with code 1.
When I comment that out and try the console.writeline, that works.
I appreciate any suggestions,
Thanks...
I am confused as to why a Window will not appear with the below code. Am I missing an import?
using System.Text;
using System.Xml;
using System.Windows;
using System;
using System.Windows.Forms;
using System.IO;
using System.Threading;
public class Program {
public Window mainWindow;
static void main() {
// Create the application's main window
mainWindow = new Window();
mainWindow.Title = "Enter SN";
mainWindow.Show();
}
}
You want to run your Window via an Application.Run() call. Your current code will not fire it off on a standard windows message loop, which is required.
Remove your Show() call and replace it with:
Application.Run(mainWindow);
To be even simpler, if you set your title as your wish on your WinForms designer, your main can be a single line:
Application.Run(new Window());
Also, you have many unnecessary using statements. These statements aren't a real problem, just unnecessary and confusing.
I am using .NET Remoting. My server/hoster is a Windows Service. It will sometimes work just fine and other times it will process one request and then it does not process any more (until I restart it). It is running as a Windows service Here is the code from the Windows Service:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.ServiceProcess;
using System.Text;
using Remoting;
namespace CreateReview
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
readonly TcpChannel channel = new TcpChannel(8180);
protected override void OnStart(string[] args)
{
// Create an instance of a channel
ChannelServices.RegisterChannel(channel, false);
// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SampleObject),
"SetupReview",
WellKnownObjectMode.SingleCall);
}
protected override void OnStop()
{
}
}
}
Thanks for any help offered.
Vaccano
as a SingleCall type, your SampleObject will be created for every call the client makes. This suggests to me that your object is at fault, and you don't show what it does. You need to look at any dependancies it has on shared resources orlocks. Try writing some debug out in the SampleObject's constructor to see how far the remoting call gets.