|
If I create a program in c# with multiple threads,
does the OS automatically spread them over the available cores?
Or is the app just divided in some time slices on a single core?
|
|
|
|
|
Yes, If you create and execute multiple threads, the OS will run each thread on whatever CPU it thinks will give the best performance to that thread.
Simon
|
|
|
|
|
Hello,
Could someone tell me if its possible to create a OLE Server using C#? I want to be able to insert documents from the C# into other applications using the "Insert Object" method.
Many Thanks
|
|
|
|
|
|
I have a regular System.Windows.Forms.ComboBox. This control listens to OnDrawItem(DrawItemEventArgs ea) events. How can I programmatically generate this event for an arbitrary item in the ComboBox? Thanks for any help.
|
|
|
|
|
arnold_w wrote: I have a regular System.Windows.Forms.ComboBox. This control listens to OnDrawItem(DrawItemEventArgs ea) events.
I think you have misunderstood events. The combo box triggers DrawItem events. Your code listens to them.
You can attach a listener using the += construct, like this
private void CreateComboBox()
{
ComboBox cbox = new ComboBox();
this.Controls.Add(cbox);
cbox.DrawItem += new DrawItemEventHandler(cbox_DrawItem);
}
void cbox_DrawItem(object sender, DrawItemEventArgs e)
{
}
Be careful not to attach listeners multiple times. Once attached, the listener will continue to respond to the event every time it is triggered until you detach it, or the object is destroyed.
Simon
|
|
|
|
|
Thanks for your reply. Ooops, sorry. Of course, you are correct. I shouldn't write posts early in the morning What I mean is that I have inherited the ComboBox and have overridden the OnDrawItem method:
public SpecialComboBox() : ComboBox
{
protected override void OnDrawItem(DrawItemEventArgs ea)
{
}
}
I want the OnDrawItem to be executed at my request. Of course, I can simply call the OnDrawItem method, but if I do I don't know how to determine the DrawItemEventArgs argument. Therefore, I would like to make it be executed "automatically". Is there something I can do, for example, to invalidate the control, call Refresh on the control, etc to force this event to be generated?
|
|
|
|
|
You need to set combobox.DrawMode = DrawMode.OwnerDrawFixed
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Ahh, I see what you're trying to do.
You shouldn't really be calling OnDrawItem yourself. The idea is that the method is called automatically whenever windows needs to draw the item. You then intercept that with your override and perform the customised drawing.
Like Leppie says, you need to enable owner drawing for the method to be triggers. This bit of sample code draws the items in red.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateComboBox();
}
private void CreateComboBox()
{
MyCombo cbox = new MyCombo();
cbox.DrawMode = DrawMode.OwnerDrawFixed;
cbox.Items.Add("test1");
cbox.Items.Add("test2");
this.Controls.Add(cbox);
}
}
public class MyCombo : ComboBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
String item = (String)this.Items[e.Index];
Debug.WriteLine("drawing: " + item);
e.Graphics.DrawString(item, e.Font, Brushes.Red, e.Bounds.Location);
base.OnDrawItem(e);
}
}
(Set it to fixed or variable, depending on the type of custom drawing you plan on doing)
Simon
|
|
|
|
|
Thank you both you for your suggestions, but I'm still unsure how I can use it in my application. This is basically what I need:
public SpecialComboBox() : ComboBox
{
protected override void OnDrawItem(DrawItemEventArgs ea)
{
}
private void MakeWindowsThinkItNeedsToDrawAnItem(int item)
{
}
}
Is it possible to implement the MakeWindowsThinkItNeedsToDrawAnItem method?
|
|
|
|
|
I think you may have a design issue here. Why do you want to make windows think it needs to draw an item?
You only want to execute the drawing code, when you actually need the item drawn.
If you need it called at some other time, I would suggest that you possibly have code in the OnDrawItem method that shouldn't really be there.
Simon
|
|
|
|
|
Again, thanks for your input. The reason I want to be able to make Windows think it needs to draw an item is that I want to be able to switch color of an item at any given moment.
|
|
|
|
|
In that case, just call Invalidate, the Draw methods will be called again, and you can paint your changes. Sometimes, Invalidate is not enough, the you can fall back to Refresh.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
You are correct, if I call Invalidate and Refresh that will cause OnDrawItem(DrawItemEventArgs ea) to execute. However, even if the dropdown area is show, ea.Index is always -1. If there are 5 items in the ComboBox, I would expect OnDrawItem(DrawItemEventArgs ea) to be called 5 times, one for each item. However, that is not the case. Can I somehow invalidate and/or refresh the individual items instead?
|
|
|
|
|
arnold_w wrote: Can I somehow invalidate and/or refresh the individual items instead?
I dont think so, I cant think of any hack ideas either.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Ok, thanks for your posts.
|
|
|
|
|
Can you pass true to the invalidate method to invalidate all children?
Simon
|
|
|
|
|
I just tried it, it doesn't make a difference. Thanks anyway for your suggestions.
|
|
|
|
|
Sorry, it was just a guess, I'm surprised it doesn't work. That's what the invalidate/refresh methods are for.
Simon
|
|
|
|
|
Hehe, my hacks was leaning towards invalidating only a specific region, but that would be real bad
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Good Morning
i have written something to kill a Process based on the Supplied parameter. Here is the Code
I created a Class that will Contain my Function like this
namespace Process_Killer
{
public class Process_Killer
{
public bool FindAndKillProcess(String name)
{
Boolean Bolres = false;
Process clsProcess = new Process();
if (clsProcess.ProcessName.Contains(name))
{
clsProcess.Kill();
Bolres = true;
}
else
{
Bolres = false;
}
return Bolres;
}
}
}
And after that i have a Win App , that will pass the name of the Process and in the kill button am calling the function that i created above like this
Boolean Bolres = false;
String Processname = txtProcessname.Text;
Process_Killer objProcesskiller = new Process_Killer();
Bolres = objProcesskiller.FindAndKillProcess(Processname);
if (Bolres == true)
{
MessageBox.Show("Process Killed", "Process Killer");
}
else
{
MessageBox.Show("Process not Found, Do not include the Process Extension", "Process Killer");
}
And when i run this app , i get the Error
No process is associated with this object.
Thank you
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
You are creating a new process, and then checking if it's name is the one you are looking for. It won't be, you just created it.
You need to be using System.Diagnostics.Process.GetProcessesByName(processName); to find the process with the name you want to kill.
Simon
|
|
|
|
|
Thank you it
It worked
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
I post this a C# newbie - please bear with me
I have coded a small project in C#, and ran into a problem illustrated in the below minimal project cs file.
CODE
---------------------------------
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int ScanProgress
{
set
{
progressBar1.Value = value; // Error: An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.progressBar1' ...
}
}
}
public static class AnotherClass
{
public static void SetScanProgress(int Value)
{
WindowsFormsApplication1.Form1.ScanProgress = 17;
}
}
}
---------------------------------
/CODE
I get the error when I refer to the non-static progress bar in the set method?
I would like to be able to do something like this, to show the progress of a scanner from a callback function, in the main GUI.
Or more general - how do I alter the Main GUI from another class/object?
Thanks,
munk
|
|
|
|
|
You method is static. You cant refer to instance variables.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|