|
I have a thread (which is not the main GUI thread )that I want to be able to create a form using the Show method.
The thread basically loops for commands and responds to them. Some of the commands are to display a form - and then wait for the next command.
Now when I try to do this the Form freezes i.e. not responding. Now I assume that is because the thread is blocking waiting (Monitor.Wait) for the next command. I suppose this blocking prevent the GUI from responding to events.
So my question is related to this problem.
1. Is is good practice to create Form from a thread other than the main GUI
2. Can I do a for.Show method and spawn it into a worker thread?
All thoughts and suggestions would be appreciated.
|
|
|
|
|
Hello
LiamD wrote: Is is good practice to create Form from a thread other than the main GUI
If you want your working thread to be blocked by the shown forms, you can use it to show these forms. Simple, isn't it??
OTOH I don't think is what you want. So, simply make a thread for each form that is shown. The thread should be alive as long as the form is visible. Once the form is closed, abort the thread nd dispose it.
And yes of course!! Sometimes this is a good practice, depending on the structure of your program.
LiamD wrote: Can I do a for.Show method and spawn it into a worker thread?
I don't know what is a "for.Show" method, but you can -and probably should- show each form on its own thread as above.
Regards
|
|
|
|
|
I was concerned because i have never seen any examples of projects creating a thread for each form.
Can you please give an example of code for this? Something liek this ...
Form f = new Form();
t = new Thread(new ThreadStart(f));
t.Start();
I just want to make sure that I will not run into problem using this method when running in a multi threaded project.
By the way I meant to write Form.Show() method above.
Thanks,
Liam
|
|
|
|
|
Hello
Well, running into trouble in multithreading is as easy as slipping in the mud in a rainy day!! There are two scenarios. One if you want to keep track of each thread -for several reasons I suppose-. This is the hard way, so I'd rather skip it for now .
The easy way is to launch each thread and let it die as it finishes without tracking -Thank God for the GarbageCollector!!-. Here is a sample code:
private void button2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Thread MyThread = new Thread(new ThreadStart(MyThreadMethod));
MyThread.Start();
}
private void MyThreadMethod()
{
Form1 MyForm = new Form1();
MyForm.ShowDialog();
}
The Thread will die and get collected once the form is closed. Carful not to open too many forms though.
Regards
|
|
|
|
|
in my program i diclar
string itm1,itm2,itm3,itm4,itm5,itm6,itm7,itm8,itm9,itm10,itm11,itm12,itm13;
i want to make method as
save(int ID,string Value)
{
itm+ID=Value;
//itm1=value;
}
the problem now in this line ([itm+ID=Value;])
i cant make this
but how cam make thing like this
?????????????????
Palestine
|
|
|
|
|
Hi,
Try using an array. Like this:
<br />
string []items = new string[13];<br />
And then create the function:
<br />
public string Save(int index,string Value)<br />
{<br />
items[index] = Value;<br />
}<br />
Do your best to be the best
|
|
|
|
|
TAREQ F ABUZUHRI wrote: string itm1,itm2,itm3,itm4,itm5,itm6,itm7,itm8,itm9,itm10,itm11,itm12,itm13;
i want to make method as
save(int ID,string Value)
{
itm+ID=Value;
//itm1=value;
}
That won't work, Tareq. And it's bad design too, don't do that. Instead, use a string[]:
string[] items = new string[13];
...
save(int ID, string Value)
{
items[ID] = Value;
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Dumbest. Movie. Title. Evaaar.
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
*edit* oops, karkster beat me to it.
|
|
|
|
|
Hello
Put your items in an array and access them using the index.
string MyItems[] = new string[13];
public void Save(int ID, string Value)
{
MyItems[ID] = Value;
}
Regards
|
|
|
|
|
Hello,
if you whant to make it dynamic some day, you should use "System.Collection.Specialized.StringCollection"
All the best,
Martin
|
|
|
|
|
Even better than an array of strings!
private dictionary<int, string> items = new dictionary<int, string>();
public void save (int id, string value)
{
items[id] = value;
}
-- modified at 14:41 Monday 28th August, 2006
Maybe I should clarify why its better... your "id"s need only be unique with a dictionary, whereas with the string array they would need to be, unique and sequential. Also stuck in a certain range (unless you used an offset).
|
|
|
|
|
I have inserted a datagridviewcheckboxcolumn. But the values of those the checkbox cells are all set to null by default. How do I change them all to "false" which is unchecked?
|
|
|
|
|
Hello
Is your DataGridView databound?? In this case you could just use this line of code to return false to the datasource when the user leaves the value null:
dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false;
If you still want to initialize the values of the cells, I remember something about default values in DatagridView, but i don't seem to recall it at the moment..
What I can think of now is to handle the RowsAdded and initialize the new cells value to false.
Regards
|
|
|
|
|
Hi
I have set the datagridview.datasource to the defaultview of a datatable. Is it databound then? The thing is that the checkbox column is inserted to the datagridview with null values by default.
The idea is to let the user to check for export. Cos the unchecked boxes are of null values, I've got an error when the null value is accessed.
|
|
|
|
|
Hello
I provided you with two possible solutions in my last post. Are they not suitable??
1- You could ignore the null value and return false to the datasource. This should eleminate the error:
dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false;
2-Otherwise you could handle the RowsAdded event and set the value of your column for each new row to false
Regards
|
|
|
|
|
Hi
I've tried both but they don't seem to work. It could be that the datagridview is not databound??? For the 2nd solution, it seemed to work initially, but I don't know why the value switched back to null again. I have made the datagridview a global variable, so nothing should have changed the value back.
Now I don't know what to do.
|
|
|
|
|
Hello
printscreen12345 wrote: I don't know why the value switched back to null again.
What??!!
What do you mean switched back again?!! Are you sure you did initialize them to false??
Also, how do you populate your DataGridView if it's not databound?
Regards
|
|
|
|
|
Hi
Yes, I did initialise them to false in the "try" block and they switched back to null in the "finally" block.
I'm not sure if the DataGridView is databound or not. As mentioned in my earlier messsage, I have set dataGridView1.DataSource to a defaultView of a datatable. Is this databound??
|
|
|
|
|
Hello
Yes this is databound, as long as your DataGridView is populated automatically via the view.
now let's work this out step by step..
1-Does your try throws an exception?? I mean do you catch anything in the catch block??
2- After you change the value to false, make a break point and watch the value of the last row to be changed.
Regards
|
|
|
|
|
Hi,
There is no exception thrown at all. It goes straight into the finally block from the last line of code in the try block.
I set a break point at the last line of code in the try block and watched the value of the 1st 10 rows of the checkboxcolumn. They were all set to false. But when it stepped into the next line of code (which is the "}" of the try block), they all turned back to null value. This means, it is at the end of the try block that they were switched back.
I just realised one thing. The datagridview is displayed when I press a search button. It is very strange that the false values are all switched back to null only the first time when the search button is pressed. They all remained set to false the second time onwards. Actually it has the same kind of problem with the row numbers display - it will never display the row numbers for the first time but it will from the 2nd time onward. Strange?!!
Thanks
-- modified at 21:12 Tuesday 29th August, 2006
|
|
|
|
|
Hello
printscreen12345 wrote: Strange?!!
Well, errors like these in Visual Studio are what keeps Borland on business;P
Anyway, My last question would be: Do you put the false value directly into the cells, or do you put some sort of a local reference field in the try block to hold it?? If you put it in a local field it will turn to null once the block is finished, otherwise really... I'm out of guesses.
printscreen12345 wrote: it is at the end of the try block that they were switched back.
I guess I'd pass out if I see something like that while debugging??
Regards
|
|
|
|
|
Hello,
I've tried various ways to solving my problem. First using the System.management I tried to get to the WMI data of Exchange but keep getting an Access Denied error.
What I did now is create a DLL in VB6.0 and set a COM reference to it in my C# project. When I use my code in the click event of a button, the code works fine (it creates a file with the size of the public folders). When I try to use it in a DLL, the code no longer works. Very frustrating. Been looking at this for days already.
The code stops when I call the function of the VB6.0 dll.
I've already tried changing the security in WMI but after restarting the WMI service, I still get the access denied error.
Very weird.
Can anyone help out ?
Jason
|
|
|
|
|
I'm certainly confused. What do you mean, "use it in a DLL". .NET assemblies are DLL's
only two letters away from being an asset
|
|
|
|
|
What I did is created the code in VB6.0 and saved it as a DLL.
From C# I added the DLL through the COM tab.
Then I used the function defined in the DLL. But the code only works when I use it in the click event of a button. If I want to save my code as a DLL, then it no longer works.
What I really want to do is access the WMI from Exchange. But with C# it's a pain in the ... . that's why I switched to this VB6.0 DLL import into C#. But even that seems a hard job.
You would suspect the code to work in the .net DLL if it works in a form.
Could this have to do with some sort of protection level of variables. I have the impression that the parameters passed to the function aren't getting through once the code is used inside a .net Dll.
|
|
|
|
|
I actually thought that this was the forum where you get answers quickly.
I found out that when using WMI, the permissions have to be set and the service had to run as administrator (although I was logged in as admin it still didn't work). Next to that the app.config file contained 2 \'s. In c# you have to use 2. But when it's a variable inside the app.config .net sort of translates it correctly. Giving 2 slashes caused the whole thing to crash.
So thanks for the help (though I found it all myself LOL)
|
|
|
|
|
killazzz wrote: I actually thought that this was the forum where you get answers quickly.
Considering CodeProject and every other forum out there is an all-volunteer effort, and we all have real lives and jobs that need our attention also, you can't really expect this 100% of the time, if at all...
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|